-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
439 lines (399 loc) · 13.4 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
const config = require('./config.js')
const npm = require('npm')
const http = require('http')
const fs = require('fs')
const TelegramBot = require('node-telegram-bot-api');
const botId = config.botId
const token = config.token
const bot = new TelegramBot(token, {polling: true})
const INPUT_TIMEOUT = 60000
const MAX_LENGTH = 4096
const MSG_ACCESS_DENIED = 'bot_access_denied'
const MSG_PLUGIN_LIST = 'bot_plugin_list'
const MSG_NO_HELP = 'bot_no_help'
const MSG_HELP_USAGE = 'bot_help_usage'
const MSG_CONFIG_USAGE = 'bot_config_usage'
const MSG_CONFIG_SET = 'bot_config_set'
const MSG_INVALID_CONFIG_KEY = 'bot_invalid_config_key'
const MSG_INVALID_CONFIG_VALUE = 'bot_invalid_config_value'
let plugins = {}
let listeners = {
message: [],
inline: [],
command: {},
stream: {},
read: {}
}
let botConfigs = {}
let userConfigs = {}
let groupConfigs = {}
let users = {}
let groups = {}
const API = {
getConfig: function(key) {
return config[key]
},
getBotConfig: function(key) {
return botConfigs[key]
},
getPlugin: function(name) {
return plugins[name].api
},
getString: function(locale, key, args) {
return API.getPlugin('i18n').getString(locale, key, args)
},
getUserString: function(userId, key, args) {
return API.getPlugin('i18n').getUserString(userId, key, args)
},
getUserConfig: function(userId, key, defaultValue = null) {
if(!users[userId]) return defaultValue
if(!users[userId][key]) return defaultValue
return users[userId][key]
},
setUserConfig: function(userId, key, value) {
if(!users[userId]) users[userId] = {}
users[userId][key] = value
storeUsers()
},
getGroupConfig: function(chatId, key, defaultValue = null) {
if(!groups[chatId] || !groups[chatId][key]) return defaultValue
return groups[chatId][key]
},
setGroupConfig: function(chatId, key, value) {
if(!groups[chatId]) groups[chatId] = {}
groups[chatId][key] = value
storeGroups()
},
addUserConfigKey: function(key, values) {
userConfigs[key] = values
},
addGroupConfigKey: function(key, values) {
groupConfigs[key] = values
},
addCommand: function(command, callback, help) {
listeners.command[command] = {callback: callback, help: help}
},
removeCommand: function(priority, command) {
delete listeners.command[command]
},
addInline: function(priority, callback) {
if(!listeners.inline[priority])
listeners.inline[priority] = []
listeners.inline[priority].push(callback)
},
removeInline: function(priority, callback) {
listeners.inline[priority].splice(listeners.inline[priority].indexOf(callback), 1)
},
addListener: function(priority, callback) {
if(!listeners.message[priority])
listeners.message[priority] = []
listeners.message[priority].push(callback)
},
removeListener: function(priority, callback) {
listeners.message[priority].splice(listeners.message[priority].indexOf(callback), 1)
},
sendMessage: function(chatId, msg, options={}, delayed=false) {
bot.sendChatAction(chatId, 'typing')
msg = msg.length > MAX_LENGTH ? msg.substring(0, MAX_LENGTH-3) + '...' : msg
if(delayed) {
const length = msg.normalize("NFD").length
setTimeout(function() {
bot.sendMessage(chatId, msg, options)
}, 109 * length) // 2018년 10월 20일 기준 리의 평균 타속: 분당 550타
} else {
return bot.sendMessage(chatId, msg, options)
}
},
sendPhoto: function(chatId, url, options={}) {
bot.sendPhoto(chatId, url, options)
},
answerInlineQuery: function(id, result) {
bot.answerInlineQuery(id, result)
}
}
const registerPlugin = function(name) {
const pluginDir = config.pluginDir + name + '/'
if(!fs.existsSync(pluginDir + 'plugin.json')) return
const data = fs.readFileSync(pluginDir + 'plugin.json', 'utf8')
const plugin = JSON.parse(data)
plugin.dir = pluginDir
plugin.loaded = false
plugins[plugin.name] = plugin
if(plugin.packages) {
const options = {
'save': false,
}
npm.load(options, (err) => {
if(err) {
console.log(err)
return
}
npm.commands.install(plugin.packages, (err, data) => {
if(err) {
console.log(err)
return
}
if(plugin.main) plugin.api = require(pluginDir + plugin.main)(API)
plugin.loaded = true
})
npm.on('log', msg => console.log)
})
} else {
if(plugin.main) plugin.api = require(pluginDir + plugin.main)(API)
plugin.loaded = true
}
}
const matchCommand = function(msg) {
if(!msg.text)
return
const commands = msg.text.split(/ ?\| ?/)
const cmds = []
const streams = []
for(let i = commands.length-1 ; i >= 0 ; i--) {
const command = commands[i]
for(let cmd in listeners.command) {
const regexp = new RegExp('^\\/(' + cmd + ')(?:@' + botId + ')?(?: ([\\s\\S]*))?$', 'm')
const match = regexp.exec(command)
if(match) {
const previous = (streams.length > 0) ? streams[streams.length-1] : undefined
const stream = new Stream(msg, match[1], match[2], previous, i == commands.length-1, i == 0)
cmds.push({callback: listeners.command[cmd].callback, stream: stream})
streams.push(stream)
break
}
}
}
for(let cmd of cmds) {
setTimeout(() => cmd.callback(cmd.stream), 0)
}
if(cmds.length > 0) return true
else return false
}
const Stream = function(msg, command, args, previous=undefined, first=false, last=false) {
this.msg = msg
this.command = command
this.args = args
this.buffer = []
this.writeBuffer = (msg) => this.buffer.push(msg)
if(last) {
this.read = (callback) => {
API.sendMessage(msg.chat.id, 'INPUT>', {reply_to_message_id: msg.message_id, parse_mode: 'HTML', reply_markup: {force_reply: true, selective: true}}).then(m => {
const readId = m.chat.id + ',' + m.message_id
listeners.read[readId] = (text) => callback(text)
setTimeout(() => {
if(listeners.read[readId]) {
delete listeners.read[readId]
bot.deleteMessage(m.chat.id, m.message_id)
}
}, INPUT_TIMEOUT)
})
}
} else {
this.read = (callback) => {
const readBuffer = () => this.buffer.length > 0 ? callback(this.buffer.splice(0, 1)[0]) : setTimeout(readBuffer, 100)
readBuffer()
}
}
if(first) {
this.write = (text, delayed=false) => {
API.sendMessage(msg.chat.id, text, {reply_to_message_id: msg.message_id, parse_mode: 'HTML'}, delayed)
}
} else {
if(previous && previous.writeBuffer) {
this.write = (text, delayed=false) => {
previous.writeBuffer(text)
}
}
}
}
bot.on('inline_query', (query) => {
for(let i in listeners.inline) {
for(let listener of listeners.inline[i]) {
if(listener(query))
return
}
}
})
bot.on('message', (msg) => {
if(msg.reply_to_message && msg.reply_to_message.from.username == botId) {
const readId = msg.chat.id + ',' + msg.reply_to_message.message_id
const read = listeners.read[readId]
if(read) {
read(msg.text)
delete listeners.read[readId]
bot.deleteMessage(msg.chat.id, msg.reply_to_message.message_id)
return
}
}
if(matchCommand(msg)) return
for(let i in listeners.message) {
for(let listener of listeners.message[i]) {
if(listener(msg))
return
}
}
})
bot.onText(new RegExp('^/(plugins)(@' + botId + ')?$'), (msg, match) => {
let result = API.getUserString(msg.from.id, MSG_PLUGIN_LIST, [])
for(let key in plugins) {
result += '\n- ' + plugins[key].name
}
API.sendMessage(msg.chat.id, result, {reply_to_message_id: msg.message_id})
})
bot.onText(new RegExp('^/(help)(@' + botId + ')?( (.*))?$'), (msg, match) => {
const command = match[4]
let result = ''
if(command) {
for(let cmd in listeners.command) {
if(new RegExp('^(' + cmd + ')$').exec(command)) {
const help = listeners.command[cmd].help
const helpString = API.getUserString(msg.from.id, help, [])
if(helpString) {
result += helpString
} else if(help) {
result += help
} else {
result += API.getUserString(msg.from.id, MSG_NO_HELP, [])
}
break
}
}
} else {
result += API.getUserString(msg.from.id, MSG_HELP_USAGE, [botId])
for(let cmd in listeners.command) {
result += '\n- ' + cmd
}
}
API.sendMessage(msg.chat.id, result, {reply_to_message_id: msg.message_id})
})
bot.onText(new RegExp('^/(config)(@' + botId + ')?( (.*))?$'), (msg, match) => {
const regex = RegExp('([^\\"]\\S*|".+?")\\s*', 'g')
let args = []
while(true) {
const m = regex.exec(match[4])
if(!m) break
else args.push(m[1].replace(/"/g, ''))
}
if(args && args.length >= 2) {
const key = args[0]
const value = args[1]
if(userConfigs[key]) {
if(userConfigs[key].length > 0 && userConfigs[key].includes(value) || userConfigs[key].length == 0) {
API.setUserConfig(msg.from.id, key, value)
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_CONFIG_SET, []), {reply_to_message_id: msg.message_id})
} else {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_INVALID_CONFIG_VALUE, []), {reply_to_message_id: msg.message_id})
}
} else {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_INVALID_CONFIG_KEY, []), {reply_to_message_id: msg.message_id})
}
} else {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_CONFIG_USAGE, [botId]), {reply_to_message_id: msg.message_id})
}
})
bot.onText(new RegExp('^/(groupconfig)(@' + botId + ')?( (.*))?$'), (msg, match) => {
bot.getChatMember(msg.chat.id, msg.from.id).then((member) => {
if(!checkAdmin(member)) {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_ACCESS_DENIED, []), {reply_to_message_id: msg.message_id})
return
}
const regex = RegExp('([^\\"]\\S*|".+?")\\s*', 'g')
let args = []
while(true) {
const m = regex.exec(match[4])
if(!m) break
else args.push(m[1].replace(/"/g, ''))
}
if(args && args.length >= 2) {
const key = args[0]
const value = args[1]
if(groupConfigs[key]) {
if(groupConfigs[key].length > 0 && groupConfigs[key].includes(value) || groupConfigs[key].length == 0) {
API.setGroupConfig(msg.chat.id, key, value)
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_CONFIG_SET, []), {reply_to_message_id: msg.message_id})
} else {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_INVALID_CONFIG_VALUE, []), {reply_to_message_id: msg.message_id})
}
} else {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_INVALID_CONFIG_KEY, []), {reply_to_message_id: msg.message_id})
}
} else {
API.sendMessage(msg.chat.id, API.getUserString(msg.from.id, MSG_CONFIG_USAGE, [botId]), {reply_to_message_id: msg.message_id})
}
})
})
const checkAdmin = function(member) {
if(member.status === 'creator' || member.status === 'administrator') return true
else return false
}
bot.on('polling_error', (err) => {
console.log(err)
})
// Main code.
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.write('')
res.end()
}).listen(config.port || 80)
fs.readdir(config.pluginDir, (err, files) => {
if(err)
return
files.forEach(file => {
registerPlugin(file)
})
const checkLoaded = () => {
if(!Object.values(plugins).every(plugin => plugin.loaded)) setTimeout(checkLoaded, 100)
else initPlugins()
}
checkLoaded()
})
const initPlugins = function() {
for(let key in plugins) {
const plugin = plugins[key]
if(plugin.localeDir) API.getPlugin('i18n').load(plugin.dir + plugin.localeDir)
if(plugin.api && plugin.api.init) plugin.api.init()
}
loadBotConfig()
loadUsers()
loadGroups()
}
const loadBotConfig = function() {
API.getPlugin('google-sheets').select('config!A:Z', (err, rows) => {
if(err || !rows) return
rows.forEach(row => {
botConfigs[row[0]] = JSON.parse(row[1])
})
})
}
const loadUsers = function() {
API.getPlugin('google-sheets').select('users!A:Z', (err, rows) => {
if(err || !rows) return
rows.forEach(row => {
users[row[0]] = JSON.parse(row[1])
})
})
}
const storeUsers = function() {
API.getPlugin('google-sheets').update('users!A:Z', (row, index) => [[row[0], JSON.stringify(users[row[0]])]])
API.getPlugin('google-sheets').select('users!A:Z', (err, rows) => {
if(err) return
const toAdd = rows ? Object.keys(users).filter(key => rows.find(row => row[0] == key) == undefined) : Object.keys(users)
API.getPlugin('google-sheets').insert('users!A:Z', toAdd.map(key => [key, JSON.stringify(users[key])]))
})
}
const loadGroups = function() {
API.getPlugin('google-sheets').select('groups!A:Z', (err, rows) => {
if(err || !rows) return
rows.forEach(row => {
groups[row[0]] = JSON.parse(row[1])
})
})
}
const storeGroups = function() {
API.getPlugin('google-sheets').update('groups!A:Z', (row, index) => [[row[0], JSON.stringify(groups[row[0]])]])
API.getPlugin('google-sheets').select('groups!A:Z', (err, rows) => {
if(err) return
const toAdd = rows ? Object.keys(groups).filter(key => rows.find(row => row[0] == key) == undefined) : Object.keys(groups)
API.getPlugin('google-sheets').insert('groups!A:Z', toAdd.map(key => [key, JSON.stringify(groups[key])]))
})
}