From e72ecf923c9961a90025acf25920d50c59b6b7e2 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 16 Jun 2016 20:56:37 +0200 Subject: [PATCH] - Portiere media_download (#1) - Portiere BTC - Portiere Calc --- otouto/bot.lua | 3 +- otouto/plugins/btc.lua | 40 +++++++++++++ otouto/plugins/calc.lua | 56 +++++++++--------- otouto/plugins/getfile.lua | 113 +++++++++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+), 31 deletions(-) create mode 100644 otouto/plugins/btc.lua create mode 100644 otouto/plugins/getfile.lua diff --git a/otouto/bot.lua b/otouto/bot.lua index a9302c5..45cd581 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -51,7 +51,8 @@ function bot:init(config) -- The function run when the bot is started or reloade end function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received. - + -- remove comment to enable debugging + -- vardump(msg) -- 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 diff --git a/otouto/plugins/btc.lua b/otouto/plugins/btc.lua new file mode 100644 index 0000000..41548d2 --- /dev/null +++ b/otouto/plugins/btc.lua @@ -0,0 +1,40 @@ +local btc = {} + +local https = require('ssl.https') +local URL = require('socket.url') +local json = require('dkjson') +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') + +function btc:init(config) + btc.triggers = { + "^/btc$" + } + btc.doc = [[* +]]..config.cmd_pat..[[btc*: Zeigt aktuellen Bitcoin-Kurs an]] +end + +btc.command = 'btc' + +-- See https://bitcoinaverage.com/api +function btc:getBTCX() + local base_url = 'https://api.bitcoinaverage.com/ticker/global/' + -- Do request on bitcoinaverage, the final / is critical! + local res,code = https.request(base_url.."EUR/") + + if code ~= 200 then return nil end + local data = json.decode(res) + local ask = string.gsub(data.ask, "%.", ",") + local bid = string.gsub(data.bid, "%.", ",") + + -- Easy, it's right there + text = 'BTC/EUR\n'..'*Kaufen:* '..ask..'\n'..'*Verkaufen:* '..bid + return text +end + + +function btc:action(msg, config, matches) + utilities.send_reply(self, msg, btc:getBTCX(cur), true) +end + +return btc \ No newline at end of file diff --git a/otouto/plugins/calc.lua b/otouto/plugins/calc.lua index e9cfd99..f43adf7 100644 --- a/otouto/plugins/calc.lua +++ b/otouto/plugins/calc.lua @@ -1,43 +1,39 @@ local calc = {} local URL = require('socket.url') -local HTTPS = require('ssl.https') +local http = require('socket.http') local utilities = require('otouto.utilities') -calc.command = 'calc ' +calc.command = 'calc ' function calc:init(config) - calc.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('calc', true).table - calc.doc = [[``` -]]..config.cmd_pat..[[calc -Returns solutions to mathematical expressions and conversions between common units. Results provided by mathjs.org. -```]] + calc.triggers = { + "^/calc (.*)$" + } + calc.doc = [[* +]]..config.cmd_pat..[[calc* _[Ausdruck]_: Rechnet]] end -function calc:action(msg, config) - - local input = utilities.input(msg.text) - if not input then - if msg.reply_to_message and msg.reply_to_message.text then - input = msg.reply_to_message.text - else - utilities.send_message(self, msg.chat.id, calc.doc, true, msg.message_id, true) - return - end - end - - local url = 'https://api.mathjs.org/v1/?expr=' .. URL.escape(input) - - local output = HTTPS.request(url) - if not output then - utilities.send_reply(self, msg, config.errors.connection) - return - end - - output = '`' .. output .. '`' - - utilities.send_message(self, msg.chat.id, output, true, msg.message_id, true) +function calc:mathjs(exp) + local exp = string.gsub(exp, ",", "%.") + local url = 'http://api.mathjs.org/v1/' + url = url..'?expr='..URL.escape(exp) + local b,c = http.request(url) + local text = nil + if c == 200 then + text = '= '..string.gsub(b, "%.", ",") + + elseif c == 400 then + text = b + else + text = 'Unerwarteter Fehler\n' + ..'Ist api.mathjs.org erreichbar?' + end + return text +end +function calc:action(msg, config, matches) + utilities.send_reply(self, msg, calc:mathjs(matches[1])) end return calc diff --git a/otouto/plugins/getfile.lua b/otouto/plugins/getfile.lua new file mode 100644 index 0000000..5bca07f --- /dev/null +++ b/otouto/plugins/getfile.lua @@ -0,0 +1,113 @@ +-- YOU NEED THE FOLLOWING FOLDERS: photo, document, video, voice +-- PLEASE ADJUST YOUR PATH BELOW +-- Save your bot api key in redis set telegram:credentials! + +local media_download = {} + +local utilities = require('otouto.utilities') +local bindings = require('otouto.bindings') +local ltn12 = require('ltn12') +local HTTPS = require('ssl.https') +local redis = (loadfile "./otouto/redis.lua")() + +media_download.triggers = { + '/nil' +} + +function media_download:download_to_file_permanently(url, file_name) + local respbody = {} + local options = { + url = url, + sink = ltn12.sink.table(respbody), + redirect = false + } + local response = nil + response = {HTTPS.request(options)} + + local code = response[2] + local headers = response[3] + local status = response[4] + + if code ~= 200 then return false end + + -- TODO: Save, when folder doesn't exist + -- Create necessary folders in this folder! + local file_path = "/home/YOURPATH/tmp/"..file_name + file = io.open(file_path, "w+") + file:write(table.concat(respbody)) + file:close() + print("Downloaded to: "..file_path) + return true +end + +function media_download:pre_process(msg, self) + if msg.photo then + local lv = #msg.photo -- find biggest photo, always the last value + file_id = msg.photo[lv].file_id + file_size = msg.photo[lv].file_size + elseif msg.video then + file_id = msg.video.file_id + file_size = msg.video.file_size + elseif msg.sticker then + file_id = msg.sticker.file_id + file_size = msg.sticker.file_size + elseif msg.voice then + file_id = msg.voice.file_id + file_size = msg.voice.file_size + elseif msg.audio then + file_id = msg.audio.file_id + file_size = msg.audio.file_size + elseif msg.document then + file_id = msg.document.file_id + file_size = msg.document.file_size + else + return + end + + if file_size > 19922944 then + print('File is over 20 MB - can\'t download :(') + return + end + + -- Check if file has already been downloaded + local already_downloaded = redis:sismember('telegram:file_id', file_id) + if already_downloaded == true then + print('File has already been downloaded in the past, skipping...') + return + end + + -- Saving file to the Telegram Cloud + local request = bindings.request(self, 'getFile', { + file_id = file_id + } ) + + -- Getting file from the Telegram Cloud + if not request then + print('Download failed!') + return + end + + -- Use original filename for documents + if msg.document then + file_path = 'document/'..file_id..'-'..msg.document.file_name -- to not overwrite a file + else + file_path = request.result.file_path + end + + -- Construct what we want + local download_url = 'https://api.telegram.org/file/bot'..cred_data.bot_api_key..'/'..request.result.file_path + local ok = media_download:download_to_file_permanently(download_url, file_path) + if not ok then + print('Download failed!') + return + end + + -- Save file_id to redis to prevent downloading the same file over and over when forwarding + redis:sadd('telegram:file_id', file_id) + return +end + +function media_download:action(msg) +end + +return media_download