-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
telegram.js
304 lines (282 loc) · 13.2 KB
/
telegram.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
const fs = require('fs'),
path = require('path'),
express = require('express'),
router = express.Router(),
_ = require('underscore'),
config = require('../../config/config'),
utils = require('../../utils'),
morgan = require('./../../config/middlewares/morgan')(),
logger = require('./../../config/log')(),
auth = require('./../../config/middlewares/auth'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
Image = mongoose.model('Image'),
tgutils = require('./utils/telegram'),
addLink = require('./utils/collection').addLink,
fetchCollections = require('./utils/collection').fetchCollections,
addShortlink = require('./utils/shortlink').addShortlink
const otps = {}
const pendingLinks = {} // by user
const CMD_START = 'CMD_START',
CMD_LOGOUT = 'CMD_LOGOUT',
CMD_WHOAMI = 'CMD_WHOAMI',
CMD_SHORTEN = 'CMD_SHORTEN',
CMD_ADD_LINK = 'CMD_ADD_LINK',
CMD_CHOOSE_COLLECTION = 'CMD_CHOOSE_COLLECTION',
CMD_UPLOAD_PHOTO = 'CMD_UPLOAD_PHOTO',
CMD_SET_DEFAULT = 'CMD_SET_DEFAULT',
CMD_CLEAR_DEFAULT = 'CMD_CLEAR_DEFAULT',
CMD_HELP = 'CMD_HELP'
const commandMatchers = {
[CMD_START]: function (m) {
const args = m.text.match(/^\/start\s(\d+)$/)
return args ? args.slice(1) : null
},
[CMD_LOGOUT]: function (m) {
const args = m.text.match(/^\/logout$/)
return args ? args.slice(1) : null
},
[CMD_WHOAMI]: function (m) {
const args = m.text.match(/^\/whoami$/)
return args ? args.slice(1) : null
},
[CMD_SHORTEN]: function (m) {
const args = m.text.match(/^\/shorten\s(.+)$/)
return args ? args.slice(1) : null
},
[CMD_ADD_LINK]: function (m) {
// "/add https://anchr.io Anchr" or
// "/add https://anchr.io" or
// "https://anchr.io Anchr" or
// "https://anchr.io" or
// "lorem ipsum https://anchr.io Anchr"
const args = m.text.match(/(?:^\/add\s)?(https?:\/\/[^\s]+)(?:\s(.+))?/)
return args && utils.isURL(args[0]) ? args.slice(1) : null
},
[CMD_CHOOSE_COLLECTION]: function (m) {
const args = m.text.match(/^(\d+)$/)
return args ? args.slice(1) : null
},
[CMD_SET_DEFAULT]: function (m) {
const args = m.text.match(/^\/setdefault\s(\d+)$/)
return args ? args.slice(1) : null
},
[CMD_CLEAR_DEFAULT]: function (m) {
const args = m.text.match(/^\/cleardefault$/)
return args ? args.slice(1) : null
},
[CMD_UPLOAD_PHOTO]: function (m) {
return m.photo ? [] : null
},
[CMD_HELP]: function (m) {
const args = m.text.match(/^\/(?:start|help)$/)
return args ? args.slice(1) : null
}
}
function unauthenticatedHandler(rawMessage) {
return function () {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '🤔 You do not seem to be authenticated, yet.' })
}
}
const commandProcessors = {
[CMD_START]: function (args, rawMessage) {
const otp = args[0]
if (!otps[otp]) return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Sorry, the code you just entered is invalid.' })
return User.updateOne({ _id: otps[otp]._id }, { telegramUserId: rawMessage.chat.id.toString() })
.then(() => {
delete otps[otp]
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '✅ Successfully authenticated!' })
})
},
[CMD_WHOAMI]: function (args, rawMessage) {
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: `👤 You are ${user.getEmail()}` })
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_LOGOUT]: function (args, rawMessage) {
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
return User.updateOne({ _id: user._id }, { $unset: { telegramUserId: '' } })
})
.then(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '👋 Logged out successfully.' })
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_SHORTEN]: function (args, rawMessage) {
if (!utils.isURL(args[0])) {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Not a valid URL.' })
}
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
addShortlink(args[0], user)
.then(({ data }) => {
const link = `${config.publicShortlinkUrl}/${data._id}`
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: `✅ Successfully added shortlink: ${link}`, disable_web_page_preview: true })
})
.catch(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to shorten link, sorry.' })
})
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_ADD_LINK]: function (args, rawMessage) {
if (!utils.isURL(args[0])) {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Not a valid URL.' })
}
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
pendingLinks[user._id] = {
url: args[0],
description: args.length > 1 ? args[1] : '',
date: Date.now()
}
if (user.telegramDefaultCollId) {
commandProcessors[CMD_CHOOSE_COLLECTION]([user.telegramDefaultCollId], rawMessage)
} else {
fetchCollections(user)
.then(({ data }) => {
// this is not robust to the collections changing while the user does their choice
let text = '👇 Choose a collection to save the link to by typing its number:\n\n'
text += data
.map((c, i) => {
return `**${i + 1}.** ${c.name}`
})
.join('\n')
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: text, parse_mode: 'Markdown' })
})
.catch(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to add link, sorry.' })
})
}
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_CHOOSE_COLLECTION]: function (args, rawMessage) {
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
if (!pendingLinks[user._id]) return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '🙅♂️ No pending link to add to this collection.' })
const link = pendingLinks[user._id]
const index = parseInt(args[0]) - 1
fetchCollections(user)
.then(({ data }) => {
// this is not robust to the collections changing while the user does their choice
if (index < 0 || index >= data.length) return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Invalid collection.' })
addLink(link, data[index]._id, user)
.then(() => {
delete pendingLinks[user._id]
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '✅ Link added successfully.' })
})
.catch(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to add link.' })
})
})
.catch(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to add link, sorry.' })
})
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_SET_DEFAULT]: function (args, rawMessage) {
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
User.updateOne({ _id: user._id }, { telegramDefaultCollId: parseInt(args[0]) })
.then(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '✅ Set default collection.' })
})
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_CLEAR_DEFAULT]: function (args, rawMessage) {
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
User.updateOne({ _id: user._id }, { $unset: { telegramDefaultCollId: '' } })
.then(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '✅ Unset default collection.' })
})
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_UPLOAD_PHOTO]: function (args, rawMessage) {
return tgutils.resolveUser(rawMessage.from)
.then((user) => {
const fileId = rawMessage.photo[rawMessage.photo.length - 1].file_id
tgutils.downloadFile(fileId)
.then(({ file, stream }) => {
const targetName = utils.generateUUID() + path.parse(file.file_path).ext
const targetPath = config.uploadDir + targetName
stream.pipe(fs.createWriteStream(targetPath))
const image = new Image({
_id: targetName,
created: Date.now(),
source: 'telegram',
encrypted: false,
type: utils.guessMimeType(targetName),
createdBy: user._id
})
image.save((err, obj) => {
if (err) throw err
const link = `${config.publicImageUrl}/${obj._id}`
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: `✅ Successfully uploaded image: ${link}`, disable_web_page_preview: true })
})
})
.catch(() => {
return tgutils.doRequest('sendMessage', { chat_id: rawMessage.chat.id, text: '❌ Failed to upload photo, sorry.' })
})
})
.catch(unauthenticatedHandler(rawMessage))
},
[CMD_HELP]: function (args, rawMessage) {
return tgutils.doRequest('sendMessage', {
chat_id: rawMessage.chat.id,
text: '👋 Welcome! To get started, browse to https://anchr.io, go to Settings and get your token. Then, type `/start <YOUR TOKEN>` to authenticate.',
parse_mode: 'Markdown',
disable_web_page_preview: true
})
},
}
function parseCommand(message) {
for (const key in commandMatchers) {
const result = commandMatchers[key](message)
if (result !== null) return [key, result]
}
throw 'failed to parse command'
}
module.exports = function (app, passport) {
if (!config.telegram.botToken) return
if (!config.telegram.urlSecret) logger.default('[WARN] It is highly recommendable to specify a ANCHR_TELEGRAM_URL_SECRET, see https://core.telegram.org/bots/api#setwebhook. This can be any random string.')
app.use('/api/telegram', router)
router.use(morgan)
const updatesPath = `/updates/${config.telegram.urlSecret}`
logger.default(`Listening for Telegram updates at /api/telegram${updatesPath}`)
router.post(updatesPath, (req, res) => {
if (!req.body) return res.status(400).end()
const message = req.body.message
message.text = message.text ? message.text.trim() : ''
try {
const parseResult = parseCommand(message)
const command = parseResult[0]
const args = parseResult[1]
commandProcessors[command](args, message)
.catch((e) => {
logger.error(`Failed to process Telegram command "${command} - `, e)
})
} catch (e) {
tgutils.doRequest('sendMessage', { chat_id: message.chat.id, text: '🤷♂️ Sorry, I did not understand you' })
}
res.status(204).end()
})
router.get('/otp', auth(passport), (req, res) => {
if (!req.user) return res.makeError(401, 'Unauthorized')
const otp = _.range(0, 6)
.map(() => {
return _.random(0, 9)
})
.join('')
otps[otp] = req.user
res.setHeader('Content-Type', 'text/plain')
res.send(otp)
})
}