""" Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Date:18-04-10 Record: #---------------------------------v1-----------------------------------# """ import urllib.request import json #-------------------------------- # 获取企业微信token #-------------------------------- def get_token(url, corpid, corpsecret): token_url = ‘%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (url, corpid, corpsecret) token = json.loads(urllib.request.urlopen(token_url).read().decode())[‘access_token‘] return token #-------------------------------- # 构建告警信息json #-------------------------------- def messages(msg): values = { "touser": ‘@all‘, "msgtype": ‘text‘, "agentid": 1000002, #修改为对应应用的agentid "text": {‘content‘: msg}, "safe": 0 } msges=(bytes(json.dumps(values), ‘utf-8‘)) return msges #-------------------------------- # 发送告警信息 #-------------------------------- def send_message(url,token, data): send_url = ‘%s/cgi-bin/message/send?access_token=%s‘ % (url,token) respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read() x = json.loads(respone.decode())[‘errcode‘] # print(x) if x == 0: print (‘Succesfully‘) else: print (‘Failed‘) ##############函数结束######################## corpid = ‘ww7dd0074bd8b006f9‘ corpsecret = ‘8gPCvguwomL0WMej8fcghxQgOY-y0LlGQsAicaDHvA8‘ url = ‘https://qyapi.weixin.qq.com‘ msg=input(‘请输入内容:>>‘) #函数调用 test_token=get_token(url, corpid, corpsecret) msg_data= messages(msg) send_message(url,test_token, msg_data)
以上为Python调用企业微信号接口进行发送微信消息的测试案例
仅供参考