-
Notifications
You must be signed in to change notification settings - Fork 13
/
template.go
221 lines (195 loc) · 5.53 KB
/
template.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"strings"
"github.com/robfig/cron"
)
var (
APPID = "*********"
APPSECRET = "**************"
SentTemplateID = "0U-LzrPOkwGsg0GwCEcNuEfmotI-JYsX60WqO0tIJdo" //每日一句的模板ID,替换成自己的
WeatTemplateID = "YBF_WVK2Bbq2ChkDa4PGecZ8UnQsqugqbfolZedP-Sc" //天气模板ID,替换成自己的
WeatherVersion = "v1"
)
type token struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
type sentence struct {
Content string `json:"content"`
Note string `json:"note"`
Translation string `json:"translation"`
}
func main() {
spec := "0 0 12 * * *" // 每天12:00
spec1 := "0 0 7 * * *" // 每天早晨7:00
c := cron.New()
c.AddFunc(spec, everydaysen)
c.AddFunc(spec1, weather)
c.Start()
fmt.Println("开启定时任务")
select {}
//weather()
//everydaysen()
}
//发送每日一句
func everydaysen() {
req, fxurl := getsen()
if req.Content == "" {
return
}
access_token := getaccesstoken()
if access_token == "" {
return
}
flist := getflist(access_token)
if flist == nil {
return
}
reqdata := "{\"content\":{\"value\":\"" + req.Content + "\", \"color\":\"#0000CD\"}, \"note\":{\"value\":\"" + req.Note + "\"}, \"translation\":{\"value\":\"" + req.Translation + "\"}}"
for _, v := range flist {
templatepost(access_token, reqdata, fxurl, SentTemplateID, v.Str)
}
}
//发送天气预报
func weather() {
access_token := getaccesstoken()
if access_token == "" {
return
}
flist := getflist(access_token)
if flist == nil {
return
}
var city string
for _, v := range flist {
switch v.Str {
case "oeZ6P5kyGsLKn3sIGRVfpb8oT4mg":
city = "青岛"
go sendweather(access_token, city, v.Str)
case "oeZ6P5jvFNh2y_h_2UcaoTXBaC2o":
city = "西安"
go sendweather(access_token, city, v.Str)
default:
}
}
fmt.Println("weather is ok")
}
//获取微信accesstoken
func getaccesstoken() string {
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%v&secret=%v", APPID, APPSECRET)
resp, err := http.Get(url)
if err != nil {
fmt.Println("获取微信token失败", err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("微信token读取失败", err)
return ""
}
token := token{}
err = json.Unmarshal(body, &token)
if err != nil {
fmt.Println("微信token解析json失败", err)
return ""
}
return token.AccessToken
}
//获取每日一句
func getsen() (sentence, string) {
resp, err := http.Get("http://open.iciba.com/dsapi/?date")
sent := sentence{}
if err != nil {
fmt.Println("获取每日一句失败", err)
return sent, ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("读取内容失败", err)
return sent, ""
}
err = json.Unmarshal(body, &sent)
if err != nil {
fmt.Println("每日一句解析json失败")
return sent, ""
}
fenxiangurl := gjson.Get(string(body), "fenxiang_img").String()
fmt.Println(sent)
return sent, fenxiangurl
}
//获取关注者列表
func getflist(access_token string) []gjson.Result {
url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token + "&next_openid="
resp, err := http.Get(url)
if err != nil {
fmt.Println("获取关注列表失败", err)
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("读取内容失败", err)
return nil
}
flist := gjson.Get(string(body), "data.openid").Array()
return flist
}
//发送模板消息
func templatepost(access_token string, reqdata string, fxurl string, templateid string, openid string) {
url := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token
reqbody := "{\"touser\":\"" + openid + "\", \"template_id\":\"" + templateid + "\", \"url\":\"" + fxurl + "\", \"data\": " + reqdata + "}"
resp, err := http.Post(url,
"application/x-www-form-urlencoded",
strings.NewReader(string(reqbody)))
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
//获取天气
func getweather(city string) (string, string, string, string) {
url := fmt.Sprintf("https://www.tianqiapi.com/api?version=%s&city=%s", WeatherVersion, city)
resp, err := http.Get(url)
if err != nil {
fmt.Println("获取天气失败", err)
return "", "", "", ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("读取内容失败", err)
return "", "", "", ""
}
data := gjson.Get(string(body), "data").Array()
thisday := data[0].String()
day := gjson.Get(thisday, "day").Str
wea := gjson.Get(thisday, "wea").Str
tem := gjson.Get(thisday, "tem").Str
//tem2 := gjson.Get(thisday, "tem2").Str
air_tips := gjson.Get(thisday, "air_tips").Str
return day, wea, tem, air_tips
}
//发送天气
func sendweather(access_token, city, openid string) {
day, wea, tem, air_tips := getweather(city)
if day == "" || wea == "" || tem == ""|| air_tips == "" {
return
}
reqdata := "{\"city\":{\"value\":\"城市:" + city + "\", \"color\":\"#0000CD\"}, \"day\":{\"value\":\"" + day + "\"}, \"wea\":{\"value\":\"天气:" + wea + "\"}, \"tem1\":{\"value\":\"平均温度:" + tem + "\"}, \"air_tips\":{\"value\":\"tips:" + air_tips + "\"}}"
//fmt.Println(reqdata)
templatepost(access_token, reqdata, "http://47.105.41.130:9999", WeatTemplateID, openid)
}