微信團體號可以實現報警信息 微信通知,或者用郵件實現報警,但是這些在調用的時候比較麻煩,需要寫點代碼,為了解決這個問題,我用flask封裝了微信的報警功能,用api的形式去提供服務,運維組的都可以調用,而不需要自己申請corpid。當然一個公司肯定會申請一個id,如果沒有自己申請一個,下面的代碼corpid我已經處理了,不能用。
#!/usr/bin/env python # coding=utf-8 from flask import Flask import urllib2 import json from flask import request import datetime now = datetime.datetime.now() nowStyleTime = now.strftime("%Y-%m-%d %H:%M:%S") get_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=efc44e79734&corpsecret=ALXK-IiA6Jidt84F-ll4x5goaa7U3iOOmCBOlKT80oAVKkWrltHDE9kH' res = urllib2.urlopen(get_token_url).read() token = eval(res)['access_token'] sendurl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % (token) app = Flask(__name__) @app.route('/api/send', methods=['GET']) def index(): """ 使用說明 curl -X GET "http://127.0.0.1:5000/api/send?username=test&content=123456" username 需要去關注自己的團體號,在後臺定義一個名字 countent 就是你需要發送的事件內容 """ username_list = ['test', 'test002'] username = request.args.get('username') if username not in username_list: return "username is not correct", 804 content = request.args.get('content') json_txt = { "touser": username, # 可以是 user1 | user2 | user3; @all是全體成員 "toparty": " PartyID1 | PartyID2 ", "totag": " TagID1 | TagID2 ", "msgtype": "text", "agentid": 1, "text": { "content": '[' + str(nowStyleTime) + ']' + '\r' + content }, "safe": "0" # 0是默認,不保密 1是保密 } json_txt = json.dumps(json_txt, ensure_ascii=False, encoding='utf-8') req = urllib2.Request(sendurl, json_txt) res = urllib2.urlopen(req) return res.read() if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
轉載地址:
https://blog.51cto.com/ribble/1834057