From 306782496e9b94d96d7fb4da519959f36a6393f8 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sun, 12 Jun 2016 20:53:20 +0200 Subject: [PATCH] Integriere Telegram-Funktionen, wie sendPhoto, sendVideo, sendDocument, etc. in utilites.lua und update alle Plugins, damit sie diese Funktion nutzen --- otouto/plugins/9gag.lua | 5 +- otouto/plugins/gImages.lua | 5 +- otouto/plugins/imdb.lua | 2 +- otouto/plugins/twitter.lua | 8 +-- otouto/plugins/youtube.lua | 4 +- otouto/utilities.lua | 101 +++++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 16 deletions(-) diff --git a/otouto/plugins/9gag.lua b/otouto/plugins/9gag.lua index 9577cac..e32fda6 100644 --- a/otouto/plugins/9gag.lua +++ b/otouto/plugins/9gag.lua @@ -26,6 +26,7 @@ function ninegag:get_9GAG() end function ninegag:action(msg, config) + utilities.send_typing(self, msg.chat.id, 'upload_photo') local url, title = ninegag:get_9GAG() if not url then utilities.send_reply(self, msg, config.errors.connection) @@ -33,9 +34,7 @@ function ninegag:action(msg, config) end local file = download_to_file(url) - bindings.sendPhoto(self, {chat_id = msg.chat.id, caption = title}, {photo = file} ) - os.remove(file) - print("Deleted: "..file) + utilities.send_photo(self, msg.chat.id, file, title) end return ninegag diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index 36bc7f8..a73b85b 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -47,6 +47,7 @@ function gImages:action(msg, config) return end + utilities.send_typing(self, msg.chat.id, 'upload_photo') local apikey = cred_data.google_apikey local cseid = cred_data.google_cse_id local BASE_URL = 'https://www.googleapis.com/customsearch/v1' @@ -68,9 +69,7 @@ function gImages:action(msg, config) local img_url = jdat.items[i].link local file = download_to_file(img_url) - bindings.sendPhoto(self, {chat_id = msg.chat.id, caption = img_url}, {photo = file} ) - os.remove(file) - print("Deleted: "..file) + utilities.send_photo(self, msg.chat.id, file, img_url) end return gImages diff --git a/otouto/plugins/imdb.lua b/otouto/plugins/imdb.lua index caa3897..b82d803 100644 --- a/otouto/plugins/imdb.lua +++ b/otouto/plugins/imdb.lua @@ -51,7 +51,7 @@ function imdb:action(msg, config) if jdat.Poster ~= "N/A" then local file = download_to_file(jdat.Poster) - bindings.sendPhoto(self, {chat_id = msg.chat.id}, {photo = file} ) + utilities.send_photo(self, msg.chat.id, file) end end diff --git a/otouto/plugins/twitter.lua b/otouto/plugins/twitter.lua index 98546a1..92175cd 100644 --- a/otouto/plugins/twitter.lua +++ b/otouto/plugins/twitter.lua @@ -140,15 +140,11 @@ function twitter:action(msg) utilities.send_reply(self, msg, header .. "\n" .. text.."\n"..footer) for k, v in pairs(images) do local file = download_to_file(v) - bindings.sendPhoto(self, {chat_id = msg.chat.id}, {photo = file} ) - os.remove(file) - print("Deleted: "..file) + utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id) end for k, v in pairs(videos) do local file = download_to_file(v) - bindings.sendVideo(self, {chat_id = msg.chat.id}, {video = file} ) - os.remove(file) - print("Deleted: "..file) + utilities.send_video(self, msg.chat.id, file, nil, msg.message_id) end end diff --git a/otouto/plugins/youtube.lua b/otouto/plugins/youtube.lua index b41a82e..cc48638 100644 --- a/otouto/plugins/youtube.lua +++ b/otouto/plugins/youtube.lua @@ -142,9 +142,7 @@ function send_youtube_data(data, msg, self, link, sendpic) text = text..'\nACHTUNG, In Deutschland gesperrt!' end local file = download_to_file(image_url) - bindings.sendPhoto(self, {chat_id = msg.chat.id, reply_to_message_id = msg.message_id, caption = text }, {photo = file} ) - os.remove(file) - print("Deleted: "..file) + utilities.send_photo(self, msg.chat.id, file, text, msg.message_id) else utilities.send_reply(self, msg, text, true) end diff --git a/otouto/utilities.lua b/otouto/utilities.lua index ef6ab91..51fbe9d 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -34,6 +34,107 @@ function utilities:send_reply(old_msg, text, use_markdown) parse_mode = use_markdown and 'Markdown' or nil } ) end + +-- NOTE: Telegram currently only allows file uploads up to 50 MB +-- https://core.telegram.org/bots/api#sendphoto +function utilities:send_photo(chat_id, file, text, reply_to_message_id) + local output = bindings.request(self, 'sendPhoto', { + chat_id = chat_id, + caption = text or nil, + reply_to_message_id = reply_to_message_id + }, {photo = file} ) + os.remove(file) + print("Deleted: "..file) + return output +end + +-- https://core.telegram.org/bots/api#sendaudio +function utilities:send_audio(chat_id, file, text, reply_to_message_id, duration, performer, title) + local output = bindings.request(self, 'sendAudio', { + chat_id = chat_id, + caption = text or nil, + duration = duration or nil, + performer = performer or nil, + title = title or nil, + reply_to_message_id = reply_to_message_id + }, {audio = file} ) + os.remove(file) + print("Deleted: "..file) + return output +end + +-- https://core.telegram.org/bots/api#senddocument +function utilities:send_document(chat_id, file, text, reply_to_message_id) + local output = bindings.request(self, 'sendDocument', { + chat_id = chat_id, + caption = text or nil, + reply_to_message_id = reply_to_message_id + }, {document = file} ) + os.remove(file) + print("Deleted: "..file) + return output +end + +-- https://core.telegram.org/bots/api#sendvideo +function utilities:send_video(chat_id, file, text, reply_to_message_id, duration, width, height) + local output = bindings.request(self, 'sendVideo', { + chat_id = chat_id, + caption = text or nil, + duration = duration or nil, + width = width or nil, + height = height or nil, + reply_to_message_id = reply_to_message_id + }, {video = file} ) + os.remove(file) + print("Deleted: "..file) + return output +end + +-- NOTE: Voice messages are .ogg files encoded with OPUS +-- https://core.telegram.org/bots/api#sendvoice +function utilities:send_voice(chat_id, file, text, reply_to_message_id, duration) + local output = bindings.request(self, 'sendVoice', { + chat_id = chat_id, + duration = duration or nil, + reply_to_message_id = reply_to_message_id + }, {voice = file} ) + os.remove(file) + print("Deleted: "..file) + return output +end + +-- https://core.telegram.org/bots/api#sendlocation +function utilities:send_location(chat_id, latitude, longitude, reply_to_message_id) + return bindings.request(self, 'sendLocation', { + chat_id = chat_id, + latitude = latitude, + longitude = longitude, + reply_to_message_id = reply_to_message_id + } ) +end + +-- NOTE: Venue is different from location: it shows information, such as the street adress or +-- title of the location with it. +-- https://core.telegram.org/bots/api#sendvenue +function utilities:send_venue(chat_id, latitude, longitude, reply_to_message_id, title, address) + return bindings.request(self, 'sendVenue', { + chat_id = chat_id, + latitude = latitude, + longitude = longitude, + title = title, + address = address, + reply_to_message_id = reply_to_message_id + } ) +end + +-- https://core.telegram.org/bots/api#sendchataction +function utilities:send_typing(chat_id, action) + return bindings.request(self, 'sendChatAction', { + chat_id = chat_id, + action = action + } ) +end + -- get the indexed word in a string function utilities.get_word(s, i) s = s or ''