forked from Astrian/IFS-RSVP-Bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
250 lines (225 loc) · 8.65 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
// 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')
// Generate express app, and get the port number
const expressApp = express()
const PORT = process.env.PORT || 5000
// Initial Telegram bot
const telegrafbot = new Telegraf(process.env.TELEGRAM_TOKEN)
const telegrambot = new Telegram(process.env.TELEGRAM_TOKEN, {
agent: null,
webhookReply: true
})
expressApp.use(telegrafbot.webhookCallback(process.env.RANDOM_ADDRESS))
telegrafbot.telegram.setWebhook(process.env.DOMAIN + process.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} PoC。\n/checkin - 进行签到\n/checkout - 进行签退`, {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"})
}
})
// 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,500\``,
{
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,500\``,
{
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
telegrafbot.hears(new RegExp(/\d{1,2},\d{1,}/), async (ctx) => {
try {
let info = await API.checkpoc(ctx.message.from.id)
await API.logaplevel(info.faction, info.location, (ctx.message.text.split(','))[0], (ctx.message.text.split(','))[1], 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}!`)
})