- Portiere media_download (#1)

- Portiere BTC
- Portiere Calc
This commit is contained in:
Andreas Bielawski 2016-06-16 20:56:37 +02:00
parent ec57c247a7
commit e72ecf923c
4 changed files with 181 additions and 31 deletions

View File

@ -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

40
otouto/plugins/btc.lua Normal file
View File

@ -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

View File

@ -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 <expression>'
calc.command = 'calc <Ausdruck>'
function calc:init(config)
calc.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('calc', true).table
calc.doc = [[```
]]..config.cmd_pat..[[calc <expression>
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

113
otouto/plugins/getfile.lua Normal file
View File

@ -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