zabbix micro-channel to send a message script

cat /usr/local/zabbix/share/zabbix/alertscripts/sed_messages_weixin.py  python2.x
#
!/usr/bin/env python # _*_ coding: utf-8 _*_ # author:chenjianwen # email:[email protected] import sys import urllib2 import json import requests corpid = 'xxxx' #企业微信号id corpsecret = 'xxxx'  ##应用secret get_access_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'%(corpid,corpsecret) def get_access_token(get_access_token_url): data = urllib2.urlopen(get_access_token_url).read().decode('utf-8') data = json.loads(data) #print data["access_token"] return data["access_token"] def send_messages(touser,access_token,content): send_messages_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'%access_token data = { "touser" : touser, #Member ID list (message recipient, multiple recipients with '|' separated, supports up to 1000). Special case: designated as @all, all members of the enterprise application is sent to the " toparty " : " 4 " , # # department ID list with multiple recipients '|' separated, supports up to 100. This parameter is ignored when touser is @all " totag " : "" , # tag ID list, with multiple recipients '|' separated, supports up to 100. When touser ignore this parameter is @all " MsgType " : " text " , # type of message, this time is fixed at: text " agentid " : 1000004, # the above mentioned id enterprise applications, integer. Can be applied settings page to view the " text " : { ": Content # message content, a maximum of 2,048 bytes }, " Safe " : 0 # indicates whether the message is confidential, no represents 0, 1, the default 0 } value = json.dumps (Data) # # Conversion str format for the request = requests.post (send_messages_url, data = value) # #post request data request_result request.status_code = # # acquisition request code IF request_result == 200 is : Print " [% s] message has been sent successfully " % Content the else : Print " [% s] message transmission failure " %content if __name__ == '__main__': access_token = get_access_token(get_access_token_url) send_messages(sys.argv[1],access_token,sys.argv[3])

 

Guess you like

Origin www.cnblogs.com/chenjw-note/p/10936411.html