forked from wangxinleo/wechat-public-account-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
133 lines (118 loc) · 4.96 KB
/
main.js
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
import dayjs from 'dayjs'
import { selfDayjs, timeZone } from './src/utils/set-def-dayjs.js'
import {
getAccessToken,
getWeather,
getCIBA,
getOneTalk,
getEarthyLoveWords,
getPoisonChickenSoup,
getMomentCopyrighting,
getBirthdayMessage,
sendMessageReply,
getDateDiffList,
getSlotList
} from './src/services/index.js'
import { config } from './config/index.js'
import { toLowerLine, getColor } from './src/utils/index.js'
const getAggregatedData = async () => {
const weekList = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
// 获取金山词霸每日一句
const { content: noteEn, note: noteCh } = await getCIBA()
// 获取每日一言
const { hitokoto: oneTalk, from: talkFrom } = await getOneTalk(config.LITERARY_PREFERENCE)
// 获取土味情话
const earthyLoveWords = await getEarthyLoveWords()
// 获取朋友圈文案
const momentCopyrighting = await getMomentCopyrighting()
// 获取毒鸡汤
const poisonChickenSoup = await getPoisonChickenSoup()
// 统计日列表计算日期差
const dateDiffParams = getDateDiffList().map(item => {
return { name: item.keyword, value: item.diffDay, color: getColor() }
})
// 获取插槽中的数据
const slotParams = getSlotList().map(item => {
return { name: item.keyword, value: item.checkout, color: getColor() }
})
// 获取生日信息
const birthdayMessage = getBirthdayMessage()
const users = config.USERS
for (const user of users) {
// 获取每日天气
const {
// 天气
weather,
// 最高温度
temp: maxTemperature,
// 最低温度
tempn: minTemperature,
// 风向
wd: windDirection,
// 风力等级
ws: windScale
} = await getWeather(user.province || config.PROVINCE, user.city || config.CITY)
// 集成所需信息
const wxTemplateParams = [
{ name: toLowerLine('toName'), value: user.name, color: getColor() },
{ name: toLowerLine('date'), value: `${selfDayjs().format('YYYY-MM-DD')} ${weekList[selfDayjs().format('d')]}`, color: getColor() },
{ name: toLowerLine('province'), value: user.province || config.PROVINCE, color: getColor() },
{ name: toLowerLine('city'), value: user.city || config.CITY, color: getColor() },
{ name: toLowerLine('weather'), value: weather, color: getColor() },
{ name: toLowerLine('minTemperature'), value: minTemperature, color: getColor() },
{ name: toLowerLine('maxTemperature'), value: maxTemperature, color: getColor() },
{ name: toLowerLine('windDirection'), value: windDirection, color: getColor() },
{ name: toLowerLine('windScale'), value: windScale, color: getColor() },
{ name: toLowerLine('birthdayMessage'), value: birthdayMessage, color: getColor() },
{ name: toLowerLine('noteEn'), value: noteEn, color: getColor() },
{ name: toLowerLine('noteCh'), value: noteCh, color: getColor() },
{ name: toLowerLine('oneTalk'), value: oneTalk, color: getColor() },
{ name: toLowerLine('talkFrom'), value: talkFrom, color: getColor() },
{ name: toLowerLine('earthyLoveWords'), value: earthyLoveWords, color: getColor() },
{ name: toLowerLine('momentCopyrighting'), value: momentCopyrighting, color: getColor() },
{ name: toLowerLine('poisonChickenSoup'), value: poisonChickenSoup, color: getColor() },
].concat(dateDiffParams.concat(slotParams))
user['wxTemplateParams'] = wxTemplateParams
}
return users
}
const getCallbackTemplateParams = (messageReply) => {
const postTimeZone = timeZone()
const postTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
return [
{ name: toLowerLine('postTimeZone'), value: postTimeZone, color: getColor() },
{ name: toLowerLine('postTime'), value: postTime, color: getColor() },
{ name: toLowerLine('needPostNum'), value: messageReply.needPostNum, color: getColor() },
{ name: toLowerLine('successPostNum'), value: messageReply.successPostNum, color: getColor() },
{ name: toLowerLine('failPostNum'), value: messageReply.failPostNum, color: getColor() },
{ name: toLowerLine('successPostIds'), value: messageReply.successPostIds, color: getColor() },
{ name: toLowerLine('failPostIds'), value: messageReply.failPostIds, color: getColor() },
]
}
const main = async () => {
// 获取accessToken
const accessToken = await getAccessToken()
// 处理好的用户数据
const aggregatedData = await getAggregatedData()
// 公众号推送消息
const {
needPostNum,
successPostNum,
failPostNum,
successPostIds,
failPostIds
} = await sendMessageReply(aggregatedData, accessToken)
// 获取回执信息
const callbackTemplateParams = getCallbackTemplateParams({
needPostNum,
successPostNum,
failPostNum,
successPostIds,
failPostIds
})
// 发送回执
if (config.CALLBACK_TEMPLATE_ID) {
await sendMessageReply(config.CALLBACK_USERS, accessToken, config.CALLBACK_TEMPLATE_ID, callbackTemplateParams)
}
}
main()