From b089efa34b7a9956a79896b8c4926300bebadfcb Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Mon, 4 Jul 2016 01:29:51 +0200 Subject: [PATCH] =?UTF-8?q?-=20Verbessertes=20Error-Handling,=20wenn=20Dat?= =?UTF-8?q?ei=20nicht=20gesendet=20werden=20konnte=20-=20Unescape=20bei=20?= =?UTF-8?q?CallbackAnswers=20(danke=20@Centzilius)=20-=20gImages=20sollte?= =?UTF-8?q?=20jetzt=20stabiler=20laufen=20und=20weniger=20"Techniker=20ist?= =?UTF-8?q?=20informiert"=20zur=C3=BCcksenden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/bindings.lua | 1 + otouto/bot.lua | 4 ++-- otouto/plugins/gImages.lua | 25 ++++++++++++++++--------- otouto/utilities.lua | 5 +++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/otouto/bindings.lua b/otouto/bindings.lua index 1f753ce..15af11b 100644 --- a/otouto/bindings.lua +++ b/otouto/bindings.lua @@ -28,6 +28,7 @@ function bindings:request(method, parameters, file) end if file and next(file) ~= nil then local file_type, file_name = next(file) + if not file_name then return false end if string.match(file_name, '/tmp/') then local file_file = io.open(file_name, 'r') local file_data = { diff --git a/otouto/bot.lua b/otouto/bot.lua index eda4b5d..c5f673b 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -90,8 +90,8 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba -- vardump(msg) -- vardump(callback) - if msg.date < os.time() - 3600 then -- Do not process old messages. - utilities.answer_callback_query(self, callback, 'Nachricht älter als eine Stunde, bitte sende den Befehl selbst noch einmal.', true) + if msg.date < os.time() - 1800 then -- Do not process old messages. + utilities.answer_callback_query(self, callback, 'Nachricht älter als eine halbe Stunde, bitte sende den Befehl selbst noch einmal.', true) return end diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index 69565b5..a87e371 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -30,7 +30,8 @@ end gImages.command = 'img ' function gImages:callback(callback, msg, self, config, input) - utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..input..'"') + if not msg then return end + utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..URL.unescape(input)..'"') utilities.send_typing(self, msg.chat.id, 'upload_photo') local img_url, mimetype, context = gImages:get_image(input) if img_url == 403 then @@ -40,14 +41,18 @@ function gImages:callback(callback, msg, self, config, input) utilities.send_reply(self, msg, config.errors.connection, true) return end - + if mimetype == 'image/gif' then local file = download_to_file(img_url, 'img.gif') - result = utilities.send_document(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}') - else + result = utilities.send_document(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..input..'"}]]}') + elseif mimetype == 'image/png' then local file = download_to_file(img_url, 'img.png') - result = utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}') + result = utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..input..'"}]]}') + elseif mimetype == 'image/jpeg' then + local file = download_to_file(img_url, 'img.jpg') + result = utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..input..'"}]]}') end + if not result then utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"gImages:'..input..'"}]]}') return @@ -55,8 +60,8 @@ function gImages:callback(callback, msg, self, config, input) end function gImages:get_image(input) - local apikey = cred_data.google_apikey - local cseid = cred_data.google_cse_id + local apikey = cred_data.google_apikey_2 -- 100 requests is RIDICULOUS Google! + local cseid = cred_data.google_cse_id_2 local BASE_URL = 'https://www.googleapis.com/customsearch/v1' local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&safe=high'..'&q=' .. input .. '&fields=searchInformation(totalResults),queries(request(count)),items(link,mime,image(contextLink))' local jstr, res = HTTPS.request(url) @@ -99,7 +104,6 @@ function gImages:action(msg, config, matches) utilities.send_typing(self, msg.chat.id, 'upload_photo') local img_url, mimetype, context = gImages:get_image(URL.escape(input)) - if img_url == 403 then utilities.send_reply(self, msg, config.errors.quotaexceeded, true) return @@ -111,9 +115,12 @@ function gImages:action(msg, config, matches) if mimetype == 'image/gif' then local file = download_to_file(img_url, 'img.gif') result = utilities.send_document(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"}],[{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}') - else + elseif mimetype == 'image/png' then local file = download_to_file(img_url, 'img.png') result = utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}') + elseif mimetype == 'image/jpeg' then + local file = download_to_file(img_url, 'img.jpg') + result = utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}') end if not result then diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 460a00e..13e7b42 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -53,6 +53,7 @@ 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, reply_markup) + if not file then return false end local output = bindings.request(self, 'sendPhoto', { chat_id = chat_id, caption = text or nil, @@ -68,6 +69,7 @@ end -- https://core.telegram.org/bots/api#sendaudio function utilities:send_audio(chat_id, file, reply_to_message_id, duration, performer, title) + if not file then return false end local output = bindings.request(self, 'sendAudio', { chat_id = chat_id, duration = duration or nil, @@ -84,6 +86,7 @@ end -- https://core.telegram.org/bots/api#senddocument function utilities:send_document(chat_id, file, text, reply_to_message_id, reply_markup) + if not file then return false end local output = bindings.request(self, 'sendDocument', { chat_id = chat_id, caption = text or nil, @@ -99,6 +102,7 @@ end -- https://core.telegram.org/bots/api#sendvideo function utilities:send_video(chat_id, file, text, reply_to_message_id, duration, width, height) + if not file then return false end local output = bindings.request(self, 'sendVideo', { chat_id = chat_id, caption = text or nil, @@ -117,6 +121,7 @@ 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) + if not file then return false end local output = bindings.request(self, 'sendVoice', { chat_id = chat_id, duration = duration or nil,