概述
环境准备:
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代码(修改代码中固定参数后可用):
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发送卡片所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复