我是靠谱客的博主 苗条香菇,这篇文章主要介绍微信测试公众号使用Python发送卡片,现在分享给大家,希望可以做个参考。

 环境准备:

1、天气接口:https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=太原

2、话术接口(“token=”此token可通过此url(ALAPI)进入页面( 接口管理 ---> 更新token密钥)进行注册账号后拿取):https://v2.alapi.cn/api/qinghua?token=xxxxxxx 

3、微信公众平台url:微信公众平台

开始操作:

1、登陆微信测试公众号:

 2、可从此公众号中获取 appID和appsecret(代码中会用到): 

3、扫描测试二维码(发送卡片的微信扫描,扫描结果在右侧,微信id代码中template中会用到。模版参考:微信公众平台

4、python代码(修改代码中固定参数后可用):

复制代码
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json import requests import datetime import time """ 模板(微信测试公众号中添加模板): {{Zdy.DATA}} 城市: {{City.DATA}} 日期: {{Date.DATA}} {{Week.DATA}} 天气: {{Weather.DATA}} 风速:{{Narrative.DATA}} 温度:{{Wendu.DATA}} 距离xx下次生日还有: {{Brithdaybb.DATA}} 天 距离xx下次生日还有: {{Brithdayfj.DATA}} 天 我们已经恋爱{{Lianaidate.DATA}} 天了 {{Qinghua.DATA}} """ appId = 'xxxxxxxxxxxx' #对应自己的appId,微信公众平台上的appId appSecret = 'xxxxxxxxxxxxxx' #对应自己的appSecret,微信公众平台上的appSecret #wxtoken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret wxtoken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % (appId, appSecret) #print(wxtoken) wxtokenResponse = requests.get(wxtoken) wxtokenData1 = wxtokenResponse.json() #print(wxtokenData1['access_token']) wxtokenUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s" %wxtokenData1['access_token'] #print(wxtokenUrl) #wxtokenUrlResponse = requests.post(wxtokenUrl, json=data) weather = 'https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city=太原' weatherResponse1 = requests.get(weather) weatherData1 = weatherResponse1.json() #print(weatherData1) #print(weatherData1['city']) #print(weatherData1['cityEn']) #print(weatherData1['data'][0]['week']) def love(): love = 'https://v2.alapi.cn/api/qinghua?token=xxxxxxx' #步骤二中方法:修改为自己的token love_response1 = requests.get(love) love_data1 = love_response1.json() #print(data2) # print(love_data1['data']['content']) print(love_data1) return love_data1['data']['content'] #love() def birthdaybb(): today = datetime.date.today() birthdayStart = datetime.date(int(2023),int(8),int(11)) #下次生日 # print(birthdayStart) # print(today) birthday_end = birthdayStart - today #print(type(a)) # print(birthday_end.days) return birthday_end.days #print(birthday()) def birthdayfj(): today = datetime.date.today() birthdayStart = datetime.date(int(2022),int(9),int(23)) #下次生日 # print(birthdayStart) # print(today) birthday_end = birthdayStart - today #print(type(a)) # print(birthday_end.days) return birthday_end.days #print(birthday()) def lianai(): today = datetime.date.today() birthdayStart = datetime.date(int(2022),int(7),int(12)) #开始时间 # print(birthdayStart) # print(today) birthday_end = today - birthdayStart #print(type(a)) # print(birthday_end.days) return birthday_end.days #print(lianai()) """ def todayDays(): todaytime = datetime.date.today() return todaytime print(todayDays()) """ def today(): todayDays = datetime.date.today() #print(todayDays) todayDays1 = "%d年%d月%d日" %(todayDays.year, todayDays.month, todayDays.day) return todayDays1 #print(todayDays1) def send(user_id): #用于发送多个微信号的函数 zdy = "工欲善其事,必先利其器。"。 #可自定义,随便写或者不写 template = { # "touser":"oFm3a6baJWPwHAhGloPjc5OxMzW8", "touser": user_id, "template_id":"xxxxxxxxxxxxxxxxxxxxxxxxx", #公众平台中模板id "url":"http://weixin.qq.com/download", "topcolor":"#FF0000", "data":{ "Zdy": { "value": zdy, "color":"#FF0000" }, "City": { "value": weatherData1['city'], "color":"#173177" }, "Date":{ "value": today(), "color":"#FF8080" }, "Weather": { "value": weatherData1['data'][0]['wea'], "color":"#FF00FF" }, "Week": { "value": weatherData1['data'][0]['week'], "color":"#99CC00" }, "Qinghua": { "value": love(), "color":"#FF9933" }, "Brithdaybb":{ "value": birthdaybb(), "color":"#FF99CC" }, "Brithdayfj":{ "value": birthdayfj(), "color":"#FF99CC" }, "Narrative":{ "value": weatherData1['data'][0]['narrative'], "color":"#FF99CC" }, "Lianaidate":{ "value": lianai(), "color":"#FF99CC" }, "Wendu":{ "value": weatherData1['data'][0]['tem2']+"°C"+" ~ "+weatherData1['data'][0]['tem1']+"°C", "color":"#FF99CC" } } } url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+wxtokenData1['access_token'] url_response = requests.post(url, json=template) #print(url_response.text) #urlJsonDumps = json.dumps(url_response.text) #print(type(urlJsonDumps)) urlJsonLoads = json.loads(url_response.text) #print(urlJsonLoads["errcode"], urlJsonLoads["errmsg"]) #根据返回状态判断是否发送成功 if urlJsonLoads["errcode"] == 0 or urlJsonLoads["errmsg"] == "ok": print("发送成功") else: print("发送失败,接口错误代码为%s" % urlJsonLoads["errcode"]) """ #template() print(template['data']['City']['value']) print(template['data']['Date']['value']) print(weatherData1['data'][0]['narrative']) """ #此循环实现给多个微信id发送卡片,如果有多个微信id,添加至列表中 for user_id in ["oFm3a6baJWPwHAhGloPjc5OxMzW8","oFm3a6XUiCNPbwZbGLv_OYhLE60Q"]: send(user_id) time.sleep(3)

5、配置模板:

模板(微信测试公众号中添加模板):
{{Zdy.DATA}} 

城市: {{City.DATA}} 
日期: {{Date.DATA}}  {{Week.DATA}}
天气: {{Weather.DATA}}
风速:{{Narrative.DATA}}
温度:{{Wendu.DATA}}
距离xx下次生日还有: {{Brithdaybb.DATA}} 天
距离xx下次生日还有: {{Brithdayfj.DATA}} 天
我们已经恋爱{{Lianaidate.DATA}} 天了


{{Qinghua.DATA}}

6、设置定时任务,如每天执行一次。

方法一:通过windows设置定时任务。

方法二:通过linux设置crontab( * * * * * python3   python文件所在位置。

最后

以上就是苗条香菇最近收集整理的关于微信测试公众号使用Python发送卡片的全部内容,更多相关微信测试公众号使用Python发送卡片内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部