Übernehme Änderungen von Brawl345/Brawlbot-v2

This commit is contained in:
Akamaru 2016-08-03 19:05:05 +02:00
parent 5856804891
commit bb0fa0e5a5
15 changed files with 317 additions and 180 deletions

View File

@ -29,7 +29,7 @@ Du benötigst **Lua 5.2+**, eine aktive **Redis-Instanz** und die folgenden **Lu
* luasocket * luasocket
* luasec * luasec
* multipart-post * multipart-post
* dkjson * lua-cjson
* lpeg * lpeg
* redis-lua * redis-lua
* fakeredis * fakeredis

View File

@ -93,6 +93,10 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
end end
msg = pre_process_msg(self, msg, config) msg = pre_process_msg(self, msg, config)
if is_service_msg(msg) then
msg = service_modify_msg(msg)
end
for _, plugin in ipairs(self.plugins) do for _, plugin in ipairs(self.plugins) do
match_plugins(self, msg, config, plugin) match_plugins(self, msg, config, plugin)
end end

View File

@ -84,9 +84,9 @@ function afk:pre_process(msg, self)
redis:hset(hash, 'afk_text', false) redis:hset(hash, 'afk_text', false)
local afk_text = afk_text:gsub("%*","") local afk_text = afk_text:gsub("%*","")
local afk_text = afk_text:gsub("_","") local afk_text = afk_text:gsub("_","")
utilities.send_message(self, msg.chat.id, user_name..' ist wieder da! (war: *'..afk_text..'* für '..duration..')', true, nil, true) utilities.send_reply(self, msg, user_name..' ist wieder da! (war: *'..afk_text..'* für '..duration..')', true, '{"hide_keyboard":true,"selective":true}')
else else
utilities.send_message(self, msg.chat.id, user_name..' ist wieder da! (war '..duration..' weg)') utilities.send_reply(self, msg, user_name..' ist wieder da! (war '..duration..' weg)', false, '{"hide_keyboard":true,"selective":true}')
end end
end end
@ -103,8 +103,15 @@ function afk:action(msg)
local chat_id = msg.chat.id local chat_id = msg.chat.id
local user_name = get_name(msg) local user_name = get_name(msg)
local timestamp = msg.date local timestamp = msg.date
local uhash = 'user:'..msg.from.id
local show_afk_keyboard = redis:hget(uhash, 'afk_keyboard')
if show_afk_keyboard == 'true' then
keyboard = '{"keyboard":[[{"text":"Wieder da."}]], "one_time_keyboard":true, "selective":true, "resize_keyboard":true}'
else
keyboard = nil
end
utilities.send_reply(self, msg, afk:switch_afk(user_name, user_id, chat_id, timestamp, matches[2])) utilities.send_reply(self, msg, afk:switch_afk(user_name, user_id, chat_id, timestamp, matches[2]), false, keyboard)
end end
return afk return afk

View File

@ -13,7 +13,7 @@ function echo:init(config)
end end
function echo:inline_callback(inline_query, config, matches) function echo:inline_callback(inline_query, config, matches)
local text = matches[1] local text = matches[1]
local results = '[' local results = '['
-- enable custom markdown button -- enable custom markdown button

View File

@ -122,9 +122,17 @@ function games:send_game_data(game_id, self, msg)
return return
end end
function games:action(msg, config)
local game = utilities.input(msg.text)
if not game then
if msg.reply_to_message and msg.reply_to_message.text then
game = msg.reply_to_message.text
else
utilities.send_message(self, msg.chat.id, fun.doc, true, msg.message_id, true)
return
end
end
function games:action(msg, config, matches)
local game = URL.escape(matches[1])
local game_id = games:get_game_id(game) local game_id = games:get_game_id(game)
if not game_id then if not game_id then
utilities.send_reply(self, msg, 'Spiel nicht gefunden!') utilities.send_reply(self, msg, 'Spiel nicht gefunden!')

View File

