-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
342 lines (307 loc) · 11.8 KB
/
app.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
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
// Import frameworks and packages
const Telegraf = require('telegraf')
const Telegram = require('telegraf/telegram')
const express = require('express')
const debug = require('debug')('rsvpbot:app.js')
// Import APIs
const API = require('./api')
// debug switch
const godmode = require('./godmode.json')
let env
if (godmode.godmode) env = godmode.config
else env = process.env
// Generate express app, and get the port number
const expressApp = express()
const PORT = env.PORT || 5000
// Initial Telegram bot
const telegrafbot = new Telegraf(env.TELEGRAM_TOKEN)
const telegrambot = new Telegram(env.TELEGRAM_TOKEN, {
agent: null,
webhookReply: true
})
debug(env.RANDOM_ADDRESS)
expressApp.use(telegrafbot.webhookCallback(env.RANDOM_ADDRESS))
telegrafbot.telegram.setWebhook(env.DOMAIN + env.RANDOM_ADDRESS)
// Bot commands
telegrafbot.command('start', async (ctx) => {
try {
let info = await API.checkpoc(ctx.message.from.id)
telegrambot.sendMessage(ctx.message.from.id, `欢迎你,${info.location} 场 ${info.faction} 签到人员。\n\n/importrsvp - 导入你所在阵营的特工\n/checkin - 进行签到\n/checkout - 进行签退\n/help - 寻求帮助`, {parse_mode: "Markdown"})
} catch (err) {
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
telegrafbot.command('checkin', async (ctx) => {
try {
let info = await API.checkpoc(ctx.message.from.id)
// check up poc info and ap logging status
await API.checkapstatus(info.faction, info.location, ctx.message.from.id)
// list all agents id first letter
let checkuplist = await API.checkupagents(info.faction, info.location, null, 'in', ctx.message.from.id)
telegrambot.sendMessage(
ctx.message.from.id,
'请选择签到 agent 的 ID 首字母:',
{
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: checkuplist
}
}
)
} catch (err) {
console.log('error accourd!')
console.log(err)
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
telegrafbot.command('checkout', async (ctx) => {
try {
let info = await API.checkpoc(ctx.message.from.id)
// check up poc info and ap logging status
await API.checkapstatus(info.faction, info.location, ctx.message.from.id)
// list all agents id first letter
let checkuplist = await API.checkupagents(info.faction, info.location, null, 'out', ctx.message.from.id)
telegrambot.sendMessage(
ctx.message.from.id,
'请选择签退 agent 的 ID 首字母:',
{
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: checkuplist
}
}
)
} catch (err) {
console.log('error accourd!')
console.log(err)
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
telegrafbot.command('cancelaprec', async (ctx) => {
try {
let info = await API.checkpoc(ctx.message.from.id)
// check up poc info and ap logging status
await API.cancelaprec(info.faction, info.location, ctx.message.from.id)
telegrambot.sendMessage(
ctx.message.from.id,
'已取消填充 AP/等级的状态。',
{
parse_mode: "Markdown",
}
)
} catch (err) {
console.log('error accourd!')
console.log(err)
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
telegrafbot.command('importrsvp', async (ctx) =>{
try {
let info = await API.checkpoc(ctx.message.from.id)
// check up poc info and ap logging status
await API.checkapstatus(info.faction, info.location, ctx.message.from.id)
// if user only send the '/importrsvp' command, reply the help infomation
if (ctx.message.text.slice(12) === '') {
let reply = ''
reply += '请以 `/importrsvp+空格+RSVP 玩家列表` 的形式,来导入你所管理阵营的签到/签退特工。例如:\n'
reply += '```\n'
reply += '/importrsvp sampleagent1\n'
reply += 'sampleagent2\n'
reply += 'sampleagent3\n'
reply += '```\n'
reply += '小提示:直接从 FevGames 中复制特工列表并直接粘贴,效率更高噢,但请记得在指令后添加一个空格。'
throw reply
}
// process list into array
let list = (ctx.message.text.slice(12)).split('\n')
// import info
let recinfo = await API.importrsvp(info.faction, info.location, list)
// output list result
telegrambot.sendMessage(ctx.message.from.id, recinfo, {parse_mode: "Markdown"})
} catch (err) {
console.log('error accourd!')
console.log(err)
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
telegrafbot.command('status', async (ctx) => {
try {
let info = await API.checkpoc(ctx.message.from.id)
let status = await API.status(info.faction)
telegrambot.sendMessage(ctx.message.from.id, status, {parse_mode: "Markdown"})
} catch (err) {
console.log('error accourd!')
console.log(err)
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
telegrafbot.command('help', async (ctx) =>{
try {
let info = await API.checkpoc(ctx.message.from.id)
// check up poc info and ap logging status
await API.checkapstatus(info.faction, info.location, ctx.message.from.id)
let message = '需要帮助吗?这些链接可能可以帮到你。\n'
telegrambot.sendMessage(
ctx.message.from.id,
message,
{
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: [
[{
text: '查阅帮助文档',
url: `https://github.com/Astrian/IFS-RSVP-Bot/wiki`
}],
[{
text: '订阅频道',
url: `https://t.me/ifsrsvpbot`
}],
[{
text: '加入反馈群',
url: `https://t.me/joinchat/A0P0mxHipaEeJ-4vzKgTuQ`
}]
]
}
}
)
} catch (err) {
console.log('error accourd!')
console.log(err)
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
// Inline button
telegrafbot.on('callback_query', async (ctx) => {
try {
switch ((ctx.update.callback_query.data.split(':'))[0]) {
case 'findagent4in': {
// check up poc info and ap logging status
let info = await API.checkpoc(ctx.update.callback_query.from.id)
await API.checkapstatus(info.faction, info.location, ctx.update.callback_query.from.id)
// check up agents of the ID start with the specific letter
let agentlist = await API.checkupagents(info.faction, info.location, (ctx.update.callback_query.data.split(':'))[1], 'in', ctx.update.callback_query.from.id)
console.log(agentlist)
telegrambot.editMessageText(
ctx.update.callback_query.from.id,
ctx.update.callback_query.message.message_id,
null,
`请选择签到 agent:`,
{
parse_mode: 'Markdown',
reply_markup: {
inline_keyboard: agentlist
}
}
)
break
}
case 'specificagent4in': {
// check up poc info and ap logging status
let info = await API.checkpoc(ctx.update.callback_query.from.id)
await API.checkapstatus(info.faction, info.location, ctx.update.callback_query.from.id)
// log ap status
let agentname = await API.checkstatus(info.faction, info.location, (ctx.update.callback_query.data.split(':'))[1], 'in', ctx.update.callback_query.from.id)
// edit message
telegrambot.editMessageText(
ctx.update.callback_query.from.id,
ctx.update.callback_query.message.message_id,
null,
`请在消息框中写上 agent ${(ctx.update.callback_query.data.split(':'))[1]} 目前等级、AP 和步行距离数据,以半角逗号 \`,\` 区隔。\n例如:\`16,40000000,2500\`(大部分设备点击/长按示例可复制)`,
{
parse_mode: 'Markdown'
}
)
break
}
case 'checkedin': {
telegrambot.answerCbQuery(ctx.update.callback_query.id, `⚠️ 特工 ${(ctx.update.callback_query.data.split(':'))[1]} 已经完成签到。请选择一个不同的特工。`)
break
}
case 'findagent4out': {
// check up poc info and ap logging status
let info = await API.checkpoc(ctx.update.callback_query.from.id)
await API.checkapstatus(info.faction, info.location, ctx.update.callback_query.from.id)
// check up agents of the ID start with the specific letter
let agentlist = await API.checkupagents(info.faction, info.location, (ctx.update.callback_query.data.split(':'))[1], 'out', ctx.update.callback_query.from.id)
console.log(agentlist)
telegrambot.editMessageText(
ctx.update.callback_query.from.id,
ctx.update.callback_query.message.message_id,
null,
`请选择签退 agent:`,
{
parse_mode: 'Markdown',
reply_markup: {
inline_keyboard: agentlist
}
}
)
break
}
case 'specificagent4out': {
// check up poc info and ap logging status
let info = await API.checkpoc(ctx.update.callback_query.from.id)
await API.checkapstatus(info.faction, info.location, ctx.update.callback_query.from.id)
// log ap status
let agentname = await API.checkstatus(info.faction, info.location, (ctx.update.callback_query.data.split(':'))[1], 'out', ctx.update.callback_query.from.id)
// edit message
telegrambot.editMessageText(
ctx.update.callback_query.from.id,
ctx.update.callback_query.message.message_id,
null,
`请在消息框中写上 agent ${(ctx.update.callback_query.data.split(':'))[1]} 目前等级、AP 和步行距离数据,以半角逗号 \`,\` 区隔。\n例如:\`16,40000000,2500\`(大部分设备点击/长按示例可复制)`,
{
parse_mode: 'Markdown'
}
)
break
}
case 'checkedout': {
telegrambot.answerCbQuery(ctx.update.callback_query.id, `⚠️ 特工 ${(ctx.update.callback_query.data.split(':'))[1]} 已经完成签退。请选择一个不同的特工。`)
break
}
case 'notin': {
telegrambot.answerCbQuery(ctx.update.callback_query.id, `⚠️ 特工 ${(ctx.update.callback_query.data.split(':'))[1]} 尚未进行签到,无法签退。`)
break
}
default: {
debug(ctx.update.callback_query.data)
telegrambot.editMessageText(
ctx.update.callback_query.from.id,
ctx.update.callback_query.message.message_id,
null,
`你什么也没有选择。`,
{
parse_mode: 'Markdown'
}
)
}
}
} catch(e) {
debug('error')
telegrambot.editMessageText(
ctx.update.callback_query.from.id,
ctx.update.callback_query.message.message_id,
null,
`*无法完成请求*\n${e}`,
{
parse_mode: 'Markdown'
}
)
}
})
// recive AP/Level/trekker data
telegrafbot.hears(new RegExp(/\d{1,2},\d{1,},\d{1,}/), async (ctx) => {
try {
if (parseInt(ctx.message.text.split(',')[0]) > 16 || parseInt(ctx.message.text.split(',')[0]) < 1) throw `等级错误,请尝试重新输入。`
let info = await API.checkpoc(ctx.message.from.id)
await API.logdata(info.faction, info.location, (ctx.message.text.split(','))[0], (ctx.message.text.split(','))[1], (ctx.message.text.split(','))[2], ctx.message.from.id)
telegrambot.sendMessage(ctx.message.from.id, `签到/签退已完成。`, {parse_mode: "Markdown"})
} catch (err) {
telegrambot.sendMessage(ctx.message.from.id, err, {parse_mode: "Markdown"})
}
})
// Launch App
expressApp.listen(PORT, () => {
debug(`Example app listening on port ${PORT}!`)
})