-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
243 lines (163 loc) · 5.12 KB
/
bot.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
process.env.NTBA_FIX_319 = true
const TelegramBot = require('node-telegram-bot-api')
const moment = require('moment')
const data = require('./data')
const users = require('./users')
const memes = require('./memes')
const config = require('../config.json')
const strings = require('./strings.json')
let bot
//
// Bootstrap
//
const start = () => {
data.load((error) => {
if (error) {
console.error(error)
} else {
startBot();
}
})
}
//
// Telegram bot setup and bot events handling
//
const startBot = () => {
bot = new TelegramBot(config.botToken, { polling: true })
// bot.on('polling_error', () => {}) // Silence polling errors
bot.on('polling_error', (e) => { console.error(e) })
bot.on('message', onPrivateMessage)
bot.on('callback_query', onMemeVoted)
}
const onPrivateMessage = (message) => {
if(message.new_chat_member) {
processNewChatMember(message)
} else if (users.isAuthorizedUser(message.from)) {
if (message.text) {
if (message.text.startsWith('/nick')) {
processNicknameChange(message)
}
} else if (message.photo || message.document || message.video) {
processMemeForwarding(message)
}
} else if (message.text && message.text === config.privateChatSecret) {
processUserAuth(message)
}
}
const onMemeVoted = (event) => {
const meme = memes.getMemeById(+event.data)
if (meme) {
const numVotes = memes.processUserVote(meme, event.from)
// Do not update expired messages (unless an expired message has been voted)
const currentChatId = event.message.chat.id
const targetMessages = getTargetMessages(currentChatId, meme)
// Prepare all promises to edit memes votes
let promises = targetMessages.map((message) => {
const inlineKeyboard = getMemeInlineKeyboard(meme.id, numVotes)
return bot.editMessageReplyMarkup(inlineKeyboard, {
message_id: message.id,
chat_id: message.chatId
})
})
// Force all promises to be executed
promises = promises.map(p => p.catch(e => e))
// Result
Promise.all(promises).then(() => {
bot.answerCallbackQuery(event.id)
})
} else {
bot.answerCallbackQuery(event.id)
}
}
//
// Bot's logic
//
const processNewChatMember = (message) => {
const targetChat = config.targetChats.find(c => c.id === message.chat.id)
if (config.mainChannelInfo && targetChat && targetChat.inviteToMainChannel) {
sendTextMessage(message, strings.invitationToChannel, [
message.new_chat_member.first_name,
config.mainChannelInfo.name,
config.mainChannelInfo.link
])
}
}
const processUserAuth = (message) => {
users.athorizeUser(message.from, () => {
sendTextMessage(message, strings.welcome, [message.from.first_name])
})
}
const processNicknameChange = (message) => {
const [ , nickname ] = message.text.split(' ', 2)
if (nickname) {
users.setUserNickname(message.from, nickname, () => {
sendTextMessage(message, strings.nickChanged, [nickname])
})
} else {
sendTextMessage(message, strings.nickCommandFormat)
}
}
const processMemeForwarding = (message) => {
// To publish in the name of other user
const user = message.caption && users.getUserFromNickname(message.caption) || message.from
const newMeme = memes.createNewMeme(user.id, message.date)
// Prepare all promises the send the meme in all target chats
let promises = config.targetChats.map((targetChat) => {
const options = {
caption: strings.by + users.getUserNickname(user),
reply_markup: getMemeInlineKeyboard(newMeme.id)
}
if (message.photo) {
return bot.sendPhoto(targetChat.id, message.photo[0].file_id, options);
} if (message.document) {
return bot.sendDocument(targetChat.id, message.document.file_id, options);
} if (message.video) {
return bot.sendVideo(targetChat.id, message.video.file_id, options);
}
})
// Force all the promises to be executed, even if one of them is rejected
promises = promises.map(p => p.catch(e => e))
// Process all promises results
Promise.all(promises).then((messages) => {
newMeme.messages = messages.filter(m => m.chat).map(m => ({
id: m.message_id,
chatId: m.chat.id
}))
memes.saveNewMeme(newMeme)
})
}
//
// Util functions
//
const sendTextMessage = (receivedMessage, messageText, params = []) => {
params.forEach((p, i) => messageText =
messageText.replace(new RegExp(`\\{${i}\\}`, 'g'), p))
bot.sendMessage(receivedMessage.chat.id, messageText, {
parse_mode: 'Markdown'
})
}
const getMemeInlineKeyboard = (memeId, numVotes = 0) => {
return {
inline_keyboard: [[ {
text: `\u{1F923} ${numVotes}`,
callback_data: memeId
}]],
}
}
const getTargetMessages = (currentChatId, meme) => {
return meme.messages.filter((m) => {
if (m.chatId === currentChatId) {
return true
} else {
const c = config.targetChats.find(c => c.id === m.chatId)
if (c && c.expirationDays) {
return moment.unix(meme.date).add(c.expirationDays, 'days') > moment()
} else {
return true
}
}
})
}
module.exports = {
start
}