-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnotification_helper.lua
371 lines (316 loc) · 12.9 KB
/
notification_helper.lua
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
local notification_helper = {}
local sys = require("sys")
local sysplus = require("sysplus")
local constants = require("constants")
local config = require("config")
local utils = require("utils")
-- https://tutorialspots.com/lua-urlencode-and-urldecode-5528.html
local function urlencode(str)
str = string.gsub(
str,
"([^0-9a-zA-Z !'()*._~-])", -- locale independent
function(c)
return string.format ("%%%02X", string.byte(c))
end)
str = string.gsub (str, " ", "+")
return str
end
local function smtp(sender_number, content)
local smtp_config = {
host = config.notification_channel.stmp.SMTP_HOST,
port = config.notification_channel.stmp.SMTP_PORT,
username = config.notification_channel.stmp.SMTP_USERNAME,
password = config.notification_channel.stmp.SMTP_PASSWORD,
mail_from = config.notification_channel.stmp.SMTP_MAIL_FROM,
mail_to = config.notification_channel.stmp.SMTP_MAIL_TO,
tls_enable = config.notification_channel.stmp.SMTP_TLS_ENABLE,
subject_config = config.notification_channel.stmp.SMTP_MAIL_SUBJECT,
}
local result = lib_smtp.send(content,sender_number,smtp_config)
log.info("util_notify", "SMTP", result.success, result.message, result.is_retry)
if result.success then
return 200, nil, result.message
end
if result.is_retry then
return 500, nil, result.message
end
return 400, nil, result.message
end
local function bark(sender_number, content)
if not config.notification_channel.bark.enabled then
return
end
if utils.is_empty(config.notification_channel.bark.api_key) then
log.warn("notification_helper", "Bark API key为空,跳过调用Bark API")
return
end
log.info("notification_helper", "正在发送Bark通知")
local url = "https://api.day.app/"..config.notification_channel.bark.api_key
log.debug("notification_helper", "Calling Bark API: "..url)
local request_body = {
title = sender_number,
body = content,
level = "timeSensitive",
}
local code, headers, body = http.request(
"POST",
url,
{["Content-Type"] = "application/json"},
json.encode(request_body),
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "Bark API返回值不是200,HTTP状态码:"..code..",响应内容:"..(body or ""))
end
log.info("notification_helper", "Bark通知发送完成")
end
local function luatos_notification(sender_number, content)
if not config.notification_channel.luatos.enabled then
return
end
if utils.is_empty(config.notification_channel.luatos.token) then
log.warn("notification_helper", "合宙推送平台token为空,跳过调用合宙推送平台API")
return
end
log.info("notification_helper", "正在发送合宙推送平台通知")
local url = "https://push.luatos.org/"..config.notification_channel.luatos.token..".send/"..urlencode(sender_number).."/"..urlencode(content)
log.debug("notification_helper", "Calling LuatOS notification API: "..url)
local code, headers, body = http.request("GET", url, nil, nil, {ipv6=true}).wait()
if code ~= 200 then
log.warn("notification_helper", "合宙推送API返回值不是200,HTTP状态码:"..code..",响应内容:"..(body or ""))
end
log.info("notification_helper", "合宙推送平台通知发送完成")
end
local function server_chan(sender_number, content)
if not config.notification_channel.server_chan.enabled then
return
end
if utils.is_empty(config.notification_channel.server_chan.send_key) then
log.warn("notification_helper", "Server酱的SendKey为空,跳过调用Server酱API")
return
end
log.info("notification_helper", "正在发送Server酱通知")
local url = "https://sctapi.ftqq.com/"..config.notification_channel.server_chan.send_key..".send"
log.debug("notification_helper", "Calling ServerChan API: "..url)
local request_body = {
title = sender_number,
desp = content
}
local request_body_json, json_error = json.encode(request_body)
if json_error then
log.warn("notification_helper", "Server酱请求序列化失败,错误信息:"..json_error)
return
end
log.debug("notification_helper", "ServerChan request body: "..request_body_json)
local code, headers, response_body = http.request(
-- Method
"POST",
-- URL
url,
-- Headers
{["Content-Type"] = "application/json"},
request_body_json,
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "Server酱API返回值不是200,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
end
log.info("notification_helper", "Server酱通知发送完成")
end
local function ding_talk_bot(sender_number, content)
if not config.notification_channel.ding_talk.enabled then
return
end
if utils.is_empty(config.notification_channel.ding_talk.webhook_url) then
log.warn("notification_helper", "钉钉机器人webhook URL未填写,跳过调用钉钉机器人webhook")
return
elseif utils.is_empty(config.notification_channel.ding_talk.keyword) then
log.warn("notification_helper", "钉钉机器人关键词未填写,跳过调用钉钉机器人webhook")
return
end
log.info("notification_helper", "正在发送钉钉机器人通知")
local url = config.notification_channel.ding_talk.webhook_url
local keyword = config.notification_channel.ding_talk.keyword
local request_body = {
msgtype = "markdown",
markdown = {
title = keyword,
text = "收到来自 **"..sender_number.."** 的短信,内容:\n\n"..content
}
}
local code, headers, response_body = http.request(
"POST",
url,
{["Content-Type"] = "application/json"},
json.encode(request_body),
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "钉钉webhook返回值不是200,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
end
log.info("notification_helper", "钉钉机器人通知发送完成")
end
local function pushplus(sender_number, content)
if not config.notification_channel.pushplus.enabled then
return
elseif utils.is_empty(config.notification_channel.pushplus.token) then
log.warn("notification_helper", "PushPlus token未填写,跳过调用PushPlus")
return
end
log.info("notification_helper", "正在发送PushPlus通知")
local url = "http://www.pushplus.plus/send"
local request_body = {
token = config.notification_channel.pushplus.token,
title = sender_number,
content = content
}
local code, headers, response_body = http.request(
"POST",
url,
{["Content-Type"] = "application/json"},
json.encode(request_body),
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "PushPlus API返回值不是200,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
end
log.info("notification_helper", "PushPlus通知发送完成")
end
local function telegram_bot(sender_number, content)
if not config.notification_channel.telegram.enabled then
return
end
if utils.is_empty(config.notification_channel.telegram.webhook_url) then
log.warn("notification_helper", "Telegram URL未填写,跳过调用Telegram bot")
return
elseif utils.is_empty(config.notification_channel.telegram.chat_id) then
log.warn("notification_helper", "Telegram chat_id 未填写,跳过调用Telegram bot")
return
end
log.info("notification_helper", "正在发送Telegram bot通知")
local url = config.notification_channel.telegram.webhook_url
local chat_id = config.notification_channel.telegram.chat_id
local request_body = {
chat_id = chat_id,
parse_mode = "Markdown",
text = "*"..sender_number.."*\n"..content
}
local code, headers, response_body = http.request(
"POST",
url,
{["Content-Type"] = "application/json"},
json.encode(request_body),
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "telegram api返回值不是200,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
end
log.info("notification_helper", "telegram bot通知发送完成")
end
local function feishu_bot(sender_number, content)
if not config.notification_channel.feishu.enabled then
return
end
if utils.is_empty(config.notification_channel.feishu.app_id) or utils.is_empty(config.notification_channel.feishu.app_secret) then
log.warn("notification_helper", "飞书机器人app_id或app_secret未填写,跳过调用飞书机器人")
return
end
if utils.is_empty(config.notification_channel.feishu.receive_id) then
log.warn("notification_helper", "飞书机器人receive_id未填写,跳过调用飞书机器人")
return
end
local app_id = config.notification_channel.feishu.app_id
local app_secret = config.notification_channel.feishu.app_secret
local receive_id = config.notification_channel.feishu.receive_id
local token_url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
local request_body = {
app_id = app_id,
app_secret = app_secret
}
local code, headers, response_body = http.request(
"POST",
token_url,
{["Content-Type"] = "application/json"},
json.encode(request_body),
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "飞书机器人获取access_token失败,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
return
end
local data = json.decode(response_body)
local access_token = data.tenant_access_token
if utils.is_empty(access_token) then
log.warn("notification_helper", "飞书机器人获取access_token失败,响应内容:"..(response_body or ""))
return
end
log.info("notification_helper", "正在发送飞书机器人通知")
local url = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=email"
local request_body = {
msg_type = "text",
content = json.encode({
text = "收到来自 **"..sender_number.."** 的短信,内容:\n\n"..content
}),
receive_id = receive_id
}
local code, headers, response_body = http.request(
"POST",
url,
{["Content-Type"] = "application/json", ["Authorization"] = "Bearer "..access_token},
json.encode(request_body),
{ipv6=true}
).wait()
if code ~= 200 then
log.warn("notification_helper", "飞书机器人发送消息失败,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
end
log.info("notification_helper", "飞书机器人通知发送完成")
end
local function wecom_bot(sender_number, content)
if not config.notification_channel.wecom.enabled then
return
end
if utils.is_empty(config.notification_channel.wecom.url) then
log.warn("notification_helper", "企业微信机器人推送 URL 未填写,跳过调用飞书机器人")
return
end
log.info("notification_helper", "正在发送企业微信机器人通知")
local request_body = {
msgtype = "text",
text = {
content = "收到来自 "..sender_number.." 的短信,内容:\n\n"..content
}
}
local code, headers, response_body = http.request(
"POST",
config.notification_channel.wecom.url,
{["Content-Type"] = "application/json"},
json.encode(request_body)
).wait()
if code ~= 200 then
log.warn("notification_helper", "企业微信机器人发送消息失败,HTTP状态码:"..code..",响应内容:"..(response_body or ""))
end
log.info("notification_helper", "企业微信机器人通知发送完成")
end
local notification_channels = {
smtp = smtp,
bark = bark,
luatos_notification = luatos_notification,
server_chan = server_chan,
ding_talk_bot = ding_talk_bot,
pushplus = pushplus,
telegram_bot = telegram_bot,
feishu_bot = feishu_bot,
wecom_bot = wecom_bot,
}
local function call_notification_channels(sender_number, content)
for _, notification_channel in pairs(notification_channels) do
sys.taskInit(function()
notification_channel(sender_number, content)
end)
end
end
sys.subscribe(constants.air780_message_topic_new_notification_request,
function(sender_number, content)
call_notification_channels(sender_number, content)
end)
return notification_helper