-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechat.py
41 lines (36 loc) · 1.2 KB
/
wechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
import requests
import json
import configparser
# 获取access_token
def get_access_token(corpid, secret):
payload = {'corpid': corpid, 'corpsecret': secret}
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
resp = requests.get(url, params=payload)
access_token = eval(resp.text)['access_token']
return access_token
# 发送文本消息
def message_send(userid, agentid, content, access_token):
msg = {
'touser': userid,
'msgtype': 'text',
'agentid': agentid,
'text': {
'content': content
},
'safe': 0
}
payload = {'access_token': access_token}
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send'
resp = requests.post(url, params=payload, data=json.dumps(msg))
return resp.text
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read('config.ini')
CorpID = config['wechat']['CorpID']
Secret = config['wechat']['Secret']
ACCESS_TOKEN = get_access_token(CorpID, Secret)
UserID = config['wechat']['UserID']
AgentId = config['wechat']['AgentId']
CONTENT = 'hello, world'
print(message_send(UserID, AgentId, CONTENT, ACCESS_TOKEN))