一、 申請企業微信賬號,申請地址 https://qy.weixin.qq.com/
二、 登陸企業微信賬
圖一
圖二
2、添加微信賬號
圖一
圖二
完成以上步驟后 就完成了微信賬號的添加
三、新建應用
圖一
圖二
圖三
圖四
以上四幅圖完成后就應用創建完成
四、設置權限管理
圖一
圖二
圖三
完成以上三幅圖的操作,權限管理設置完成;到此微信設置已經完成!
五、Zabbix Server配置
圖一
圖二
圖三
完成以上三幅圖中的配置,則zabbix server的配置已經完成。
七、weixin.py程序內容
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Create time 2016-10-08
4 #Auth chenpeng
5 import urllib2
6 import json 7 import sys 8 import time 9 10 class WebChat(object): 11 def __init__(self,CropID,Secret): 12 self.CropID = CropID 13 self.Secret = Secret 14 def Get_Token(self,info): 15 ''' 16 :param info: 存儲執行結果和執行程序狀態碼code (0代表執行成功,非零表示不成功) 17 :return: 18 ''' 19 self.info = info 20 gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (self.CropID,self.Secret) 21 try: 22 #通過Get方式獲取token 23 req = urllib2.Request(gurl) 24 response = urllib2.urlopen(req) 25 g_result = json.loads(response.read(),"UTF-8") 26 27 if g_result .has_key('access_token'): 28 self.info['result']= g_result ['access_token'] 29 self.info['code'] = 0 30 else: 31 self.info['result'] = g_result 32 self.info['code'] = 1 33 except Exception,e: 34 self.info['code'] = 1 35 self.info['result'] = e 36 37 38 def Send_Msg(self,touser,toparty,agentid,access_token,content,info,*args,**kwargs): 39 ''' 40 發送信息到微信 41 :param touser: 部門成員id,zabbix中定義的微信接收者, 42 成員ID列表(消息接收者,多個接收者用‘|’分隔,最多支持1000個)。 43 特殊情況:指定為@all,則向關注該企業應用的全部成員發送 44 :param toparty: 部門id,定義了范圍,組內成員都可接收到消息, 45 部門ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數 46 :param agentid: 企業應用的id,整型。可在應用的設置頁面查看 47 :param access_token: 根據CropID,Secret獲取的訪問token值 48 :param content: 濾出zabbix傳遞的第三個參數, 49 表示發送微信消息的內容消息內容,最長不超過2048個字節, 50 注意:主頁型應用推送的文本消息在微信端最多只顯示20個字(包含中英文) 51 :param info: 返回執行結果信息{'result':None,'code':None};'code':0或者非零 ;0表示成功 非零表示失敗 52 :param args: 53 :param kwargs: 54 :return: 55 ''' 56 self.touser = touser 57 self.toparty = toparty 58 self.agentid = agentid 59 self.conntent = content 60 self.access_token = access_token 61 self.info = info 62 purl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % (access_token) 63 data = { 64 "touser": "", 65 "toparty": "", 66 "totag": "", #標簽ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數,非必須 67 "msgtype": "text", #必須 68 "agentid": "", #必須 69 "text": { 70 "content": "" #必須 71 }, 72 "safe": "0" # 表示是否是保密消息,0表示否,1表示是,默認0 73 } 74 data['touser'] = self.touser 75 data['agentid'] = self.agentid 76 data['toparty'] = self.toparty 77 data['text']['content']=self.conntent 78 data = json.dumps(data,ensure_ascii=False) 79 try: 80 #通過PUT方式獲取發送數據 81 req = urllib2.Request(purl, data) 82 response = urllib2.urlopen(req) 83 res = json.loads(response.read()) 84 self.info['code'] = res['errcode'] 85 self.info['result'] = res['errmsg'] 86 87 except Exception,e: 88 self.info['result'] = e 89 self.info['code'] = 1 90 91 if __name__ == '__main__': 92 reload(sys) 93 sys.setdefaultencoding('utf-8') 94 def log(date, touser, content,info): 95 ''' 96 發送的日志打印日志 97 :param date: 時間 98 :param touser: 發送給誰 99 :param content: 發送的信息內容 100 :param info: 發送執行的結果 101 :return: 102 ''' 103 msg = '%s %s %s 發送結果 - %s\n' % (date, touser, content, info) 104 with open('msg.log', 'a') as f: 105 f.write(msg) 106 107 agentid = sys.argv[1] 108 #agentid = 1 109 touser = 'xxxxxxx@qq.com' 110 toparty = '' 111 content = sys.argv[2:] 112 content = '\n'.join(content) 113 #content = '測試' 114 CropID = 'xxxxxxxxxxxxxxxxxxx' 115 Secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 116 info={'result':None,'code':None} 117 date = time.strftime('%Y-%m-%d %H:%M:%S') 118 res=WebChat(CropID,Secret) 119 res.Get_Token(info) 120 if info['code'] == 0: 121 access_token = info['result'] 122 res.Send_Msg(touser=touser, toparty=toparty, agentid=agentid, access_token=access_token, 123 content=content,info=info) 124 if info['code'] == 0: 125 content = eval(content) 126 log(date, touser, content,info) 127 else: 128 log(date, touser, content, info) 129 else: 130 log(date,touser,content,info)
其中代碼114、115行中的CropID 和 Secret對應的是第四步《設置權限管理》中圖三對應的CropID 和 Secret
代碼63行中的data數據,請參考微信接口文檔
地址:http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E