-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.lua
108 lines (85 loc) · 2.92 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
HTTP = require('socket.http')
HTTPS = require('ssl.https')
URL = require('socket.url')
JSON = require('dkjson')
version = '3.2'
bot_init = function() -- The function run when the bot is started or reloaded.
config = dofile('config.lua') -- Load configuration file.
dofile('bindings.lua') -- Load Telegram bindings.
dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions.
-- Fetch bot information. Try until it succeeds.
repeat bot = getMe() until bot
bot = bot.result
plugins = {} -- Load plugins.
for i,v in ipairs(config.plugins) do
local p = dofile('plugins/'..v)
table.insert(plugins, p)
end
print('@'..bot.username .. ', AKA ' .. bot.first_name ..' ('..bot.id..')')
-- Generate a random seed and "pop" the first random number. :)
math.randomseed(os.time())
math.random()
last_update = last_update or 0 -- Set loop variables: Update offset,
last_cron = last_cron or os.time() -- the time of the last cron job,
is_started = true -- whether the bot should be running or not.
usernames = usernames or {} -- Table to cache usernames by user ID.
end
on_msg_receive = function(msg) -- The fn run whenever a message is received.
if msg.from.username then
usernames[msg.from.username:lower()] = msg.from.id
end
if msg.date < os.time() - 5 then return end -- Do not process old messages.
if not msg.text then msg.text = msg.caption or '' end
if msg.text:match('^/start .+') then
msg.text = '/' .. msg.text:input()
end
for i,v in ipairs(plugins) do
for k,w in pairs(v.triggers) do
if string.match(msg.text:lower(), w) then
-- a few shortcuts
msg.chat.id_str = tostring(msg.chat.id)
msg.from.id_str = tostring(msg.from.id)
msg.text_lower = msg.text:lower()
local success, result = pcall(function()
return v.action(msg)
end)
if not success then
sendReply(msg, 'An unexpected error occurred.')
handle_exception(result, msg.text)
return
end
-- If the action returns a table, make that table msg.
if type(result) == 'table' then
msg = result
-- If the action returns true, don't stop.
elseif result ~= true then
return
end
end
end
end
end
bot_init() -- Actually start the script. Run the bot_init function.
while is_started do -- Start a loop while the bot should be running.
local res = getUpdates(last_update+1) -- Get the latest updates!
if res then
for i,v in ipairs(res.result) do -- Go through every new message.
last_update = v.update_id
on_msg_receive(v.message)
end
else
print(config.errors.connection)
end
if last_cron < os.time() - 5 then -- Run cron jobs if the time has come.
for i,v in ipairs(plugins) do
if v.cron then -- Call each plugin's cron function, if it has one.
local res, err = pcall(function() v.cron() end)
if not res then
handle_exception(err, 'CRON: ' .. i)
end
end
end
last_cron = os.time() -- And finally, update the variable.
end
end
print('Halted.')