-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdemo05_wx_notice_03.py
42 lines (31 loc) · 1.38 KB
/
demo05_wx_notice_03.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
42
import json
import requests
# 我们可以接受定义的复杂,但是不能接受调用的复杂
# 把代码从面向函数改成面向对象
class WxTools():
def __init__(self, app_id, app_secret):
self.app_id = app_id
self.app_secret = app_secret
def get_access_token(self):
url = f'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={self.app_id}&secret={self.app_secret}'
resp = requests.get(url).json()
access_token = resp.get('access_token')
return access_token
def send_wx_customer_msg(self, opend_id, msg="有人闯入了你的家"):
url = f'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={self.get_access_token()}'
req_data = {
"touser": opend_id,
"msgtype":"text",
"text":
{
"content": msg
}
}
requests.post(url, data=json.dumps(req_data, ensure_ascii=False).encode('utf-8'))
if __name__ == "__main__":
app_id = 'wx7f23641379450a28'
app_secret = '2bfecd48e13964d00b4a1b0bf26b0acb'
# access_token = get_access_token(app_id, app_secret)
# send_wx_customer_msg(access_token, "oqtB6wXelAcohf9rasCA7VLHNk9c")
wx_tools = WxTools('wx7f23641379450a28', '2bfecd48e13964d00b4a1b0bf26b0acb')
wx_tools.send_wx_customer_msg("oqtB6wXelAcohf9rasCA7VLHNk9c")