@ -104,4 +104,4 @@ end
function media_download:action(msg) function media_download:action(msg)
end end
return media_download return media_download

View File

@ -1,49 +0,0 @@
local leave_group = {}
leave_group.triggers = {
'/nil'
}
local report_to_admin = true -- set to false to not be notified, when Bot leaves groups without you
function leave_group:check_for_admin(msg, self, config)
local result = bindings.request(self, 'getChatMember', {
chat_id = msg.chat.id,
user_id = config.admin
} )
if not result.ok then
print('Konnte nicht prüfen, ob Admin in Gruppe ist. Verlasse Gruppe.')
return false
end
if result.result.status ~= "member" and result.result.status ~= "administrator" and result.result.status ~= "creator" then
return false
else
return true
end
end
function leave_group:pre_process(msg, self, config)
if msg.group_chat_created or msg.new_chat_member then
local admin_in_group = leave_group:check_for_admin(msg, self, config)
if not admin_in_group then
print('Admin ist nicht in der Gruppe, verlasse sie deshalb...')
utilities.send_reply(self, msg, 'Dieser Bot wurde in eine fremde Gruppe hinzugefügt. Dies wird gemeldet!\nThis bot was added to foreign group. This incident will be reported!')
local result = bindings.request(self, 'leaveChat', {
chat_id = msg.chat.id
} )
local chat_name = msg.chat.title
local chat_id = msg.chat.id
local from = msg.from.name
local from_id = msg.from.id
if report_to_admin then
utilities.send_message(self, config.admin, '#WARNUNG: Bot wurde in fremde Gruppe hinzugefügt:\nGruppenname: '..chat_name..' ('..chat_id..')\nHinzugefügt von: '..from..' ('..from_id..')')
end
end
end
return msg
end
function leave_group:action(msg)
end
return leave_group

View File

@ -11,54 +11,56 @@ function remind:init(config)
end end
function remind:action(msg) function remind:action(msg)
-- Ensure there are arguments. If not, send doc. -- Ensure there are arguments. If not, send doc.
local input = utilities.input(msg.text) local input = utilities.input(msg.text)
if not input then if not input then
utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true) utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true)
return return
end end
-- Ensure first arg is a number. If not, send doc.
local duration = utilities.get_word(input, 1) -- Ensure first arg is a number. If not, send doc.
if not tonumber(duration) then local duration = utilities.get_word(input, 1)
utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true) if not tonumber(duration) then
return utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true)
end return
-- Duration must be between one minute and one year (approximately). end
duration = tonumber(duration)
if duration < 1 then -- Duration must be between one minute and one day (approximately).
duration = 1 duration = tonumber(duration)
elseif duration > 526000 then if duration < 1 then
duration = 526000 duration = 1
end elseif duration > 1440 then
-- Ensure there is a second arg. duration = 1440
local message = utilities.input(input) end
if not message then
utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true) -- Ensure there is a second arg.
return local message = utilities.input(input)
end if not message then
-- Make a database entry for the group/user if one does not exist. utilities.send_message(self, msg.chat.id, remind.doc, true, msg.message_id, true)
self.database.reminders[msg.chat.id_str] = self.database.reminders[msg.chat.id_str] or {} return
-- Limit group reminders to 10 and private reminders to 50. end
if msg.chat.type ~= 'private' and utilities.table_size(self.database.reminders[msg.chat.id_str]) > 9 then
utilities.send_reply(self, msg, 'Diese Gruppe hat schon zehn Erinnerungen!') -- Make a database entry for the group/user if one does not exist.
return self.database.reminders[msg.chat.id_str] = self.database.reminders[msg.chat.id_str] or {}
elseif msg.chat.type == 'private' and utilities.table_size(self.database.reminders[msg.chat.id_str]) > 49 then -- Limit group reminders to 10 and private reminders to 50.
utilities.send_reply(msg, 'Du hast schon 50 Erinnerungen!') if msg.chat.type ~= 'private' and utilities.table_size(self.database.reminders[msg.chat.id_str]) > 9 then
return utilities.send_reply(self, msg, 'Diese Gruppe hat schon zehn Erinnerungen!')
end return
-- Put together the reminder with the expiration, message, and message to reply to. elseif msg.chat.type == 'private' and utilities.table_size(self.database.reminders[msg.chat.id_str]) > 49 then
local reminder = { utilities.send_reply(msg, 'Du hast schon 50 Erinnerungen!')
time = os.time() + duration * 60, return
message = message end
}
table.insert(self.database.reminders[msg.chat.id_str], reminder) -- Put together the reminder with the expiration, message, and message to reply to.
local output = 'Ich werde dich in ' .. duration local timestamp = os.time() + duration * 60
if duration == 1 then local reminder = {
output = output .. ' Minute erinnern!' time = timestamp,
else message = message
output = output .. ' Minuten erinnern!' }
end table.insert(self.database.reminders[msg.chat.id_str], reminder)
utilities.send_reply(self, msg, output) local human_readable_time = convert_timestamp(timestamp, '%H:%M:%S')
local output = 'Ich werde dich um *'..human_readable_time..' Uhr* erinnern.'
utilities.send_reply(self, msg, output, true)
end end
function remind:cron() function remind:cron()

