From 31d0c65e91c6f40abab1416831f328ad1b9fc730 Mon Sep 17 00:00:00 2001 From: Brayden Banks Date: Fri, 26 Aug 2016 19:31:12 -0700 Subject: [PATCH 1/6] Record moderation reasoning to log when possible. --- otouto/plugins/administration.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/otouto/plugins/administration.lua b/otouto/plugins/administration.lua index c5646d9..d190add 100644 --- a/otouto/plugins/administration.lua +++ b/otouto/plugins/administration.lua @@ -223,7 +223,7 @@ function administration:get_targets(msg, config) target.name = utilities.build_name(target.first_name, target.last_name) target.id_str = tostring(target.id) target.rank = administration.get_rank(self, target.id, msg.chat.id, config) - return { target } + return { target }, utilities.input(msg.text) else local input = utilities.input(msg.text) if input then @@ -809,8 +809,9 @@ function administration.init_command(self_, config_) doc = 'Removes a user from the group. The target may be specified via reply, username, or ID.', action = function(self, msg, group, config) - local targets = administration.get_targets(self, msg, config) + local targets, reason = administration.get_targets(self, msg, config) if targets then + reason = reason and ': ' .. utilities.trim(reason) or '' local output = '' local s = drua.sopen() for _, target in ipairs(targets) do @@ -820,7 +821,7 @@ function administration.init_command(self_, config_) output = output .. target.name .. ' is too privileged to be kicked.\n' else output = output .. target.name .. ' has been kicked.\n' - administration.kick_user(self, msg.chat.id, target.id, 'kicked by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']', config, s) + administration.kick_user(self, msg.chat.id, target.id, 'kicked by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']' .. reason, config, s) if msg.chat.type == 'supergroup' then bindings.unbanChatMember{ chat_id = msg.chat.id, user_id = target.id } end @@ -843,8 +844,9 @@ function administration.init_command(self_, config_) doc = 'Bans a user from the group. The target may be specified via reply, username, or ID.', action = function(self, msg, group, config) - local targets = administration.get_targets(self, msg, config) + local targets, reason = administration.get_targets(self, msg, config) if targets then + reason = reason and ': ' .. utilities.trim(reason) or '' local output = '' local s = drua.sopen() for _, target in ipairs(targets) do @@ -856,7 +858,7 @@ function administration.init_command(self_, config_) output = output .. target.name .. ' is too privileged to be banned.\n' else output = output .. target.name .. ' has been banned.\n' - administration.kick_user(self, msg.chat.id, target.id, 'banned by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']', config, s) + administration.kick_user(self, msg.chat.id, target.id, 'banned by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']' .. reason, config, s) group.mods[target.id_str] = nil group.bans[target.id_str] = true end @@ -1306,8 +1308,9 @@ Use this command to configure the point values for each message type. When a use doc = 'Bans a user from all groups. The target may be specified via reply, username, or ID.', action = function(self, msg, group, config) - local targets = administration.get_targets(self, msg, config) + local targets, reason = administration.get_targets(self, msg, config) if targets then + reason = reason and ': ' .. utilities.trim(reason) or '' local output = '' local s = drua.sopen() for _, target in ipairs(targets) do @@ -1319,8 +1322,8 @@ Use this command to configure the point values for each message type. When a use output = output .. target.name .. ' is too privileged to be globally banned.\n' else if group then - local reason = 'hammered by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']' - administration.kick_user(self, msg.chat.id, target.id, reason, config) + local reason_ = 'hammered by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']' .. reason + administration.kick_user(self, msg.chat.id, target.id, reason_, config) end for k,v in pairs(self.database.administration.groups) do if not v.flags[6] then From e92beeb58ac72b65c057f184b5a770239c11f7c4 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Fri, 26 Aug 2016 23:25:55 -0400 Subject: [PATCH 2/6] trivial stuff --- README.md | 1 + otouto/bindings.lua | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a2779d8..c9e4992 100644 --- a/README.md +++ b/README.md @@ -410,3 +410,4 @@ Contributions are appreciated in all forms. Monetary contributions will go towar | [n8 c00](http://telegram.me/n8_c00) | | [Alex](http://telegram.me/sandu) | | [Brayden](http://telegram.me/bb010g) | +| [Milad](http://telegram.me/thelad) | diff --git a/otouto/bindings.lua b/otouto/bindings.lua index b821722..5250422 100644 --- a/otouto/bindings.lua +++ b/otouto/bindings.lua @@ -22,10 +22,10 @@ local bindings = {} -local HTTPS = require('ssl.https') -local JSON = require('dkjson') +local https = require('ssl.https') +local json = require('dkjson') local ltn12 = require('ltn12') -local MP_ENCODE = require('multipart-post').encode +local mp_encode = require('multipart-post').encode function bindings.init(token) bindings.BASE_URL = 'https://api.telegram.org/bot' .. token .. '/' @@ -58,8 +58,8 @@ function bindings.request(method, parameters, file) parameters = {''} end local response = {} - local body, boundary = MP_ENCODE(parameters) - local success, code = HTTPS.request{ + local body, boundary = mp_encode(parameters) + local success, code = https.request{ url = bindings.BASE_URL .. method, method = 'POST', headers = { @@ -74,7 +74,7 @@ function bindings.request(method, parameters, file) print(method .. ': Connection error. [' .. code .. ']') return false, false else - local result = JSON.decode(data) + local result = json.decode(data) if not result then return false, false elseif result.ok then From 4faa945e3409dbb724809f4922b6eca75e42e59d Mon Sep 17 00:00:00 2001 From: Akamaru Date: Sat, 27 Aug 2016 16:06:21 +0200 Subject: [PATCH 3/6] =?UTF-8?q?Nutze=20HTTPS=20f=C3=BCr=20MyAnimeList=20(#?= =?UTF-8?q?11)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/myanimelist.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/otouto/plugins/myanimelist.lua b/otouto/plugins/myanimelist.lua index 5030f82..4b11627 100644 --- a/otouto/plugins/myanimelist.lua +++ b/otouto/plugins/myanimelist.lua @@ -28,7 +28,7 @@ end local user = cred_data.mal_user local password = cred_data.mal_pw -local BASE_URL = 'http://'..user..':'..password..'@myanimelist.net/api' +local BASE_URL = 'https://'..user..':'..password..'@myanimelist.net/api' function mal:delete_tags(str) str = string.gsub( str, '
', '') @@ -50,7 +50,7 @@ function mal:get_mal_info(query, typ) elseif typ == 'manga' then url = BASE_URL..'/manga/search.xml?q='..query end - local res,code = http.request(url) + local res,code = https.request(url) if code ~= 200 then return "HTTP-Fehler" end local result = xml.load(res) return result @@ -59,7 +59,7 @@ end function mal:send_anime_data(result, receiver) local title = xml.find(result, 'title')[1] local id = xml.find(result, 'id')[1] - local mal_url = 'http://myanimelist.net/anime/'..id + local mal_url = 'https://myanimelist.net/anime/'..id if xml.find(result, 'synonyms')[1] then alt_name = '\noder: '..unescape(mal:delete_tags(xml.find(result, 'synonyms')[1])) @@ -121,7 +121,7 @@ end function mal:send_manga_data(result) local title = xml.find(result, 'title')[1] local id = xml.find(result, 'id')[1] - local mal_url = 'http://myanimelist.net/manga/'..id + local mal_url = 'https://myanimelist.net/manga/'..id if xml.find(result, 'type')[1] then typ = ' ('..xml.find(result, 'type')[1]..')' From 472eaa30ea09ed984824e42f981c3217ffac145e Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sat, 27 Aug 2016 16:13:42 +0200 Subject: [PATCH 4/6] Noch nicht bereit --- otouto/plugins/voice_to_text.lua | 65 -------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 otouto/plugins/voice_to_text.lua diff --git a/otouto/plugins/voice_to_text.lua b/otouto/plugins/voice_to_text.lua deleted file mode 100644 index 7b9c98b..0000000 --- a/otouto/plugins/voice_to_text.lua +++ /dev/null @@ -1,65 +0,0 @@ -local vtt = {} - -vtt.triggers = { - '/nil' -} - -local apikey = cred_data.witai_apikey -local headers = { - ["Content-Type"] = "audio/mpeg3", - Authorization = "Bearer "..apikey -} - -function vtt:pre_process(msg, config) - if not msg.voice then return msg end -- Ignore - local mime_type = msg.voice.mime_type - if mime_type ~= 'audio/ogg' then return msg end -- We only want to transcript voice messages - - local file_id = msg.voice.file_id - local file_size = msg.voice.file_size - if file_size > 19922944 then - print('File is over 20 MB - can\'t download :(') - return msg - end - - utilities.send_typing(msg.chat.id, 'typing') - -- Saving file to the Telegram Cloud - local request = bindings.request('getFile', { - file_id = file_id - } ) - - local download_url = 'https://api.telegram.org/file/bot'..config.bot_api_key..'/'..request.result.file_path - local ogg_file_name = file_id..'.ogg' - local ogg_file = download_to_file(download_url, ogg_file_name) - - -- Convert to MP3 - run_command('ffmpeg -loglevel panic -i /tmp/'..ogg_file_name..' -ac 1 -y /tmp/'..file_id..'.mp3') - os.remove('/tmp/'..ogg_file_name) - - -- Voice-To-Text via wit.ai - local req_url = 'https://api.wit.ai/speech' - local response_body = {} - local ok, response_code = https.request{ - url = req_url, - method = "POST", - headers = headers, - source = ltn12.source.file(io.open("/tmp/"..file_id..".mp3")), - sink = ltn12.sink.table(response_body) - } - os.remove("/tmp/"..file_id..".mp3") - - if response_code ~= 200 then - return msg - end - vardump(response_body) - local out = json.decode(table.concat(response_body)) - vardump(out) - utilities.send_reply(msg, out._text) - - return msg -end - -function vtt:action(msg) -end - -return vtt \ No newline at end of file From 4014ec055023bef4a9b5839f67edf5520bdb4b39 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sat, 27 Aug 2016 23:31:53 +0200 Subject: [PATCH 5/6] =?UTF-8?q?-=20NEU:=20Voice-To-Text!=20"=C3=9Cbersetzt?= =?UTF-8?q?"=20Sprachnachrichten=20in=20Text=20(wit.ai=20Server=20API-Key?= =?UTF-8?q?=20muss=20eingetragen=20werden!)=20-=20POST-Requests=20mit=20Da?= =?UTF-8?q?teien=20sind=20nun=20m=C3=B6glich?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/voice_to_text.lua | 61 ++++++++++++++++++++++++++++++++ otouto/utilities.lua | 22 ++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 otouto/plugins/voice_to_text.lua diff --git a/otouto/plugins/voice_to_text.lua b/otouto/plugins/voice_to_text.lua new file mode 100644 index 0000000..2f37406 --- /dev/null +++ b/otouto/plugins/voice_to_text.lua @@ -0,0 +1,61 @@ +local vtt = {} + +vtt.triggers = { + '/nil' +} + +local apikey = cred_data.witai_apikey + +function vtt:pre_process(msg, config) + if not msg.voice then return msg end -- Ignore + local mime_type = msg.voice.mime_type + if mime_type ~= 'audio/ogg' then return msg end -- We only want to transcript voice messages + local file_id = msg.voice.file_id + local file_size = msg.voice.file_size + if file_size > 19922944 then + print('File is over 20 MB - can\'t download :(') + return msg + end + + utilities.send_typing(msg.chat.id, 'typing') + -- Saving file to the Telegram Cloud + local request = bindings.request('getFile', { + file_id = file_id + } ) + + local download_url = 'https://api.telegram.org/file/bot'..config.bot_api_key..'/'..request.result.file_path + local ogg_file_name = file_id..'.oga' + local ogg_file = download_to_file(download_url, ogg_file_name) + + -- Convert to MP3 + run_command('ffmpeg -loglevel panic -i /tmp/'..ogg_file_name..' -ac 1 -y /tmp/'..file_id..'.mp3') + os.remove('/tmp/'..ogg_file_name) + + local mp3_file = '/tmp/'..file_id..'.mp3' + + -- Voice-To-Text via wit.ai + local headers = { + ["Content-Type"] = "audio/mpeg3", + Authorization = "Bearer "..apikey + } + local data = post_petition('https://api.wit.ai/speech?v=20160526', io.open(mp3_file, 'r'), headers) + os.remove(mp3_file) + + if not data then + return msg + end + + if not data._text then + utilities.send_reply(msg, 'Keine Stimme zu hören!') + return + end + + utilities.send_reply(msg, data._text) + + return msg +end + +function vtt:action(msg) +end + +return vtt \ No newline at end of file diff --git a/otouto/utilities.lua b/otouto/utilities.lua index e53d786..24d4651 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -450,6 +450,15 @@ function utilities.save_data(filename, data) f:close() end +-- Returns file size as Integer +-- See: https://www.lua.org/pil/21.3.html +function get_file_size(file) + local current = file:seek() -- get current position + local size = file:seek("end") -- get file size + file:seek("set", current) -- restore position + return tonumber(size) +end + -- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua. function utilities.get_coords(input, config) local url = 'https://maps.googleapis.com/maps/api/geocode/json?address='..URL.escape(input)..'&language=de' @@ -723,6 +732,10 @@ function is_service_msg(msg) return var end +-- Make a POST request +-- URL = obvious +-- Arguments = Things, that go into the body. If sending a file, use 'io.open('path/to/file', "r")' +-- Headers = Header table. If not set, we will set a few! function post_petition(url, arguments, headers) local url, h = string.gsub(url, "http://", "") local url, hs = string.gsub(url, "https://", "") @@ -749,8 +762,13 @@ function post_petition(url, arguments, headers) request_constructor.headers["X-Accept"] = "application/json" request_constructor.headers["Accept"] = "application/json" end - request_constructor.headers["Content-Length"] = tostring(#source) - request_constructor.source = ltn12.source.string(source) + if type(arguments) == 'userdata' then + request_constructor.headers["Content-Length"] = get_file_size(source) + request_constructor.source = ltn12.source.file(source) + else + request_constructor.headers["Content-Length"] = tostring(#source) + request_constructor.source = ltn12.source.string(source) + end if post_prot == "http" then ok, response_code, response_headers, response_status_line = http.request(request_constructor) From b172aa5b16b96636b1de88ce3d7304c43131d039 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sun, 28 Aug 2016 00:08:08 +0200 Subject: [PATCH 6/6] =?UTF-8?q?-=20Cats:=20Bugfix=20-=20Besserer=20Text=20?= =?UTF-8?q?f=C3=BCr=20V-T-T?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/cats.lua | 2 +- otouto/plugins/voice_to_text.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/otouto/plugins/cats.lua b/otouto/plugins/cats.lua index 5ef7552..a95dd3a 100644 --- a/otouto/plugins/cats.lua +++ b/otouto/plugins/cats.lua @@ -59,7 +59,7 @@ function cats:inline_callback(inline_query, config, matches) utilities.answer_inline_query(inline_query, results, 30) end -function cats:action(msg, config) +function cats:action(msg, config, matches) if matches[1] == 'gif' then local url = 'http://thecatapi.com/api/images/get?type=gif&apikey='..apikey local file = download_to_file(url, 'miau.gif') diff --git a/otouto/plugins/voice_to_text.lua b/otouto/plugins/voice_to_text.lua index 2f37406..f08bd69 100644 --- a/otouto/plugins/voice_to_text.lua +++ b/otouto/plugins/voice_to_text.lua @@ -46,7 +46,7 @@ function vtt:pre_process(msg, config) end if not data._text then - utilities.send_reply(msg, 'Keine Stimme zu hören!') + utilities.send_reply(msg, 'Keine Stimme zu hören oder Erkennung fehlgeschlagen.') return end