- Verbessertes Error-Handling, wenn Datei nicht gesendet werden konnte

- Unescape bei CallbackAnswers (danke @Centzilius)
- gImages sollte jetzt stabiler laufen und weniger "Techniker ist informiert" zurücksenden
This commit is contained in:
Andreas Bielawski 2016-07-04 01:29:51 +02:00
parent 465b6e0da7
commit b089efa34b
4 changed files with 24 additions and 11 deletions

View File

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

View File

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

View File

@ -30,7 +30,8 @@ end
gImages.command = 'img <Suchbegriff>'
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

View File

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