2016-04-08 23:12:02 +02:00
|
|
|
local bot = {}
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-29 06:36:35 +02:00
|
|
|
-- Requires are moved to init to allow for reloads.
|
|
|
|
local bindings -- Load Telegram bindings.
|
|
|
|
local utilities -- Load miscellaneous and cross-plugin functions.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-29 06:36:35 +02:00
|
|
|
bot.version = '3.7'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
function bot:init() -- The function run when the bot is started or reloaded.
|
|
|
|
|
2016-04-29 06:36:35 +02:00
|
|
|
bindings = require('bindings')
|
|
|
|
utilities = require('utilities')
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
self.config = require('config') -- Load configuration file.
|
|
|
|
|
|
|
|
self.BASE_URL = 'https://api.telegram.org/bot' .. self.config.bot_api_key
|
|
|
|
if self.config.bot_api_key == '' then
|
|
|
|
error('You did not set your bot token in config.lua!')
|
|
|
|
end
|
2015-07-10 09:52:22 +02:00
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
-- Fetch bot information. Try until it succeeds.
|
2016-04-08 23:12:02 +02:00
|
|
|
repeat self.info = bindings.getMe(self) until self.info
|
|
|
|
self.info = self.info.result
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-03-08 14:15:48 +01:00
|
|
|
-- Load the "database"! ;)
|
2016-04-08 23:12:02 +02:00
|
|
|
if not self.database then
|
|
|
|
self.database = utilities.load_data(self.info.username..'.db')
|
2016-03-08 14:15:48 +01:00
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
self.plugins = {} -- Load plugins.
|
|
|
|
for _,v in ipairs(self.config.plugins) do
|
2016-04-14 05:48:20 +02:00
|
|
|
local p = require('plugins.'..v)
|
2016-04-08 23:12:02 +02:00
|
|
|
table.insert(self.plugins, p)
|
|
|
|
if p.init then p.init(self) end
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
print('@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.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
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
self.last_update = self.last_update or 0 -- Set loop variables: Update offset,
|
|
|
|
self.last_cron = self.last_cron or os.date('%M') -- the time of the last cron job,
|
|
|
|
self.is_started = true -- and whether or not the bot should be running.
|
|
|
|
self.database.users = self.database.users or {} -- Table to cache userdata.
|
|
|
|
self.database.users[tostring(self.info.id)] = self.info
|
2016-03-22 11:16:26 +01:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
function bot:on_msg_receive(msg) -- The fn run whenever a message is received.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-03-22 11:16:26 +01:00
|
|
|
-- Create a user entry if it does not exist.
|
2016-04-08 23:12:02 +02:00
|
|
|
if not self.database.users[tostring(msg.from.id)] then
|
|
|
|
self.database.users[tostring(msg.from.id)] = {}
|
2016-03-08 14:15:48 +01:00
|
|
|
end
|
2016-03-22 11:16:26 +01:00
|
|
|
-- Clear things that no longer exist.
|
2016-04-08 23:12:02 +02:00
|
|
|
self.database.users[tostring(msg.from.id)].username = nil
|
|
|
|
self.database.users[tostring(msg.from.id)].last_name = nil
|
2016-03-22 11:16:26 +01:00
|
|
|
-- Wee.
|
2016-03-08 14:15:48 +01:00
|
|
|
for k,v in pairs(msg.from) do
|
2016-04-08 23:12:02 +02:00
|
|
|
self.database.users[tostring(msg.from.id)][k] = v
|
2016-03-08 14:15:48 +01:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if msg.date < os.time() - 5 then return end -- Do not process old messages.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-12 15:47:30 +02:00
|
|
|
msg = utilities.enrich_message(msg)
|
2016-04-03 21:18:25 +02:00
|
|
|
|
2016-01-08 04:30:12 +01:00
|
|
|
if msg.text:match('^/start .+') then
|
2016-04-08 23:12:02 +02:00
|
|
|
msg.text = '/' .. utilities.input(msg.text)
|
2016-04-12 11:24:56 +02:00
|
|
|
msg.text_lower = msg.text:lower()
|
2016-01-08 04:30:12 +01:00
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
for _,v in ipairs(self.plugins) do
|
|
|
|
for _,w in pairs(v.triggers) do
|
2015-12-13 22:31:22 +01:00
|
|
|
if string.match(msg.text:lower(), w) then
|
2015-11-25 03:22:04 +01:00
|
|
|
local success, result = pcall(function()
|
2016-04-08 23:12:02 +02:00
|
|
|
return v.action(self, msg)
|
2015-11-25 03:22:04 +01:00
|
|
|
end)
|
|
|
|
if not success then
|
2016-04-08 23:12:02 +02:00
|
|
|
bindings.sendReply(self, msg, 'Sorry, an unexpected error occurred.')
|
2016-04-14 05:48:20 +02:00
|
|
|
utilities.handle_exception(self, result, msg.from.id .. ': ' .. 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
|
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
function bot:run()
|
|
|
|
bot.init(self) -- Actually start the script. Run the bot_init function.
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
while self.is_started do -- Start a loop while the bot should be running.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
do
|
|
|
|
local res = bindings.getUpdates(self, self.last_update+1) -- Get the latest updates!
|
|
|
|
if res then
|
|
|
|
for _,v in ipairs(res.result) do -- Go through every new message.
|
|
|
|
self.last_update = v.update_id
|
|
|
|
bot.on_msg_receive(self, v.message)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print(self.config.errors.connection)
|
2016-04-08 23:12:02 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
if self.last_cron ~= os.date('%M') then -- Run cron jobs every minute.
|
|
|
|
self.last_cron = os.date('%M')
|
|
|
|
utilities.save_data(self.info.username..'.db', self.database) -- Save the database.
|
|
|
|
for i,v in ipairs(self.plugins) do
|
|
|
|
if v.cron then -- Call each plugin's cron function, if it has one.
|
|
|
|
local res, err = pcall(function() v.cron(self) end)
|
|
|
|
if not res then
|
|
|
|
utilities.handle_exception(self, err, 'CRON: ' .. i)
|
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
end
|
2015-07-15 08:15:23 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2016-04-14 05:48:20 +02:00
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
-- Save the database before exiting.
|
|
|
|
utilities.save_data(self.info.username..'.db', self.database)
|
|
|
|
print('Halted.')
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
2015-08-29 08:15:01 +02:00
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
return bot
|