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.
|
2016-06-11 14:46:41 +02:00
|
|
|
local redis = (loadfile "./otouto/redis.lua")()
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-13 22:18:36 +02:00
|
|
|
bot.version = '2.0'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function bot:init(config) -- The function run when the bot is started or reloaded.
|
2016-04-08 23:12:02 +02:00
|
|
|
|
2016-06-07 06:31:34 +02:00
|
|
|
bindings = require('otouto.bindings')
|
|
|
|
utilities = require('otouto.utilities')
|
2016-06-11 14:46:41 +02:00
|
|
|
redis = (loadfile "./otouto/redis.lua")()
|
|
|
|
cred_data = load_cred()
|
2016-04-29 06:36:35 +02:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
assert(
|
2016-05-27 02:26:30 +02:00
|
|
|
config.bot_api_key and config.bot_api_key ~= '',
|
2016-06-18 12:51:13 +02:00
|
|
|
'You did not set your bot token in the config!'
|
2016-05-29 19:08:39 +02:00
|
|
|
)
|
2016-05-27 02:26:30 +02:00
|
|
|
self.BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key .. '/'
|
2015-07-10 09:52:22 +02:00
|
|
|
|
2016-01-12 11:22:28 +01:00
|
|
|
-- Fetch bot information. Try until it succeeds.
|
2016-05-21 02:47:13 +02:00
|
|
|
repeat
|
|
|
|
print('Fetching bot information...')
|
|
|
|
self.info = bindings.getMe(self)
|
|
|
|
until self.info
|
2016-04-08 23:12:02 +02:00
|
|
|
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-06-07 05:13:26 +02:00
|
|
|
self.database.users = self.database.users or {} -- Table to cache userdata.
|
|
|
|
self.database.users[tostring(self.info.id)] = self.info
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
self.plugins = {} -- Load plugins.
|
2016-06-18 12:51:13 +02:00
|
|
|
enabled_plugins = load_plugins()
|
|
|
|
for k,v in pairs(enabled_plugins) do
|
2016-06-07 06:31:34 +02:00
|
|
|
local p = require('otouto.plugins.'..v)
|
2016-06-18 12:51:13 +02:00
|
|
|
print('loading plugin',v)
|
2016-04-08 23:12:02 +02:00
|
|
|
table.insert(self.plugins, p)
|
2016-06-18 12:51:13 +02:00
|
|
|
self.plugins[k].name = v
|
2016-05-27 02:26:30 +02:00
|
|
|
if p.init then p.init(self, config) end
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
2016-06-18 16:41:21 +02:00
|
|
|
|
2016-06-18 12:51:13 +02:00
|
|
|
print('Bot started successfully as:\n@' .. self.info.username .. ', AKA ' .. self.info.first_name ..' ('..self.info.id..')')
|
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.
|
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-05-27 02:26:30 +02:00
|
|
|
function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received.
|
2016-06-16 20:56:37 +02:00
|
|
|
-- remove comment to enable debugging
|
|
|
|
-- vardump(msg)
|
2016-05-15 14:22:31 +02:00
|
|
|
-- Cache user info for those involved.
|
|
|
|
utilities.create_user_entry(self, msg.from)
|
|
|
|
if msg.forward_from and msg.forward_from.id ~= msg.from.id then
|
|
|
|
utilities.create_user_entry(self, msg.forward_from)
|
|
|
|
elseif msg.reply_to_message and msg.reply_to_message.from.id ~= msg.from.id then
|
|
|
|
utilities.create_user_entry(self, msg.reply_to_message.from)
|
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-06-15 20:10:30 +02:00
|
|
|
|
2016-04-03 21:18:25 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
if msg.text:match('^'..config.cmd_pat..'start .+') then
|
|
|
|
msg.text = config.cmd_pat .. 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-06-17 20:44:28 +02:00
|
|
|
-- gsub out user name if multiple bots are in the same group
|
2016-06-18 13:42:30 +02:00
|
|
|
msg.text = string.gsub(msg.text, '@'..self.info.username, "")
|
|
|
|
msg.text_lower = string.gsub(msg.text, '@'..string.lower(self.info.username), "")
|
2016-06-17 20:44:28 +02:00
|
|
|
|
|
|
|
msg = pre_process_msg(self, msg, config)
|
2016-06-18 12:51:13 +02:00
|
|
|
|
|
|
|
match_plugins(self, msg, config)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function bot:run(config)
|
2016-05-27 02:26:30 +02:00
|
|
|
bot.init(self, config) -- Actually start the script.
|
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-05-29 19:08:39 +02:00
|
|
|
local res = bindings.getUpdates(self, { timeout=20, offset = self.last_update+1 } )
|
2016-05-22 09:03:50 +02:00
|
|
|
if res then
|
|
|
|
for _,v in ipairs(res.result) do -- Go through every new message.
|
|
|
|
self.last_update = v.update_id
|
|
|
|
if v.message then
|
2016-05-27 02:26:30 +02:00
|
|
|
bot.on_msg_receive(self, v.message, config)
|
2016-04-14 05:48:20 +02:00
|
|
|
end
|
2016-04-08 23:12:02 +02:00
|
|
|
end
|
2016-05-22 09:03:50 +02:00
|
|
|
else
|
2016-06-07 05:13:26 +02:00
|
|
|
print('Connection error while fetching updates.')
|
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.
|
2016-05-27 05:28:44 +02:00
|
|
|
local result, err = pcall(function() v.cron(self, config) end)
|
2016-05-29 19:08:39 +02:00
|
|
|
if not result then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.handle_exception(self, err, 'CRON: ' .. i, config)
|
2016-04-14 05:48:20 +02:00
|
|
|
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-06-15 20:10:30 +02:00
|
|
|
-- Apply plugin.pre_process function
|
2016-06-17 20:44:28 +02:00
|
|
|
function pre_process_msg(self, msg, config)
|
2016-06-15 20:10:30 +02:00
|
|
|
for number,plugin in ipairs(self.plugins) do
|
|
|
|
if plugin.pre_process and msg then
|
2016-06-15 21:31:15 +02:00
|
|
|
-- print('Preprocess #'..number) -- remove comment to restore old behaviour
|
2016-06-17 20:44:28 +02:00
|
|
|
new_msg = plugin:pre_process(msg, self, config)
|
2016-06-15 20:10:30 +02:00
|
|
|
end
|
|
|
|
end
|
2016-06-17 20:44:28 +02:00
|
|
|
return new_msg
|
2016-06-15 20:10:30 +02:00
|
|
|
end
|
|
|
|
|
2016-06-18 12:51:13 +02:00
|
|
|
function match_plugins(self, msg, config)
|
|
|
|
for _, plugin in ipairs(self.plugins) do
|
|
|
|
for _, trigger in pairs(plugin.triggers) do
|
|
|
|
if string.match(msg.text_lower, trigger) then
|
|
|
|
-- Check if Plugin is disabled
|
|
|
|
if is_plugin_disabled_on_chat(plugin.name, msg) then return end
|
|
|
|
local success, result = pcall(function()
|
|
|
|
-- trying to port matches to otouto
|
|
|
|
for k, pattern in pairs(plugin.triggers) do
|
|
|
|
matches = match_pattern(pattern, msg.text)
|
|
|
|
if matches then
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return plugin.action(self, msg, config, matches)
|
|
|
|
end)
|
|
|
|
if not success then
|
|
|
|
-- If the plugin has an error message, send it. If it does
|
|
|
|
-- not, use the generic one specified in config. If it's set
|
|
|
|
-- to false, do nothing.
|
|
|
|
if plugin.error then
|
|
|
|
utilities.send_reply(self, msg, plugin.error)
|
|
|
|
elseif plugin.error == nil then
|
|
|
|
utilities.send_reply(self, msg, config.errors.generic, true)
|
|
|
|
end
|
|
|
|
utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text, config)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- If the action returns a table, make that table the new msg.
|
|
|
|
if type(result) == 'table' then
|
|
|
|
msg = result
|
|
|
|
-- If the action returns true, continue.
|
|
|
|
elseif result ~= true then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function is_plugin_disabled_on_chat(plugin_name, msg)
|
|
|
|
local hash = get_redis_hash(msg, 'disabled_plugins')
|
|
|
|
local disabled = redis:hget(hash, plugin_name)
|
|
|
|
|
|
|
|
-- Plugin is disabled
|
|
|
|
if disabled == 'true' then
|
|
|
|
print('Plugin '..plugin_name..' ist in diesem Chat deaktiviert')
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function load_plugins()
|
|
|
|
enabled_plugins = redis:smembers('telegram:enabled_plugins')
|
|
|
|
if not enabled_plugins[1] then
|
|
|
|
create_plugin_set()
|
|
|
|
end
|
|
|
|
return enabled_plugins
|
|
|
|
end
|
|
|
|
|
|
|
|
-- create plugin set if it doesn't exist
|
|
|
|
function create_plugin_set()
|
|
|
|
enabled_plugins = {
|
|
|
|
'control',
|
|
|
|
'blacklist',
|
|
|
|
'about',
|
|
|
|
'ping',
|
|
|
|
'whoami',
|
|
|
|
'nick',
|
|
|
|
'echo',
|
|
|
|
'imgblacklist',
|
|
|
|
'gImages',
|
|
|
|
'gSearch',
|
|
|
|
'wikipedia',
|
|
|
|
'hackernews',
|
|
|
|
'imdb',
|
|
|
|
'calc',
|
|
|
|
'urbandictionary',
|
|
|
|
'time',
|
|
|
|
'reddit',
|
|
|
|
'xkcd',
|
|
|
|
'slap',
|
|
|
|
'commit',
|
|
|
|
'pun',
|
|
|
|
'currency',
|
|
|
|
'shout',
|
|
|
|
'set',
|
|
|
|
'get',
|
|
|
|
'patterns',
|
|
|
|
'9gag',
|
|
|
|
'shell',
|
|
|
|
'adfly',
|
|
|
|
'twitter',
|
|
|
|
'rss',
|
|
|
|
'remind',
|
|
|
|
'youtube',
|
|
|
|
'youtube_search',
|
|
|
|
'youtube_channel',
|
|
|
|
'youtube_playlist',
|
|
|
|
'tagesschau_eil',
|
|
|
|
'twitter_send',
|
|
|
|
'respond',
|
|
|
|
'roll',
|
|
|
|
'quotes',
|
|
|
|
'pasteee',
|
|
|
|
'images',
|
|
|
|
'media',
|
|
|
|
'location_manager',
|
|
|
|
'creds',
|
|
|
|
'weather',
|
|
|
|
'forecast',
|
|
|
|
'expand',
|
|
|
|
'facebook',
|
|
|
|
'github',
|
|
|
|
'bitly',
|
|
|
|
'app_store',
|
|
|
|
'bitly_create',
|
|
|
|
'br',
|
|
|
|
'heise',
|
|
|
|
'tagesschau',
|
|
|
|
'wiimmfi',
|
|
|
|
'wikia',
|
|
|
|
'afk',
|
|
|
|
'stats',
|
|
|
|
'btc',
|
|
|
|
'cats',
|
|
|
|
'cleverbot',
|
|
|
|
'imgur',
|
|
|
|
'banhammer',
|
|
|
|
'channels',
|
|
|
|
'plugins',
|
|
|
|
'help',
|
|
|
|
'greetings'
|
|
|
|
}
|
|
|
|
print ('enabling a few plugins - saving to redis set telegram:enabled_plugins')
|
|
|
|
for _,plugin in pairs(enabled_plugins) do
|
|
|
|
redis:sadd("telegram:enabled_plugins", plugin)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function load_cred()
|
|
|
|
if redis:exists("telegram:credentials") == false then
|
|
|
|
-- If credentials hash doesnt exists
|
|
|
|
print ("Created new credentials hash: telegram:credentials")
|
|
|
|
create_cred()
|
|
|
|
end
|
|
|
|
return redis:hgetall("telegram:credentials")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- create credentials hash with redis
|
|
|
|
function create_cred()
|
|
|
|
cred = {
|
|
|
|
bitly_access_token = "",
|
|
|
|
cloudinary_apikey = "",
|
|
|
|
cloudinary_api_secret = "",
|
|
|
|
cloudinary_public_id = "",
|
|
|
|
derpibooru_apikey = "",
|
|
|
|
fb_access_token = "",
|
|
|
|
flickr_apikey = "",
|
|
|
|
ftp_site = "",
|
|
|
|
ftp_username = "",
|
|
|
|
ftp_password = "",
|
|
|
|
gender_apikey = "",
|
|
|
|
golem_apikey = "",
|
|
|
|
google_apikey = "",
|
|
|
|
google_cse_id = "",
|
|
|
|
gitlab_private_token = "",
|
|
|
|
gitlab_project_id = "",
|
|
|
|
instagram_access_token = "",
|
|
|
|
lyricsnmusic_apikey = "",
|
|
|
|
mal_username = "",
|
|
|
|
mal_pw = "",
|
|
|
|
neutrino_userid = "",
|
|
|
|
neutrino_apikey = "",
|
|
|
|
owm_apikey = "",
|
|
|
|
page2images_restkey = "",
|
|
|
|
soundcloud_client_id = "",
|
|
|
|
tw_consumer_key = "",
|
|
|
|
tw_consumer_secret = "",
|
|
|
|
tw_access_token = "",
|
|
|
|
tw_access_token_secret = "",
|
|
|
|
x_mashape_key = "",
|
|
|
|
yandex_translate_apikey = "",
|
|
|
|
yandex_rich_content_apikey = "",
|
|
|
|
yourls_site_url = "",
|
|
|
|
yourls_signature_token = ""
|
|
|
|
}
|
|
|
|
redis:hmset("telegram:credentials", cred)
|
|
|
|
print ('saved credentials into reds hash telegram:credentials')
|
|
|
|
end
|
|
|
|
|
2016-04-14 05:48:20 +02:00
|
|
|
return bot
|