From 83e5f4a85eacebf1f142e3c2952a2f1d6405117d Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sun, 7 Aug 2016 17:33:33 +0200 Subject: [PATCH] - NEU: post_photo: Downloadet Foto, wenn es als Dokument gesendet wird und sendet es als Foto erneut - getfile nimmt jetzt den API-Key aus der Config - Venue: Bugfix - Facebook: Bugfix --- otouto/plugins/facebook.lua | 1 + otouto/plugins/getfile.lua | 5 ++--- otouto/plugins/post_photo.lua | 37 +++++++++++++++++++++++++++++++++++ otouto/plugins/venue.lua | 2 +- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 otouto/plugins/post_photo.lua diff --git a/otouto/plugins/facebook.lua b/otouto/plugins/facebook.lua index 97fc473..71f53db 100644 --- a/otouto/plugins/facebook.lua +++ b/otouto/plugins/facebook.lua @@ -43,6 +43,7 @@ function facebook:fb_post (id, story_id) local from = data.from.name local message = data.message + if not message then return nil end local name = data.name if data.link then link = '\n'..data.name..'' diff --git a/otouto/plugins/getfile.lua b/otouto/plugins/getfile.lua index cf29dfb..918855e 100644 --- a/otouto/plugins/getfile.lua +++ b/otouto/plugins/getfile.lua @@ -1,6 +1,5 @@ -- 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 = {} @@ -34,7 +33,7 @@ function media_download:download_to_file_permanently(url, file_name) return true end -function media_download:pre_process(msg, self) +function media_download:pre_process(msg, self, config) if msg.photo then local lv = #msg.photo -- find biggest photo, always the last value file_id = msg.photo[lv].file_id @@ -89,7 +88,7 @@ function media_download:pre_process(msg, self) end -- Construct what we want - local download_url = 'https://api.telegram.org/file/bot'..cred_data.bot_api_key..'/'..request.result.file_path + 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, file_path) if not ok then print('Download failed!') diff --git a/otouto/plugins/post_photo.lua b/otouto/plugins/post_photo.lua new file mode 100644 index 0000000..713a196 --- /dev/null +++ b/otouto/plugins/post_photo.lua @@ -0,0 +1,37 @@ +-- This plugin goes through every message with a document and if the document is an image, +-- it downloads the file and resends it as image + +local post_photo = {} + +post_photo.triggers = { + '/nil' +} + +function post_photo:pre_process(msg, self, config) + if not msg.document then return msg end -- Ignore + local mime_type = msg.document.mime_type + if mime_type ~= 'image/jpeg' and mime_type ~= 'image/png' and mime_type ~= 'image/bmp' then return msg end + + local file_id = msg.document.file_id + local file_size = msg.document.file_size + if file_size > 19922944 then + print('File is over 20 MB - can\'t download :(') + return + end + + -- Saving file to the Telegram Cloud + local request = bindings.request(self, 'getFile', { + file_id = file_id + } ) + + local download_url = 'https://api.telegram.org/file/bot'..config.bot_api_key..'/'..request.result.file_path + local file = download_to_file(download_url, msg.file_name) + utilities.send_photo(self, msg.chat.id, file, msg.caption, msg.message_id) + + return msg +end + +function post_photo:action(msg) +end + +return post_photo diff --git a/otouto/plugins/venue.lua b/otouto/plugins/venue.lua index ce75c59..80ad16d 100644 --- a/otouto/plugins/venue.lua +++ b/otouto/plugins/venue.lua @@ -7,7 +7,7 @@ venue.triggers = { local apikey = cred_data.google_apikey function venue:pre_process(msg, self) - if not msg.venue then return end -- Ignore + if not msg.venue then return msg end -- Ignore local lat = msg.venue.location.latitude local lng = msg.venue.location.longitude