View File

@ -1,7 +1,8 @@
local entergroup = {} local entergroup = {}
entergroup.triggers = { entergroup.triggers = {
'/nil' '^//tgservice (new_chat_member)$',
'^//tgservice (left_chat_member)$'
} }
function entergroup:chat_new_user(msg, self) function entergroup:chat_new_user(msg, self)
@ -38,17 +39,14 @@ function entergroup:chat_del_user(msg, self)
utilities.send_reply(self, msg, text, true) utilities.send_reply(self, msg, text, true)
end end
function entergroup:pre_process(msg, self) function entergroup:action(msg, config, matches)
if msg.new_chat_member then if not is_service_msg(msg) then return end -- Bad attempt at trolling!
if matches[1] == 'new_chat_member' then
entergroup:chat_new_user(msg, self) entergroup:chat_new_user(msg, self)
elseif msg.left_chat_member then elseif matches[1] == 'left_chat_member'then
entergroup:chat_del_user(msg, self) entergroup:chat_del_user(msg, self)
end end
return msg
end end
function entergroup:action(msg) return entergroup
end
return entergroup

View File

@ -0,0 +1,45 @@
local leave_group = {}
leave_group.triggers = {
'^//tgservice group_chat_created$',
'^//tgservice supergroup_chat_created$'
}
local report_to_admin = true -- set to false to not be notified, when Bot leaves groups without you
function leave_group:check_for_admin(msg, self, config)
local result = bindings.request(self, 'getChatMember', {
chat_id = msg.chat.id,
user_id = config.admin
} )
if not result.ok then
print('Konnte nicht prüfen, ob Admin in Gruppe ist! Verlasse sie sicherheitshalber...')
return false
end
if result.result.status ~= "member" and result.result.status ~= "administrator" and result.result.status ~= "creator" then
return false
else
return true
end
end
function leave_group:action(msg, config)
if not is_service_msg(msg) then return end -- Bad attempt at trolling!
local admin_in_group = leave_group:check_for_admin(msg, self, config)
if not admin_in_group then
print('Admin ist nicht in der Gruppe, verlasse sie deshalb...')
utilities.send_reply(self, msg, 'Dieser Bot wurde in eine fremde Gruppe hinzugefügt. Dies wird gemeldet!\nThis bot was added to foreign group. This incident will be reported!')
local result = bindings.request(self, 'leaveChat', {
chat_id = msg.chat.id
} )
local chat_name = msg.chat.title
local chat_id = msg.chat.id
local from = msg.from.name
local from_id = msg.from.id
if report_to_admin then
utilities.send_message(self, config.admin, '#WARNUNG: Bot wurde in fremde Gruppe hinzugefügt:\nGruppenname: '..chat_name..' ('..chat_id..')\nHinzugefügt von: '..from..' ('..from_id..')')
end
end
end
return leave_group

