forked from group-butler/GroupButler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.lua
429 lines (386 loc) · 13.8 KB
/
bot.lua
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
local api = require 'methods'
local redis = require 'redis'
local clr = require 'term.colors'
local u, config, plugins, last_update, last_cron
db = redis.connect('127.0.0.1', 6379)
function bot_init(on_reload) -- The function run when the bot is started or reloaded.
config = dofile('config.lua') -- Load configuration file.
assert(not (config.bot_api_key == "" or not config.bot_api_key), clr.red..'Insert the bot token in config.lua -> bot_api_key'..clr.reset)
assert(#config.superadmins > 0, clr.red..'Insert your Telegram ID in config.lua -> superadmins'..clr.reset)
assert(config.log.admin, clr.red..'Insert your Telegram ID in config.lua -> log.admin'..clr.reset)
db:select(config.db or 0) --select the redis db
u = dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions.
locale = dofile('languages.lua')
now_ms = require('socket').gettime
bot = api.getMe().result -- Get bot info
bot.revision = u.bash('git rev-parse --short HEAD')
plugins = {} -- Load plugins.
for i,v in ipairs(config.plugins) do
local p = require('plugins.'..v)
package.loaded['plugins.'..v] = nil
if p.triggers then
for funct, trgs in pairs(p.triggers) do
for i = 1, #trgs do
-- interpret any whitespace character in commands just as space
trgs[i] = trgs[i]:gsub(' ', '%%s+')
end
if not p[funct] then
p.trgs[funct] = nil
print(clr.red..funct..' triggers ignored in '..v..': '..funct..' function not defined'..clr.reset)
end
end
end
table.insert(plugins, p)
end
print('\n'..clr.blue..'BOT RUNNING:'..clr.reset, clr.red..'[@'..bot.username .. '] [' .. bot.first_name ..'] ['..bot.id..']'..clr.reset..'\n')
last_update = last_update or -2 --skip pending updates
last_cron = last_cron or os.time() -- the time of the last cron job
if on_reload then
return #plugins
else
api.sendAdmin(_("*Bot *@%s* started!*\n_%s_\n%d plugins loaded"):format(bot.username:escape(), os.date('!%c UTC'), #plugins), true)
start_timestamp = os.time()
current = {h = 0}
last = {h = 0}
end
end
local function extract_usernames(msg)
if msg.from then
if msg.from.username then
db:hset('bot:usernames', '@'..msg.from.username:lower(), msg.from.id)
end
end
if msg.forward_from and msg.forward_from.username then
db:hset('bot:usernames', '@'..msg.forward_from.username:lower(), msg.forward_from.id)
end
if msg.new_chat_member then
if msg.new_chat_member.username then
db:hset('bot:usernames', '@'..msg.new_chat_member.username:lower(), msg.new_chat_member.id)
end
db:sadd(string.format('chat:%d:members', msg.chat.id), msg.new_chat_member.id)
end
if msg.left_chat_member then
if msg.left_chat_member.username then
db:hset('bot:usernames', '@'..msg.left_chat_member.username:lower(), msg.left_chat_member.id)
end
db:srem(string.format('chat:%d:members', msg.chat.id), msg.left_chat_member.id)
end
if msg.reply_to_message then
extract_usernames(msg.reply_to_message)
end
if msg.pinned_message then
extract_usernames(msg.pinned_message)
end
end
local function collect_stats(msg)
extract_usernames(msg)
if msg.chat.type ~= 'private' and msg.chat.type ~= 'inline' and msg.from then
db:hset('chat:'..msg.chat.id..':userlast', msg.from.id, os.time()) --last message for each user
db:hset('bot:chats:latsmsg', msg.chat.id, os.time()) --last message in the group
end
end
local function match_triggers(triggers, text)
if text and triggers then
text = text:gsub('^(/[%w_]+)@'..bot.username, '%1')
for i, trigger in pairs(triggers) do
local matches = {}
matches = { string.match(text, trigger) }
if next(matches) then
return matches, trigger
end
end
end
end
local function on_msg_receive(msg, callback) -- The fn run whenever a message is received.
--u.dump('PARSED', msg)
if not msg then
return
end
if msg.chat.type ~= 'group' then --do not process messages from normal groups
if msg.date < os.time() - 7 then print('Old update skipped') return end -- Do not process old messages.
if not msg.text then msg.text = msg.caption or '' end
locale.language = db:get('lang:'..msg.chat.id) or 'en' --group language
if not config.available_languages[locale.language] then
locale.language = 'en'
end
collect_stats(msg)
local continue = true
local onm_success
for i, plugin in pairs(plugins) do
if plugin.onEveryMessage then
onm_success, continue = pcall(plugin.onEveryMessage, msg)
if not onm_success then
api.sendAdmin('An #error occurred (preprocess).\n'..tostring(continue)..'\n'..locale.language..'\n'..msg.text)
end
end
if not continue then return end
end
for i,plugin in pairs(plugins) do
if plugin.triggers then
local blocks, trigger = match_triggers(plugin.triggers[callback], msg.text)
if blocks then
if msg.chat.type ~= 'private' and msg.chat.type ~= 'inline'and not db:exists('chat:'..msg.chat.id..':settings') and not msg.service then --init agroup if the bot wasn't aware to be in
u.initGroup(msg.chat.id)
end
if config.bot_settings.stream_commands then --print some info in the terminal
print(clr.reset..clr.blue..'['..os.date('%F %T')..']'..clr.red..' '..trigger..clr.reset..' '..msg.from.first_name..' ['..msg.from.id..'] -> ['..msg.chat.id..']')
end
--if not check_callback(msg, callback) then goto searchaction end
local success, result = xpcall(plugin[callback], debug.traceback, msg, blocks) --execute the main function of the plugin triggered
if not success then --if a bug happens
print(result)
if config.bot_settings.notify_bug then
api.sendReply(msg, _("🐞 Sorry, a *bug* occurred"), true)
end
api.sendAdmin('An #error occurred.\n'..result..'\n'..locale.language..'\n'..msg.text)
return
end
if type(result) == 'string' then --if the action returns a string, make that string the new msg.text
msg.text = result
elseif not result then --if the action returns true, then don't stop the loop of the plugin's actions
return
end
end
end
end
else
if msg.group_chat_created or (msg.new_chat_member and msg.new_chat_member.id == bot.id) then
-- set the language
locale.language = db:get(string.format('lang:%d', msg.from.id)) or 'en'
if not config.available_languages[locale.language] then
locale.language = 'en'
end
-- send disclamer
api.sendMessage(msg.chat.id, _([[
Hello everyone!
My name is %s, and I'm a bot made to help administrators in their hard work.
Unfortunately I can't work in normal groups, please ask the creator to convert this group to a supergroup.
]]):format(bot.first_name))
-- log this event
if config.bot_settings.stream_commands then
print(string.format('%s[%s]%s Bot was added to a normal group %s%s [%d] -> [%d]',
clr.blue, os.date('%X'), clr.yellow, clr.reset, msg.from.first_name, msg.from.id, msg.chat.id))
end
end
end
end
local function parseMessageFunction(update)
db:hincrby('bot:general', 'messages', 1)
local msg, function_key
--if update.message or update.edited_message or update.channel_post or update.edited_channel_post then
if update.message or update.edited_message then
function_key = 'onTextMessage'
--if not update.message then
if update.edited_message then
update.edited_message.edited = true
update.edited_message.original_date = update.edited_message.date
update.edited_message.date = update.edited_message.edit_date
function_key = 'onEditedMessage'
--[[elseif update.channel_post then
update.channel_post.channel_post = true
function_key = 'onChannelPost'
elseif update.edited_channel_post then
update.edited_channel_post.edited_channel_post = true
update.edited_channel_post.original_date = update.edited_channel_post.date
update.edited_channel_post.date = update.edited_channel_post.edit_date
function_key = 'onEditedChannelPost']]
end
--end
--msg = update.message or update.edited_message or update.channel_post or update.edited_channel_post
msg = update.message or update.edited_message
if msg.text then
elseif msg.photo then
msg.media = true
msg.media_type = 'photo'
elseif msg.audio then
msg.media = true
msg.media_type = 'audio'
elseif msg.document then
msg.media = true
msg.media_type = 'document'
if msg.document.mime_type == 'video/mp4' then
msg.media_type = 'gif'
end
elseif msg.sticker then
msg.media = true
msg.media_type = 'sticker'
elseif msg.video then
msg.media = true
msg.media_type = 'video'
elseif msg.voice then
msg.media = true
msg.media_type = 'voice'
elseif msg.contact then
msg.media = true
msg.media_type = 'contact'
elseif msg.venue then
msg.media = true
msg.media_type = 'venue'
elseif msg.location then
msg.media = true
msg.media_type = 'location'
elseif msg.game then
msg.media = true
msg.media_type = 'game'
elseif msg.left_chat_member then
msg.service = true
if msg.left_chat_member.id == bot.id then
msg.text = '###left_chat_member:bot'
else
msg.text = '###left_chat_member'
end
elseif msg.new_chat_member then
msg.service = true
if msg.new_chat_member.id == bot.id then
msg.text = '###new_chat_member:bot'
else
msg.text = '###new_chat_member'
end
elseif msg.new_chat_photo then
msg.service = true
msg.text = '###new_chat_photo'
elseif msg.delete_chat_photo then
msg.service = true
msg.text = '###delete_chat_photo'
elseif msg.group_chat_created then
msg.service = true
msg.text = '###group_chat_created'
elseif msg.supergroup_chat_created then
msg.service = true
msg.text = '###supergroup_chat_created'
elseif msg.channel_chat_created then
msg.service = true
msg.text = '###channel_chat_created'
elseif msg.migrate_to_chat_id then
msg.service = true
msg.text = '###migrate_to_chat_id'
elseif msg.migrate_from_chat_id then
msg.service = true
msg.text = '###migrate_from_chat_id'
elseif msg.new_chat_title then
msg.service = true
msg.text = '###new_chat_title'
elseif msg.pinned_message then
msg.service = true
msg.text = '###pinned_message'
else
--callback = 'onUnknownType'
print('Unknown update type') return
end
if msg.forward_from_chat then
if msg.forward_from_chat.type == 'channel' then
msg.spam = 'forwards'
end
end
if msg.caption then
local caption_lower = msg.caption:lower()
if caption_lower:match('telegram%.me') or caption_lower:match('telegram%.dog') or caption_lower:match('t%.me') then
msg.spam = 'links'
end
end
if msg.entities then
for i, entity in pairs(msg.entities) do
if entity.type == 'text_mention' then
msg.mention_id = entity.user.id
end
if entity.type == 'url' or entity.type == 'text_link' then
local text_lower = msg.text or msg.caption
text_lower = entity.url and text_lower..entity.url or text_lower
text_lower = text_lower:lower()
if text_lower:match('telegram%.me') or
text_lower:match('telegram%.dog') or
text_lower:match('t%.me') then
msg.spam = 'links'
else
msg.media_type = 'link'
msg.media = true
end
end
end
end
if msg.reply_to_message then
msg.reply = msg.reply_to_message
if msg.reply.caption then
msg.reply.text = msg.reply.caption
end
end
--[[elseif update.inline_query then
msg = update.inline_query
msg.inline = true
msg.chat = {id = msg.from.id, type = 'inline', title = 'inline'}
msg.date = os.time()
msg.text = '###inline:'..msg.query
function_key = 'onInlineQuery'
elseif update.chosen_inline_result then
msg = update.chosen_inline_result
msg.text = '###chosenresult:'..msg.query
msg.chat = {type = 'inline', id = msg.from.id, title = msg.from.first_name}
msg.message_id = msg.inline_message_id
msg.date = os.time()
function_key = 'onChosenInlineQuery']]
elseif update.callback_query then
msg = update.callback_query
msg.cb = true
msg.text = '###cb:'..msg.data
if msg.message then
msg.original_text = msg.message.text
msg.original_date = msg.message.date
msg.message_id = msg.message.message_id
msg.chat = msg.message.chat
else --when the inline keyboard is sent via the inline mode
msg.chat = {type = 'inline', id = msg.from.id, title = msg.from.first_name}
msg.message_id = msg.inline_message_id
end
msg.date = os.time()
msg.cb_id = msg.id
msg.message = nil
msg.target_id = msg.data:match('(-%d+)$') --callback datas often ship IDs
function_key = 'onCallbackQuery'
else
--function_key = 'onUnknownType'
print('Unknown update type') return
end
if (msg.chat.id < 0 or msg.target_id) and msg.from then
msg.from.admin = u.is_admin(msg.target_id or msg.chat.id, msg.from.id)
if msg.from.admin then
msg.from.mod = true
else
-- XXX: double call is_admin function
msg.from.mod = u.is_mod(msg.target_id or msg.chat.id, msg.from.id)
end
end
--print('Mod:', msg.from.mod, 'Admin:', msg.from.admin)
return on_msg_receive(msg, function_key)
end
bot_init() -- Actually start the script. Run the bot_init function.
api.firstUpdate()
while true do -- Start a loop while the bot should be running.
local res = api.getUpdates(last_update+1) -- Get the latest updates
if res then
clocktime_last_update = os.clock()
for i, msg in ipairs(res.result) do -- Go through every new message.
last_update = msg.update_id
--print(last_update)
current.h = current.h + 1
parseMessageFunction(msg)
end
else
print('Connection error')
end
if last_cron ~= os.date('%M') then -- Run cron jobs every minute.
last_cron = os.date('%M')
last.h = current.h
current.h = 0
for i,v in ipairs(plugins) do
if v.cron then -- Call each plugin's cron function, if it has one.
local res, err = xpcall(v.cron, debug.traceback)
if not res then
print(err)
api.sendLog('An #error occurred (cron).\n'..err)
return
end
end
end
end
end
print('Halted.\n')