准备工作

  • 微信公众平台接口测试帐号申请
  • 自动推送可以写本地定时(不过要一直挂着),服务器(大材小用,没必要),云函数(我在使用,因为我写了很多,所以顺便挂上去了),GitHub actions(之前文章提到过)
  • 百度地图开发者,用于天气api
  • 一双勤劳的双手

具体流程

获取信息

复制:appID和appsecret
image.png

新建模板消息

标题自拟

模板内容:

    现在是{{nowtime.DATA}} 
    你所在城市:{{dizhi.DATA}} 
    天气:{{ weather.DATA}} 
    最高温度:{{hw.DATA }} 
    最低温度:{{ dw.DATA}} 
    今天是我们恋爱的第{{ date.DATA}}天! 
    {{yiyan.DATA}}

导入云函数

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
# -*- coding: utf8 -*-
import requests
import json
import time
import csv
import linecache
import random

#填写所在城市
city = ''
#模板id
models = 'qpeaRHwuXys96O3WE2zysh4AWwn-i4DGb0BIOS79NBw'


填写上面复制的
######################
appid = ''
secret = ''
######################



填写用户id
######################
user = ' '
######################
#我
#user = ''




csv_file = csv.reader(open('city.csv', 'r',encoding='UTF-8') )
def yys():
for i in range(1,2):#for循环几次
a = random.randrange(1, 9) #1-9中生成随机数
theline = linecache.getline(r'poem.txt', a)
print (theline)
return theline

def read_city(my_city):
for line in csv_file:
if line[5] == my_city:
return line[4]


def nowtime():
now = time.strftime('%Y年%m月%d日 %H:%M:%S', time.localtime(time.time()+28800))
return now

# 计算日期差


def date_diff(date1, date2):
date1 = time.strptime(date1, "%Y-%m-%d")
date2 = time.strptime(date2, "%Y-%m-%d")
date1 = time.mktime(date1)
date2 = time.mktime(date2)
return int((date1 - date2) / 86400)


def get_token():
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + \
appid+'&secret='+secret
res = requests.get(url)
token = res.json()['access_token']
return token


def send(weather, hw, dw, date):
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+get_token()
data = {
"touser": user,
"template_id": models,
"url": "http://blog.jhx.asia",
"topcolor": "#FF0000",
"data": {
"weather": {
"value": weather,
"color": "#000000"
},
"date": {
"value": date,
"color": "#FFC0CB"
},
"hw": {
"value": hw,
"color": "#B22222"
},
"nowtime": {
"value": nowtime(),
"color": "#173177"
},
"dizhi": {
"value": city,
"color": "#7B68EE"
},
"yiyan": {
"value": yys(),
"color": "#FA8072"
},
"dw": {
"value": dw,
"color": "#1E90FF"
}
}
}

res = requests.post(url, data=json.dumps(data))
print(res.text)


def main():
citys = read_city(city)
url = 'https://api.map.baidu.com/weather/v1/?district_id=' +citys+'&data_type=all&output=json&ak='###您的ak
res = requests.get(url)
weather = res.json()['result']['now']['text']
hw = res.json()['result']['forecasts'][0]['high']
dw = res.json()['result']['forecasts'][0]['low']
date = date_diff(time.strftime('%Y-%m-%d', time.localtime(time.time()+28800)), '2022-06-29')
send(weather, hw, dw, date)


# 云函数自带,懒得改了
def main_handler(event, context):
print("Received event: " + json.dumps(event, indent = 2))
print("Received context: " + str(context))
print("Hello world")
return("Hello World")


main()





本文只做简要介绍,如有疑问,请联系我,缺少文件,一个是城市代码表(百度地图开发者那边可以下载,改名city.csv,另一个是一言,每行为一篇,可以自行编写)