This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/miku/plugins/getfile.lua
2017-02-15 18:40:14 +01:00

112 lines
2.9 KiB
Lua

-- YOU NEED THE FOLLOWING FOLDERS: photo, document, video, voice
-- PLEASE ADJUST YOUR PATH BELOW
local media_download = {}
media_download.triggers = {
'/nil'
}
function media_download:download_to_file_permanently(url, save_dir, 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
local file_path = save_dir..'/'..file_name
print(file_path)
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, config)
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 msg
end
if file_size > 19922944 then
print('Datei ist größer als 20 MB - Nicht downloadbar!')
return msg
end
local save_dir = config.getfile_path
if not save_dir then
print('getfile_path wurde nicht in der Config gesetzt, breche ab...')
return msg
end
-- Check if file has already been downloaded
local already_downloaded = redis:sismember('telegram:file_id', file_id)
if already_downloaded == true then
print('Datei wurde bereits gedownloadet')
return msg
end
-- Saving file to the Telegram Cloud
local request = bindings.request('getFile', {
file_id = file_id
} )
-- Getting file from the Telegram Cloud
if not request then
print('Download fehlgeschlagen!')
return msg
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'..config.bot_api_key..'/'..request.result.file_path
local ok = media_download:download_to_file_permanently(download_url, save_dir, file_path)
if not ok then
print('Download fehlgeschlagen!')
return msg
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 msg
end
function media_download:action(msg)
end
return media_download