54
miku/plugins/settings.lua Normal file
View File

@ -0,0 +1,54 @@
local settings = {}
settings.triggers = {
"^(⚙ [Ee]instellungen)$",
"^(/settings)$",
"^(💤 [Aa][Ff][Kk]%-[Kk]eyboard einschalten)",
"^(💤 [Aa][Ff][Kk]%-[Kk]eyboard ausschalten)",
"^(❌ [Ee]instellungen verstecken)"
}
--[[
[
[ "Top Left", "Top Right" ],
[ "Bottom Left", "Bottom Right" ]
]
]]
function settings:keyboard(user_id)
if redis:hget('user:'..user_id, 'afk_keyboard') == 'true' then
afk_button = '{"text":"💤 AFK-Keyboard ausschalten"}'
else
afk_button = '{"text":"💤 AFK-Keyboard einschalten"}'
end
local hide_settings_button = '{"text":"❌ Einstellungen verstecken"}'
local settings_keyboard = '[['..afk_button..','..hide_settings_button..']]'
return settings_keyboard
end
function settings:action(msg, config, matches)
if msg.chat.type ~= "private" then
return
end
local hash = 'user:'..msg.from.id
if matches[1] == '⚙ Einstellungen' or matches[1] == '/settings' then
utilities.send_reply(self, msg, 'Was möchtest du einstellen?', false, '{"keyboard":'..settings:keyboard(msg.from.id)..', "one_time_keyboard":true, "selective":true, "resize_keyboard":true}')
return
elseif matches[1] == '💤 AFK-Keyboard einschalten' then
redis:hset(hash, 'afk_keyboard', 'true')
utilities.send_reply(self, msg, 'Das AFK-Keyboard wurde erfolgreich *eingeschaltet*.', true, '{"keyboard":'..settings:keyboard(msg.from.id)..', "one_time_keyboard":true, "selective":true, "resize_keyboard":true}')
return
elseif matches[1] == '💤 AFK-Keyboard ausschalten' then
redis:hset(hash, 'afk_keyboard', 'false')
utilities.send_reply(self, msg, 'Das AFK-Keyboard wurde erfolgreich *ausgeschaltet*.', true, '{"keyboard":'..settings:keyboard(msg.from.id)..', "one_time_keyboard":true, "selective":true, "resize_keyboard":true}')
return
elseif matches[1] == '❌ Einstellungen verstecken' then
utilities.send_reply(self, msg, 'Um die Einstellungen wieder einzublenden, führe /settings aus.', true, '{"hide_keyboard":true}')
return
end
end
return settings

View File

@ -80,9 +80,9 @@ end
function stats:pre_process(msg, self) function stats:pre_process(msg, self)
-- Ignore service msg -- Ignore service msg
if msg.service then -- check how Bot API handles service msgs, will update this if is_service_msg(msg) then
print('Service message') print('Service message')
return return msg
end end
if msg.left_chat_member then if msg.left_chat_member then

View File

@ -2,13 +2,17 @@ local surrogate = {}
surrogate.triggers = { surrogate.triggers = {
"^/s (%-%d+) +(.+)$", "^/s (%-%d+) +(.+)$",
"^/s (%d+) +(.+)$" "^/s (%d+) +(.+)$",
"^/s (@[A-Za-z0-9-_-.-._.]+) +(.+)"
} }
function surrogate:action(msg) function surrogate:action(msg, config, matches)
-- Supergroups don't work!? if not is_sudo(msg, config) then
utilities.send_reply(self, msg, config.errors.sudo)
return
end
utilities.send_message(self, matches[1], matches[2], true, nil, true) utilities.send_message(self, matches[1], matches[2], true, nil, true)
return return
end end
return surrogate return surrogate

View File

