第一步:获得token
def get_token():
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid': '×××××××××××', 'corpsecret': '××××××××××××'}
r = requests.post(url, params=values)
data = json.loads(r.text)
return data["access_token"]
第二步:发送信息
def send_msg(msg):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+get_token()
values = {"touser": "×××", "toparty": "××", "msgtype": "text", "agentid": "×××", "text": {"content": msg}, "safe": 0}
data = json.dumps(values)
response = requests.post(url, data=data)
errcode = json.loads(response.text)['errcode']
if errcode == 0:
print('Succesfully')
else:
print('Failed')
注意:×××部分需要根据企业微信获得
"touser":"*******", #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
"toparty":"2", #企业号中的部门id。
"msgtype":"text", #消息类型。
"agentid":"*******", #企业号中的应用id。
企业微信登录管理员后台的页面
点击 "我的企业" 获取企业 ID (等一下代码中会用到)
点击 "应用与小程序" 创建应用 (报警信息将发送到应用中)
根据要求填写应用信息创建应用
获取 Agentid 和 Secret (等一下代码中会用到)