1、获取企业微信信息
1)我的企业 --> 企业信息 --> 企业ID
2)创建一个应用,记录该应用的 AgentId、Secret
2、python代码
#!/usr/bin/env python
# -*- coding:utf- -*- import json
import requests class WeChat:
def __init__(self):
self.CORPID = '*******' //企业ID
self.CORPSECRET = '*******' //自建应用的Secret
self.AGENTID = '*****' //自建应用的ID
self.TOUSER = "*****" //企业微信接收人 def _get_access_token(self):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid': self.CORPID,
'corpsecret': self.CORPSECRET,
}
req = requests.post(url, params=values)
return req def get_access_token(self):
get_req = self._get_access_token()
if get_req.status_code != :
print('连接服务器失败')
else:
get_req_json = json.loads(get_req.text)
if get_req_json['errcode'] != :
print('响应结果不正确')
else:
access_token = get_req_json['access_token']
return access_token def send_data(self, message):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
send_values = {
"touser": self.TOUSER,
"msgtype": "text",
"agentid": self.AGENTID,
"text": {
"content": message
},
"safe": ""
}
send_msges = (bytes(json.dumps(send_values), 'utf-8'))
respone = requests.post(send_url, send_msges)
respone = respone.json()
return respone["errmsg"] if __name__ == "__main__":
wx = WeChat()
wx.send_data("您有新的工单未处理,登入<a href=\"http://request-tracker.ic\">工单系统</a>查看。")