2016-08-23 06:16:32 +02:00
|
|
|
--[[
|
|
|
|
bot.lua
|
|
|
|
The heart and sole of otouto, ie the init and main loop.
|
|
|
|
|
|
|
|
Copyright 2016 topkecleon <drew@otou.to>
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-23 06:16:32 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Affero General Public License version 3 as
|
|
|
|
published by the Free Software Foundation.
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-23 06:16:32 +02:00
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
]]--
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local bot = {}
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-08-24 15:38:29 +02:00
|
|
|
bot.version = '2.2.7'
|
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-09-04 13:19:52 +02:00
|
|
|
assert(config.bot_api_key, 'Dein Bot-Token ist nicht in der Config gesetzt!')
|
2016-08-23 06:16:32 +02:00
|
|
|
bindings = require('otouto.bindings').init(config.bot_api_key)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities = require('otouto.utilities')
|
2016-06-11 14:46:41 +02:00
|
|
|
cred_data = load_cred()
|
2016-04-29 06:36:35 +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...')
|
2016-08-24 15:38:29 +02:00
|
|
|
self.info = bindings.getMe()
|
2016-05-21 02:47:13 +02:00
|
|
|
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
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
|
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-23 21:34:54 +02:00
|
|
|
-- print('loading plugin',v)
|
2016-08-07 20:45:51 +02:00
|
|
|
self.plugins[k] = 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-08-14 16:30:06 +02:00
|
|
|
-- Set loop variables
|
|
|
|
self.last_update = self.last_update or 0 -- Update offset.
|
|
|
|
self.last_cron = self.last_cron or os.date('%M') -- Last cron job.
|
|
|
|
self.last_database_save = self.last_database_save or os.date('%H') -- Last db save.
|
2016-04-08 23:12:02 +02:00
|
|
|
self.is_started = true -- and whether or not the bot should be running.
|
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
|
2016-07-13 14:33:52 +02:00
|
|
|
-- vardump(msg)
|
2016-07-05 13:14:22 +02:00
|
|
|
|
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-07-15 21:24:20 +02:00
|
|
|
if msg.reply_to_message then
|
|
|
|
msg.reply_to_message.text = msg.reply_to_message.text or msg.reply_to_message.caption or ''
|
|
|
|
end
|
|
|
|
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
-- Support deep linking.
|
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-07-13 02:01:02 +02:00
|
|
|
if msg.text:match(config.cmd_pat..'([A-Za-z0-9-_-]+)@'..self.info.username) then
|
2016-07-13 02:08:59 +02:00
|
|
|
msg.text = string.gsub(msg.text, config.cmd_pat..'([A-Za-z0-9-_-]+)@'..self.info.username, "/%1")
|
2016-07-13 02:01:02 +02:00
|
|
|
msg.text_lower = msg.text:lower()
|
|
|
|
end
|
2016-08-28 18:12:18 +02:00
|
|
|
|
2016-06-17 20:44:28 +02:00
|
|
|
msg = pre_process_msg(self, msg, config)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
if not msg then return end -- deleted by banning
|
2016-06-18 12:51:13 +02:00
|
|
|
|
2016-08-01 21:51:37 +02:00
|
|
|
if is_service_msg(msg) then
|
|
|
|
msg = service_modify_msg(msg)
|
|
|
|
end
|
|
|
|
|
2016-08-15 00:36:33 +02:00
|
|
|
for _, plugin in ipairs(self.plugins) do
|
|
|
|
match_plugins(self, msg, config, plugin)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2016-07-02 12:35:14 +02:00
|
|
|
function bot:on_callback_receive(callback, msg, config) -- whenever a new callback is received
|
2016-07-02 15:03:54 +02:00
|
|
|
-- remove comments to enable debugging
|
2016-07-02 12:35:14 +02:00
|
|
|
-- vardump(msg)
|
|
|
|
-- vardump(callback)
|
|
|
|
|
2016-09-22 21:40:46 +02:00
|
|
|
if msg.date < os.time() - 3600 then -- Do not process old messages.
|
|
|
|
utilities.answer_callback_query(callback, 'Nachricht älter als eine Stunde, bitte sende den Befehl selbst noch einmal.', true)
|
2016-07-02 12:35:14 +02:00
|
|
|
return
|
|
|
|
end
|
2016-09-22 21:40:46 +02:00
|
|
|
|
|
|
|
if not callback.data:find(':') then
|
|
|
|
utilities.answer_callback_query(callback, 'Ungültiger CallbackQuery: Kein Parameter.')
|
2016-07-02 14:58:04 +02:00
|
|
|
return
|
|
|
|
end
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
|
|
|
|
-- Check if user is blocked
|
|
|
|
local user_id = callback.from.id
|
|
|
|
local chat_id = msg.chat.id
|
|
|
|
if redis:get('blocked:'..user_id) then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if user is banned
|
|
|
|
local banned = redis:get('banned:'..chat_id..':'..user_id)
|
|
|
|
if banned then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if whitelist is enabled and user/chat is whitelisted
|
|
|
|
local whitelist = redis:get('whitelist:enabled')
|
2016-09-08 16:50:42 +02:00
|
|
|
if whitelist and not is_sudo(callback, config) then
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
local hash = 'whitelist:user#id'..user_id
|
|
|
|
local allowed = redis:get(hash) or false
|
|
|
|
if not allowed then
|
|
|
|
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
|
|
|
|
local allowed = redis:get('whitelist:chat#id'.. chat_id)
|
|
|
|
if not allowed then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-22 21:40:46 +02:00
|
|
|
|
2016-07-02 14:58:04 +02:00
|
|
|
local called_plugin = callback.data:match('(.*):.*')
|
|
|
|
local param = callback.data:sub(callback.data:find(':')+1)
|
|
|
|
|
|
|
|
print('Callback Query "'..param..'" für Plugin "'..called_plugin..'" ausgelöst von '..callback.from.first_name..' ('..callback.from.id..')')
|
2016-07-02 12:35:14 +02:00
|
|
|
|
|
|
|
msg = utilities.enrich_message(msg)
|
2016-07-05 13:14:22 +02:00
|
|
|
|
2016-08-07 20:45:51 +02:00
|
|
|
for n=1, #self.plugins do
|
|
|
|
local plugin = self.plugins[n]
|
2016-07-02 15:03:54 +02:00
|
|
|
if plugin.name == called_plugin then
|
2016-09-08 16:50:42 +02:00
|
|
|
if is_plugin_disabled_on_chat(plugin.name, msg) then utilities.answer_callback_query(callback, 'Plugin wurde in diesem Chat deaktiviert.') return end
|
2016-09-22 21:40:46 +02:00
|
|
|
if plugin.callback then
|
|
|
|
plugin:callback(callback, msg, self, config, param)
|
|
|
|
return
|
|
|
|
else
|
|
|
|
utilities.answer_callback_query(callback, 'Ungültiger CallbackQuery: Plugin unterstützt keine Callbacks.')
|
|
|
|
return
|
|
|
|
end
|
2016-07-02 12:35:14 +02:00
|
|
|
end
|
2016-07-02 15:03:54 +02:00
|
|
|
end
|
2016-09-08 16:50:42 +02:00
|
|
|
|
|
|
|
utilities.answer_callback_query(callback, 'Ungültiger CallbackQuery: Kein Plugin gefunden.')
|
2016-07-02 12:35:14 +02:00
|
|
|
end
|
|
|
|
|
2016-07-13 01:00:32 +02:00
|
|
|
-- NOTE: To enable InlineQuerys, send /setinline to @BotFather
|
|
|
|
function bot:process_inline_query(inline_query, config) -- When an inline query is received
|
|
|
|
-- remove comment to enable debugging
|
|
|
|
-- vardump(inline_query)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
|
|
|
|
-- PLEASE READ: Blocking every single InlineQuery IS NOT POSSIBLE!
|
|
|
|
-- When the request is cached, the user can still send this query
|
|
|
|
-- but he WON'T be able to make new requests.
|
|
|
|
local user_id = inline_query.from.id
|
|
|
|
if redis:get('blocked:'..user_id) then
|
2016-08-24 15:38:29 +02:00
|
|
|
abort_inline_query(inline_query)
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
return
|
|
|
|
end
|
2016-07-13 01:00:32 +02:00
|
|
|
|
|
|
|
if not config.enable_inline_for_everyone then
|
|
|
|
local is_whitelisted = redis:get('whitelist:user#id'..inline_query.from.id)
|
2016-08-24 15:38:29 +02:00
|
|
|
if not is_whitelisted then abort_inline_query(inline_query) return end
|
2016-07-13 01:00:32 +02:00
|
|
|
end
|
|
|
|
|
2016-09-22 21:40:46 +02:00
|
|
|
inline_query.query = inline_query.query:gsub('"', '\\"')
|
2016-08-07 20:45:51 +02:00
|
|
|
|
2016-09-08 01:48:14 +02:00
|
|
|
if string.len(inline_query.query) > 200 then
|
|
|
|
abort_inline_query(inline_query)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-07 20:45:51 +02:00
|
|
|
for n=1, #self.plugins do
|
|
|
|
local plugin = self.plugins[n]
|
2016-07-13 01:00:32 +02:00
|
|
|
match_inline_plugins(self, inline_query, config, plugin)
|
|
|
|
end
|
- Banhammer und Bann-System stark überararbeitet:
- /block und /block delete hinzugefügt, um User nur vom Bot zu blocken, nicht aus dem Chat zu entfernen
- /leave hinzugefügt: Bot verlässt die Gruppe
- Geblockte, gebannte oder nicht gewhitelistete User können keine Callbacks mehr benutzen
- Geblockte User können keine InlineQuerys mehr benutzen
- Admin kann direkt auf Nachrichten mit /whitelist, /whitelist delete, /block, /block delete, /ban, /ban delete und /kick antworten, um diee Aktion auszuführen
- Anpassung des InlineQuery-Systems, um falsches Caching für alle zu verhindern
- Wikipedia: Setze Caching-Zeit auf eine Stunde für InlineQuerys
2016-08-11 14:08:21 +02:00
|
|
|
|
|
|
|
-- Stop the spinning circle
|
2016-08-24 15:38:29 +02:00
|
|
|
abort_inline_query(inline_query)
|
2016-07-13 01:00:32 +02:00
|
|
|
end
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function bot:run(config)
|
2016-08-14 16:30:06 +02:00
|
|
|
bot.init(self, config)
|
2016-08-15 00:36:33 +02:00
|
|
|
|
2016-08-14 16:30:06 +02:00
|
|
|
while self.is_started do
|
|
|
|
-- Update loop
|
2016-08-24 15:38:29 +02:00
|
|
|
local res = bindings.getUpdates{ timeout = 20, offset = self.last_update+1 }
|
2016-05-22 09:03:50 +02:00
|
|
|
if res then
|
2016-08-14 16:30:06 +02:00
|
|
|
-- Iterate over every new message.
|
|
|
|
for n=1, #res.result do
|
2016-08-07 20:45:51 +02:00
|
|
|
local v = res.result[n]
|
2016-05-22 09:03:50 +02:00
|
|
|
self.last_update = v.update_id
|
2016-07-12 23:04:39 +02:00
|
|
|
if v.inline_query then
|
2016-09-22 21:40:46 +02:00
|
|
|
bot.process_inline_query(self, v.inline_query, config)
|
2016-07-12 23:04:39 +02:00
|
|
|
elseif v.callback_query then
|
2016-07-02 12:35:14 +02:00
|
|
|
bot.on_callback_receive(self, v.callback_query, v.callback_query.message, config)
|
2016-06-27 15:44:11 +02:00
|
|
|
elseif 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-08-15 00:36:33 +02:00
|
|
|
-- Run cron jobs every minute.
|
2016-08-14 16:30:06 +02:00
|
|
|
if self.last_cron ~= os.date('%M') then
|
2016-04-14 05:48:20 +02:00
|
|
|
self.last_cron = os.date('%M')
|
2016-08-07 20:45:51 +02:00
|
|
|
for n=1, #self.plugins do
|
|
|
|
local v = self.plugins[n]
|
2016-04-14 05:48:20 +02:00
|
|
|
if v.cron then -- Call each plugin's cron function, if it has one.
|
2016-08-24 15:54:16 +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-08-24 15:54:16 +02:00
|
|
|
utilities.handle_exception(self, err, 'CRON: ' .. n, config.log_chat)
|
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
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
if self.last_database_save ~= os.date('%H') then
|
|
|
|
utilities.save_data(self.info.username..'.db', self.database) -- Save the database.
|
|
|
|
self.last_database_save = os.date('%H')
|
|
|
|
end
|
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-08-07 20:45:51 +02:00
|
|
|
for n=1, #self.plugins do
|
|
|
|
local plugin = self.plugins[n]
|
2016-08-28 18:12:18 +02:00
|
|
|
if plugin.pre_process and msg then
|
|
|
|
if not is_plugin_disabled_on_chat(plugin.name, msg, true) then
|
|
|
|
-- print('Preprocess '..plugin.name) -- remove comment to restore old behaviour
|
|
|
|
new_msg = plugin:pre_process(msg, config)
|
|
|
|
if not new_msg then return end -- Message was deleted
|
|
|
|
end
|
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-07-13 01:00:32 +02:00
|
|
|
function match_inline_plugins(self, inline_query, config, plugin)
|
2016-08-07 20:45:51 +02:00
|
|
|
local match_table = plugin.inline_triggers or {}
|
|
|
|
for n=1, #match_table do
|
|
|
|
local trigger = plugin.inline_triggers[n]
|
2016-07-13 01:00:32 +02:00
|
|
|
if string.match(string.lower(inline_query.query), trigger) then
|
|
|
|
local success, result = pcall(function()
|
|
|
|
for k, pattern in pairs(plugin.inline_triggers) do
|
|
|
|
matches = match_pattern(pattern, inline_query.query)
|
|
|
|
if matches then
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
2016-07-13 01:21:50 +02:00
|
|
|
print('Inline: '..plugin.name..' triggered')
|
2016-07-13 01:00:32 +02:00
|
|
|
return plugin.inline_callback(self, inline_query, config, matches)
|
|
|
|
end)
|
2016-08-05 15:06:15 +02:00
|
|
|
end
|
2016-07-13 01:00:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-19 21:53:24 +02:00
|
|
|
function match_plugins(self, msg, config, plugin)
|
2016-08-15 00:36:33 +02:00
|
|
|
local match_table = plugin.triggers or {}
|
2016-08-07 20:45:51 +02:00
|
|
|
for n=1, #match_table do
|
|
|
|
local trigger = plugin.triggers[n]
|
2016-06-19 21:53:24 +02:00
|
|
|
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
|
2016-08-07 20:45:51 +02:00
|
|
|
local pattern = plugin.triggers[n]
|
|
|
|
local matches = match_pattern(pattern, msg.text)
|
2016-08-14 00:37:09 +02:00
|
|
|
if matches then
|
|
|
|
print('msg matches: ', pattern, ' for "'..plugin.name..'"')
|
|
|
|
return plugin.action(self, msg, config, matches)
|
|
|
|
end
|
2016-06-19 21:53:24 +02:00
|
|
|
end)
|
|
|
|
if not success then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.handle_exception(self, result, msg.from.id .. ': ' .. msg.text, config.log_chat)
|
2016-08-09 00:49:54 +02:00
|
|
|
return
|
2016-07-26 16:39:18 +02:00
|
|
|
end
|
2016-08-14 00:37:09 +02:00
|
|
|
-- if one pattern matches, end
|
|
|
|
return
|
2016-06-19 21:53:24 +02:00
|
|
|
end
|
2016-06-18 12:51:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-28 18:12:18 +02:00
|
|
|
function is_plugin_disabled_on_chat(plugin_name, msg, silent)
|
2016-06-18 12:51:13 +02:00
|
|
|
local hash = get_redis_hash(msg, 'disabled_plugins')
|
|
|
|
local disabled = redis:hget(hash, plugin_name)
|
|
|
|
|
|
|
|
-- Plugin is disabled
|
|
|
|
if disabled == 'true' then
|
2016-08-28 18:12:18 +02:00
|
|
|
if not silent then
|
|
|
|
print('Plugin '..plugin_name..' ist in diesem Chat deaktiviert')
|
|
|
|
end
|
2016-06-18 12:51:13 +02:00
|
|
|
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',
|
|
|
|
'about',
|
2016-06-21 16:20:56 +02:00
|
|
|
'id',
|
2016-08-24 15:38:29 +02:00
|
|
|
'post_photo',
|
|
|
|
'images',
|
|
|
|
'media',
|
|
|
|
'service_migrate_to_supergroup',
|
2016-08-19 14:08:54 +02:00
|
|
|
'creds',
|
2016-06-18 12:51:13 +02:00
|
|
|
'echo',
|
2016-08-12 01:34:22 +02:00
|
|
|
'currency',
|
2016-06-18 12:51:13 +02:00
|
|
|
'banhammer',
|
|
|
|
'plugins',
|
2016-08-24 15:38:29 +02:00
|
|
|
'settings',
|
2016-08-12 01:34:22 +02:00
|
|
|
'help'
|
2016-06-18 12:51:13 +02:00
|
|
|
}
|
|
|
|
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-07-05 13:14:22 +02:00
|
|
|
return bot
|