2015-07-02 18:15:52 -04:00
|
|
|
HTTP = require('socket.http')
|
2015-11-24 21:22:04 -05:00
|
|
|
HTTPS = require('ssl.https')
|
|
|
|
URL = require('socket.url')
|
2016-02-21 00:21:48 -05:00
|
|
|
JSON = require('cjson')
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-03-22 06:16:26 -04:00
|
|
|
version = '3.5'
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
bot_init = function() -- The function run when the bot is started or reloaded.
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-01-07 22:30:12 -05: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 03:52:22 -04:00
|
|
|
|
2016-01-12 05:22:28 -05:00
|
|
|
-- Fetch bot information. Try until it succeeds.
|
|
|
|
repeat bot = getMe() until bot
|
2015-11-24 21:22:04 -05:00
|
|
|
bot = bot.result
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-03-08 08:15:48 -05:00
|
|
|
-- Load the "database"! ;)
|
|
|
|
if not database then
|
|
|
|
database = load_data(bot.username..'.db')
|
|
|
|
end
|
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
plugins = {} -- Load plugins.
|
|
|
|
for i,v in ipairs(config.plugins) do
|
2016-01-07 22:30:12 -05:00
|
|
|
local p = dofile('plugins/'..v)
|
2015-11-24 21:22:04 -05:00
|
|
|
table.insert(plugins, p)
|
|
|
|
end
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-02-23 06:15:48 -05:00
|
|
|
print('@' .. bot.username .. ', AKA ' .. bot.first_name ..' ('..bot.id..')')
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
-- Generate a random seed and "pop" the first random number. :)
|
|
|
|
math.randomseed(os.time())
|
|
|
|
math.random()
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
last_update = last_update or 0 -- Set loop variables: Update offset,
|
2016-03-22 06:16:26 -04:00
|
|
|
last_cron = last_cron or os.date('%M') -- the time of the last cron job,
|
2016-02-21 14:28:40 -05:00
|
|
|
is_started = true -- and whether or not the bot should be running.
|
2016-03-08 08:15:48 -05:00
|
|
|
database.users = database.users or {} -- Table to cache userdata.
|
2016-03-22 06:16:26 -04:00
|
|
|
database.users[tostring(bot.id)] = bot
|
|
|
|
|
|
|
|
-- Migration code. Remove in 3.6.
|
|
|
|
if database.lastfm then
|
|
|
|
for k,v in pairs(database.lastfm) do
|
|
|
|
if not database.users[k] then database.users[k] = {} end
|
|
|
|
database.users[k].lastfm = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Migration code. Remove in 3.6.
|
|
|
|
if database.nicknames then
|
|
|
|
for k,v in pairs(database.nicknames) do
|
|
|
|
if not database.users[k] then database.users[k] = {} end
|
|
|
|
database.users[k].nickname = v
|
|
|
|
end
|
|
|
|
end
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
end
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
on_msg_receive = function(msg) -- The fn run whenever a message is received.
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-03-22 06:16:26 -04:00
|
|
|
-- Create a user entry if it does not exist.
|
2016-03-08 08:15:48 -05:00
|
|
|
if not database.users[tostring(msg.from.id)] then
|
|
|
|
database.users[tostring(msg.from.id)] = {}
|
|
|
|
end
|
2016-03-22 06:16:26 -04:00
|
|
|
-- Clear things that no longer exist.
|
|
|
|
database.users[tostring(msg.from.id)].username = nil
|
|
|
|
database.users[tostring(msg.from.id)].last_name = nil
|
|
|
|
-- Wee.
|
2016-03-08 08:15:48 -05:00
|
|
|
for k,v in pairs(msg.from) do
|
|
|
|
database.users[tostring(msg.from.id)][k] = v
|
|
|
|
end
|
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
if msg.date < os.time() - 5 then return end -- Do not process old messages.
|
2015-12-04 09:56:48 -05:00
|
|
|
if not msg.text then msg.text = msg.caption or '' end
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2016-01-07 22:30:12 -05:00
|
|
|
if msg.text:match('^/start .+') then
|
|
|
|
msg.text = '/' .. msg.text:input()
|
|
|
|
end
|
|
|
|
|
2015-07-02 18:15:52 -04:00
|
|
|
for i,v in ipairs(plugins) do
|
2015-11-24 21:22:04 -05:00
|
|
|
for k,w in pairs(v.triggers) do
|
2015-12-13 16:31:22 -05: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()
|
2016-02-20 05:07:20 -05:00
|
|
|
msg.from.name = msg.from.first_name
|
|
|
|
if msg.from.last_name then
|
|
|
|
msg.from.name = msg.from.first_name .. ' ' .. msg.from.last_name
|
|
|
|
end
|
2015-12-13 16:31:22 -05:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
local success, result = pcall(function()
|
|
|
|
return v.action(msg)
|
|
|
|
end)
|
|
|
|
if not success then
|
2016-02-20 05:07:20 -05:00
|
|
|
sendReply(msg, 'Sorry, an unexpected error occurred.')
|
2016-03-22 06:16:26 -04:00
|
|
|
handle_exception(result, msg.from.id .. ': ' .. msg.text)
|
2015-11-24 21:22:04 -05: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-02 18:15:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
bot_init() -- Actually start the script. Run the bot_init function.
|
2015-08-28 23:15:01 -07:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
while is_started do -- Start a loop while the bot should be running.
|
2015-07-02 18:15:52 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
local res = getUpdates(last_update+1) -- Get the latest updates!
|
|
|
|
if res then
|
2015-12-05 09:30:52 -05:00
|
|
|
for i,v in ipairs(res.result) do -- Go through every new message.
|
2015-11-24 21:22:04 -05:00
|
|
|
last_update = v.update_id
|
|
|
|
on_msg_receive(v.message)
|
2015-07-02 18:15:52 -04:00
|
|
|
end
|
2015-11-24 21:22:04 -05:00
|
|
|
else
|
|
|
|
print(config.errors.connection)
|
2015-07-02 18:15:52 -04:00
|
|
|
end
|
|
|
|
|
2016-03-22 06:16:26 -04:00
|
|
|
if last_cron ~= os.date('%M') then -- Run cron jobs every minute.
|
|
|
|
last_cron = os.date('%M')
|
2016-03-08 08:41:45 -05:00
|
|
|
save_data(bot.username..'.db', database) -- Save the database.
|
2015-11-24 21:22:04 -05:00
|
|
|
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 13:00:17 -05:00
|
|
|
if not res then
|
|
|
|
handle_exception(err, 'CRON: ' .. i)
|
|
|
|
end
|
2015-07-15 02:15:23 -04:00
|
|
|
end
|
2015-07-02 18:15:52 -04:00
|
|
|
end
|
|
|
|
end
|
2015-08-28 23:15:01 -07:00
|
|
|
|
2015-07-02 18:15:52 -04:00
|
|
|
end
|
2015-08-28 23:15:01 -07:00
|
|
|
|
2016-02-21 14:28:40 -05:00
|
|
|
-- Save the database before exiting.
|
2016-03-08 08:41:45 -05:00
|
|
|
save_data(bot.username..'.db', database)
|
2015-07-19 16:46:06 -04:00
|
|
|
print('Halted.')
|