想要說的話
鋪墊python
本腳本初始版原本自網上,做者不詳,有知道能夠聯繫我,讓我等認識下
微信中查看代碼可能有些錯亂,能夠移步 https://github.com/zhuima/wechat_for_zabbix/blob/master/zabbix_alert.py
腳本以下,若有疑問能夠聯繫我git
腳本內容github
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author: zhuima # Date: 2017-06-21 update V4 """ 一、引入warnings模塊,去除urllib3訪問https站點的warning信息 二、python正常傳參數應該是sys.argv[1] sys.argv[2] 三、zabbix傳參的時候應該是sys.argv[2] sys.argv[3] """ import warnings with warnings.catch_warnings(): warnings.filterwarnings("ignore",category=DeprecationWarning) import requests import json import sys import time reload(sys) sys.setdefaultencoding( "utf-8" ) title = sys.argv[1] content = sys.argv[2] class Token(object): def __init__(self, corpid, corpsecret): self.baseurl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}'.format(corpid, corpsecret) self.expire_time = sys.maxint def get_token(self): """ 微信Token過時時間爲2小時,天天的請求數不能超過2000次 這裏使用時間戳判斷,避免每次請求都去從新獲取一次 requests.packages.urllib3.disable_warnings() 防止ssl類的warnings信息 >>> qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token() token: AVOg9PjnEC9dEl1UIMeAgN5adedkLBo7J538GdLLvvinkmpL356Ic341g2WTasX3EDjc7mf06J7GdkyNI0mUB4dfZhuRsdfgSNT7Sf1v2ceiND1uibTQQLECduF_Omdg8RElKMA_UK5qM4AXwSGGQjLqJdX5ZLWeZ-Af1xCTtLDdoHu75VMVOwvEApbmFcxRYsj0So8FEGXSyH5dxxka-3mvMqYESX_lzEIDSFKHcMr3yd1ZJgRlb7_Ejzq_loVj4sT4B7qtCjOC2PRmuyQ6aRcU0Z55ePLJpoz-8KS4W7Qgzg full result: {u'access_token': u'AVOg9PjnEC9dEl1UIMeAgN5adedkLBo7J538GdLLvvinkmpL356Ic341g2WTasX3EDjc7mf06J7GdkyNI0mUB4ZsdfsdegSNT7Sf1v2ceiND1uibTQQLECduF_Omdg8RElKMA_UK5qM4AXwSGGQjLqJdX5ZLWeZ-Af1xCTtLDdoHu75VMVOwvEApbmFcxRYsj0So8FEGXSyH5dxxka-3mvMqYESX_lzEIDSFKHcMr3yd1ZJgRlb7_Ejzq_loVj4sT4B7qtCjOC2PRmuyQ6aRcU0Z55ePLJpoz-8KS4W7Qgzg', u'expires_in': 7200, u'errcode': 0, u'errmsg': u'ok'} """ if self.expire_time > time.time(): requests.packages.urllib3.disable_warnings() resp = requests.get(self.baseurl, verify=False) ret = resp.json() if ret.get('errcode') != 0: print ret.get('errmsg') sys.exit(1) self.expire_time = time.time() + ret.get('expires_in') self.access_token = ret.get('access_token') return self.access_token def send_msg(title, content): """ 一、corpid 和 corpsecret要從後臺獲取 二、agentid 也是從後臺獲取 >>> send_msg("測試", "這個是功能測試") result: {u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'} """ corpid = 'wx91d6csdfsdcd60da' corpsecret = '8mAFHPj-RynmOBX05isdfsdfasdfwerdvbaiGyubx4eSeEB7nEwIJSgUpuVlPYV' qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token() url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format( qs_token) payload = { "touser": "@all", "msgtype": "text", "agentid": "10", "text": { "content": "標題:{0}\n內容:{1}".format(title, content) }, "safe": "0" } requests.packages.urllib3.disable_warnings() ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False), verify=False) return ret.json() if __name__ == '__main__': #title = "這是測試" #content = "豆豆親筆書寫" print send_msg(title, content)
本文分享自微信公衆號 - 追馬Linux(zhuima_k8s)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。json