@ -1,39 +0,0 @@
local Redis = require 'redis' local FakeRedis = require 'fakeredis'
local params = {
host = os.getenv('REDIS_HOST') or '127.0.0.1',
port = tonumber(os.getenv('REDIS_PORT') or 6379)
}
local database = os.getenv('REDIS_DB') local password =
os.getenv('REDIS_PASSWORD') -- Overwrite HGETALL Redis.commands.hgetall
= Redis.command('hgetall', {
response = function(reply, command, ...)
local new_reply = { }
for i = 1, #reply, 2 do new_reply[reply[i]] = reply[i + 1] end
return new_reply
end
})
local redis = nil -- Won't launch an error if fails local ok =
pcall(function()
redis = Redis.connect(params) end) if not ok then
local fake_func = function()
print('\27[31mCan\'t connect with Redis, install/configure
it!\27[39m')
end
fake_func()
fake = FakeRedis.new()
print('\27[31mRedis addr: '..params.host..'\27[39m')
print('\27[31mRedis port: '..params.port..'\27[39m')
redis = setmetatable({fakeredis=true}, {
__index = function(a, b)
if b ~= 'data' and fake[b] then
fake_func(b)
end
return fake[b] or fake_func
end }) else
if password then
redis:auth(password)
end
if database then
redis:select(database)
end end
return redis

View File

@ -8,7 +8,7 @@ http = require('socket.http')
https = require('ssl.https') https = require('ssl.https')
socket = require('socket') socket = require('socket')
URL = require('socket.url') URL = require('socket.url')
json = require('dkjson') json = require('cjson')
serpent = require("serpent") serpent = require("serpent")
bindings = require('miku.bindings') bindings = require('miku.bindings')
redis = (loadfile "./miku/redis.lua")() redis = (loadfile "./miku/redis.lua")()
@ -254,12 +254,12 @@ function utilities.trim(str)
return s return s
end end
-- Retruns true if the string is empty -- Returns true if the string is empty
function string:isempty() function string:isempty()
return self == nil or self == '' return self == nil or self == ''
end end
-- Retruns true if the string is blank -- Returns true if the string is blank
function string:isblank() function string:isblank()
self = self:trim() self = self:trim()
return self:isempty() return self:isempty()
@ -631,6 +631,62 @@ function is_sudo(msg, config)
return var return var
end end
function service_modify_msg(msg)
if msg.new_chat_member then
msg.text = '//tgservice new_chat_member'
msg.text_lower = msg.text
elseif msg.left_chat_member then
msg.text = '//tgservice left_chat_member'
msg.text_lower = msg.text
elseif msg.new_chat_title then
msg.text = '//tgservice new_chat_title'
msg.text_lower = msg.text
elseif msg.new_chat_photo then
msg.text = '//tgservice new_chat_photo'
msg.text_lower = msg.text
elseif msg.group_chat_created then
msg.text = '//tgservice group_chat_created'
msg.text_lower = msg.text
elseif msg.supergroup_chat_created then
msg.text = '//tgservice supergroup_chat_created'
msg.text_lower = msg.text
elseif msg.channel_chat_created then
msg.text = '//tgservice channel_chat_created'
msg.text_lower = msg.text
elseif msg.migrate_to_chat_id then
msg.text = '//tgservice migrate_to_chat_id'
msg.text_lower = msg.text
elseif msg.migrate_from_chat_id then
msg.text = '//tgservice migrate_from_chat_id'
msg.text_lower = msg.text
end
return msg
end
function is_service_msg(msg)
local var = false
if msg.new_chat_member then
var = true
elseif msg.left_chat_member then
var = true
elseif msg.new_chat_title then
var = true
elseif msg.new_chat_photo then
var = true
elseif msg.group_chat_created then
var = true
elseif msg.supergroup_chat_created then
var = true
elseif msg.channel_chat_created then
var = true
elseif msg.migrate_to_chat_id then
var = true
elseif msg.migrate_from_chat_id then
var = true
end
return var
end
function post_petition(url, arguments, headers) function post_petition(url, arguments, headers)
local url, h = string.gsub(url, "http://", "") local url, h = string.gsub(url, "http://", "")
local url, hs = string.gsub(url, "https://", "") local url, hs = string.gsub(url, "https://", "")
@ -794,20 +850,62 @@ function get_http_header(url)
return header, code return header, code
end end
-- checks with If-Modified-Since header, if url has been changed
-- URL and Last-Modified heder are required
function was_modified_since(url, last_modified)
local doer = http
local do_redir = true
if url:match('^https') then
doer = https
do_redir = false
end
local _, code, header = doer.request {
url = url,
method = "HEAD",
redirect = do_redir,
headers = {
["If-Modified-Since"] = last_modified
}
}
if code == 304 then
return false, nil, code
else
if header["last-modified"] then
new_last_modified = header["last-modified"]
elseif header["Last-Modified"] then
new_last_modified = header["Last-Modified"]
end
return true, new_last_modified, code
end
end
-- only url is needed! -- only url is needed!
function get_cached_file(url, file_name, receiver, chat_action, self) function get_cached_file(url, file_name, receiver, chat_action, self)
local hash = 'telegram:cache:sent_file' local hash = 'telegram:cache:sent_file'
local cached_file_id = redis:hget(hash..':'..url, 'file_id') local cached_file_id = redis:hget(hash..':'..url, 'file_id')
local cached_last_modified = redis:hget(hash..':'..url, 'last_modified') local cached_last_modified = redis:hget(hash..':'..url, 'last_modified')
if cached_last_modified then
was_modified, new_last_modified, code = was_modified_since(url, cached_last_modified)
if not was_modified then
print('File wasn\'t modified, skipping download...')
return cached_file_id, nil, true
else
if code ~= 200 then
redis:del(hash..':'..url)
return
end
print('File was modified, redownloading...')
if receiver and chat_action and self then
utilities.send_typing(self, receiver, chat_action)
end
file = download_to_file(url, file_name)
return file, new_last_modified, false
end
end
-- get last-modified and Content-Length header -- get last-modified and Content-Length header
local header, code = get_http_header(url) local header, code = get_http_header(url)
if code ~= 200 then
if cached_file_id then
redis:del(hash..':'..url)
end
return
end
-- file size limit is 50 MB -- file size limit is 50 MB
if header["Content-Length"] then if header["Content-Length"] then
@ -839,14 +937,7 @@ function get_cached_file(url, file_name, receiver, chat_action, self)
end end
if not nocache then if not nocache then
if last_modified == cached_last_modified then file = download_to_file(url, file_name)
print('File not modified and already cached')
nocache = true
file = cached_file_id
else
print('File cached, but modified or not already cached. (Re)downloading...')
file = download_to_file(url, file_name)
end
else else
print('No Last-Modified header!') print('No Last-Modified header!')
file = download_to_file(url, file_name) file = download_to_file(url, file_name)
@ -861,11 +952,23 @@ function makeHumanTime(totalseconds)
local minutes = minutes % 60 local minutes = minutes % 60
local hours = math.floor(totalseconds / 3600) local hours = math.floor(totalseconds / 3600)
if minutes == 00 and hours == 00 then if minutes == 00 and hours == 00 then
return seconds..' Sekunden' if seconds == 1 then
return seconds..' Sekunde'
else
return seconds..' Sekunden'
end
elseif hours == 00 and minutes ~= 00 then elseif hours == 00 and minutes ~= 00 then
return string.format("%02d:%02d", minutes, seconds)..' Minuten' if minutes == 1 then
return string.format("%02d:%02d", minutes, seconds)..' Minute'
else
return string.format("%02d:%02d", minutes, seconds)..' Minuten'
end
elseif hours ~= 00 then elseif hours ~= 00 then
return string.format("%02d:%02d:%02d", hours, minutes, seconds)..' Stunden' if hours == 1 then
return string.format("%02d:%02d:%02d", hours, minutes, seconds)..' Stunde'
else
return string.format("%02d:%02d:%02d", hours, minutes, seconds)..' Stunden'
end
end end
end end