赞
踩
最近某音比较火的微信公共号消息推送教学来啦~ Python3代码编写,代码极度简单
4、创建一个推送消息的模板、点击新增,模板格式我放下面了
模板如下:
{{city.DATA}
{{temperature.DATA}}
{{info.DATA}}
{{direct.DATA}}
{{power.DATA}}
{{aqi.DATA}}
{{wenzhang.DATA}}
{{lishi.DATA}}
我这里是用的天气和历史上的今天,后面介绍这两个接口如何使用的;
如果你要新增内容、直接在这边写{{变量名字.DATA}}就行;
接下来 就是调用接口的时候了、跟着华仔向下走吧~~~
- def Access_token(): # 获取 access_token
- grant_type = 'client_credential'
- appid = "" #地址上面的ID
- secret = "" # 地址上的ID
- url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type={grant_type}&appid={appid}&secret={secret}"
- resp = requests.get(url)
- access_token = resp.json()['access_token'] # 拿到access_token
- return access_token # 返回token给函数
聚合数据地址:API服务_短信接口_热门API接口-聚合数据 自己注册个账户去申请一下就行;
这边就暂时不出那个教学了、接着就是把天气的API返回数据截取出来
- def Tianqi(): # 由于聚合数据上面的天气接口 每天有限制次数,每天只能用30次
- citys = "北京" # 你要查询的城市
- key = "你自己聚合数据天气API的key" # 天气的key
- tian_url = f"http://apis.juhe.cn/simpleWeather/query?city={citys}&key={key}" # 获取天气接口
- reason = requests.get(url=tian_url)
- result = reason.json()
- # 下面这些就是把数据取出来、后面要用
- city = result['result']['city']
- temperature = result['result']['realtime']['temperature']
- info = result['result']['realtime']['info']
- direct = result['result']['realtime']['direct']
- power = result['result']['realtime']['power']
- aqi = result['result']['realtime']['aqi']
- return city, temperature, info, direct, power, aqi
- def Lishi():
- key = "你自己的聚合数据上面的KEY"
- date = "8/24" # 日期 格式为 月/日 个人时间问题 暂时没有弄成自动获取
- lishi_url = f"http://v.juhe.cn/todayOnhistory/queryEvent.php?key={key}&date={date}"
- lishi_resp = requests.get(url=lishi_url)
- result1 = lishi_resp.json()['result'][-1]
- date = result1['date']
- title = result1['title']
- return date, title
需要用到你在第一步 第四步骤添加的模板ID了、
- def Public():
- post_url = f"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={Access_token()}"
- datas = {
- "touser": "oKRps5hFjuGIN3gaODF6ULii1RM4", # 微信号ID
- "template_id": "Rr6HogiUS5eE9b4w6MMkKrmoTy5YYRjEjR54jbjVu2s", # 模板ID
- "data": {
- "date": {"value": f"{Time()}", "color": "#FF00FF"},
- "city":{"value": f"当前城市:{Tianqi()[0]}"},
- "temperature": {"value": f"今日气温:{Tianqi()[1]}℃"},
- "info": {"value": f"天气情况:{Tianqi()[2]}"},
- "direct": {"value": f"今日风向:{Tianqi()[3]}"},
- "power": {"value": f"风力指数:{Tianqi()[4]}"},
- "aqi":{"value": f"空气指数:{Tianqi()[5]}"},
- "describe": {"value": "\n今天是你单身狗的第N+天!!!\n", "color": "#DC143C"},
- "wenzhang": {"value": "\n\n我高喊着无爱者自由 却也期待这万盏灯火其中一盏是为我而亮的~\n", "color": "#FF1493"}
- # "lishi": {"value": f"历史上的今天:{Lishi()[0]}:'{Lishi()[1]}'", "color": "#483D8B"}
-
- }
- }
- public = requests.post(url=post_url, headers=headers, data=json.dumps(datas))
- print(public.json())
- import json
-
- import requests
-
- headers = {"Content-Type": "application/json",
- "Connection": "keep-alive",
- "User-Agent": "PostmanRuntime/7.29.0"}
-
- def Access_token(): # 获取 access_token
- grant_type = 'client_credential'
- appid = "" #地址上面的ID
- secret = "" # 地址上的ID
- url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type={grant_type}&appid={appid}&secret={secret}"
- resp = requests.get(url)
- access_token = resp.json()['access_token'] # 拿到access_token
- return access_token
-
- def Tianqi(): # 由于聚合数据上面的天气接口 每天有限制次数,每天只能用30次
- citys = "北京"
- key = "" # 天气的key
- tian_url = f"http://apis.juhe.cn/simpleWeather/query?city={citys}&key={key}" # 获取天气接口
- reason = requests.get(url=tian_url)
- result = reason.json()
- city = result['result']['city']
- temperature = result['result']['realtime']['temperature']
- info = result['result']['realtime']['info']
- direct = result['result']['realtime']['direct']
- power = result['result']['realtime']['power']
- aqi = result['result']['realtime']['aqi']
- return city, temperature, info, direct, power, aqi
-
- def Lishi():
- key = ""
- date = "8/24" # 日期 格式为 月/日 个人时间问题 暂时没有弄成自动获取
- lishi_url = f"http://v.juhe.cn/todayOnhistory/queryEvent.php?key={key}&date={date}"
- lishi_resp = requests.get(url=lishi_url)
- result1 = lishi_resp.json()['result'][-1]
- date = result1['date']
- title = result1['title']
- return date, title
-
-
- def Public():
- post_url = f"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={Access_token()}"
- datas = {
- "touser": "", # 微信号ID
- "template_id": "", # 模板ID
- "data": {
- "date": {"value": f"{Time()}", "color": "#FF00FF"},
- "city":{"value": f"当前城市:{Tianqi()[0]}"},
- "temperature": {"value": f"今日气温:{Tianqi()[1]}℃"},
- "info": {"value": f"天气情况:{Tianqi()[2]}"},
- "direct": {"value": f"今日风向:{Tianqi()[3]}"},
- "power": {"value": f"风力指数:{Tianqi()[4]}"},
- "aqi":{"value": f"空气指数:{Tianqi()[5]}"},
- "describe": {"value": "\n今天是你单身狗的第N+天!!!\n", "color": "#DC143C"},
- "wenzhang": {"value": "\n\n我高喊着无爱者自由 却也期待这万盏灯火其中一盏是为我而亮的~\n", "color": "#FF1493"}
- # "lishi": {"value": f"历史上的今天:{Lishi()[0]}:'{Lishi()[1]}'", "color": "#483D8B"}
-
- }
- }
- public = requests.post(url=post_url, headers=headers, data=json.dumps(datas))
- print(public.json())
-
-
- Public()
下面是效果图、如果你想加入星座,找个API接口,取出来数据在放入到调用消息推送的接口参数里就行了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。