2015-07-03 00:15:52 +02:00
|
|
|
HTTP = require('socket.http')
|
2015-11-25 03:22:04 +01:00
|
|
|
HTTPS = require('ssl.https')
|
|
|
|
URL = require('socket.url')
|
2015-07-10 09:52:22 +02:00
|
|
|
JSON = require('dkjson')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
version = '3.2'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
bot_init = function() -- The function run when the bot is started or reloaded.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-08 04:30:12 +01:00
|
|
|
config = dofile('config.lua') -- Load configuration file.
|
|
|
|
dofile('bindings.lua') -- Load Telegram bindings.
|
|
|
|
dofile('utilities.lua') -- Load miscellaneous and cross-plugin functions.
|
2015-07-10 09:52:22 +02:00
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
-- Fetch bot information. Try until it succeeds.
|
|
|
|
repeat bot = getMe() until bot
|
2015-11-25 03:22:04 +01:00
|
|
|
bot = bot.result
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
plugins = {} -- Load plugins.
|
|
|
|
for i,v in ipairs(config.plugins) do
|
2016-01-08 04:30:12 +01:00
|
|
|
local p = dofile('plugins/'..v)
|
2015-11-25 03:22:04 +01:00
|
|
|
table.insert(plugins, p)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
print('@'..bot.username .. ', AKA ' .. bot.first_name ..' ('..bot.id..')')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
-- Generate a random seed and "pop" the first random number. :)
|
|
|
|
math.randomseed(os.time())
|
|
|
|
math.random()
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
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.
|
2016-01-12 11:22:28 +01:00
|
|
|
usernames = usernames or {} -- Table to cache usernames by user ID.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
on_msg_receive = function(msg) -- The fn run whenever a message is received.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
if msg.from.username then
|
|
|
|
usernames[msg.from.username:lower()] = msg.from.id
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if msg.date < os.time() - 5 then return end -- Do not process old messages.
|
2015-12-04 15:56:48 +01:00
|
|
|
if not msg.text then msg.text = msg.caption or '' end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-01-08 04:30:12 +01:00
|
|
|
if msg.text:match('^/start .+') then
|
|
|
|
msg.text = '/' .. msg.text:input()
|
|
|
|
end
|
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
for i,v in ipairs(plugins) do
|
2015-11-25 03:22:04 +01:00
|
|
|
for k,w in pairs(v.triggers) do
|
2015-12-13 22:31:22 +01:00
|
|
|
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()
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local success, result = pcall(function()
|
|
|
|
return v.action(msg)
|
|
|
|
end)
|
|
|
|
if not success then
|
|
|
|
sendReply(msg, 'An unexpected error occurred.')
|
2016-01-13 19:00:17 +01:00
|
|
|
handle_exception(result, msg.text)
|
2015-11-25 03:22:04 +01:00
|
|
|
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
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
bot_init() -- Actually start the script. Run the bot_init function.
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
while is_started do -- Start a loop while the bot should be running.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local res = getUpdates(last_update+1) -- Get the latest updates!
|
|
|
|
if res then
|
2015-12-05 15:30:52 +01:00
|
|
|
for i,v in ipairs(res.result) do -- Go through every new message.
|
2015-11-25 03:22:04 +01:00
|
|
|
last_update = v.update_id
|
|
|
|
on_msg_receive(v.message)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
else
|
|
|
|
print(config.errors.connection)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
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)
|
2016-01-13 19:00:17 +01:00
|
|
|
if not res then
|
|
|
|
handle_exception(err, 'CRON: ' .. i)
|
|
|
|
end
|
2015-07-15 08:15:23 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
last_cron = os.time() -- And finally, update the variable.
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2015-07-19 22:46:06 +02:00
|
|
|
print('Halted.')
|