- 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:
parent
465b6e0da7
commit
b089efa34b
@ -28,6 +28,7 @@ function bindings:request(method, parameters, file)
|
|||||||
end
|
end
|
||||||
if file and next(file) ~= nil then
|
if file and next(file) ~= nil then
|
||||||
local file_type, file_name = next(file)
|
local file_type, file_name = next(file)
|
||||||
|
if not file_name then return false end
|
||||||
if string.match(file_name, '/tmp/') then
|
if string.match(file_name, '/tmp/') then
|
||||||
local file_file = io.open(file_name, 'r')
|
local file_file = io.open(file_name, 'r')
|
||||||
local file_data = {
|
local file_data = {
|
||||||
|
@ -90,8 +90,8 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba
|
|||||||
-- vardump(msg)
|
-- vardump(msg)
|
||||||
-- vardump(callback)
|
-- vardump(callback)
|
||||||
|
|
||||||
if msg.date < os.time() - 3600 then -- Do not process old messages.
|
if msg.date < os.time() - 1800 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)
|
utilities.answer_callback_query(self, callback, 'Nachricht älter als eine halbe Stunde, bitte sende den Befehl selbst noch einmal.', true)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,7 +30,8 @@ end
|
|||||||
gImages.command = 'img <Suchbegriff>'
|
gImages.command = 'img <Suchbegriff>'
|
||||||
|
|
||||||
function gImages:callback(callback, msg, self, config, input)
|
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')
|
utilities.send_typing(self, msg.chat.id, 'upload_photo')
|
||||||
local img_url, mimetype, context = gImages:get_image(input)
|
local img_url, mimetype, context = gImages:get_image(input)
|
||||||
if img_url == 403 then
|
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)
|
utilities.send_reply(self, msg, config.errors.connection, true)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if mimetype == 'image/gif' then
|
if mimetype == 'image/gif' then
|
||||||
local file = download_to_file(img_url, 'img.gif')
|
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)..'"}]]}')
|
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..'"}]]}')
|
||||||
else
|
elseif mimetype == 'image/png' then
|
||||||
local file = download_to_file(img_url, 'img.png')
|
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
|
end
|
||||||
|
|
||||||
if not result then
|
if not result then
|
||||||
utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"gImages:'..input..'"}]]}')
|
utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"gImages:'..input..'"}]]}')
|
||||||
return
|
return
|
||||||
@ -55,8 +60,8 @@ function gImages:callback(callback, msg, self, config, input)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function gImages:get_image(input)
|
function gImages:get_image(input)
|
||||||
local apikey = cred_data.google_apikey
|
local apikey = cred_data.google_apikey_2 -- 100 requests is RIDICULOUS Google!
|
||||||
local cseid = cred_data.google_cse_id
|
local cseid = cred_data.google_cse_id_2
|
||||||
local BASE_URL = 'https://www.googleapis.com/customsearch/v1'
|
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 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)
|
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')
|
utilities.send_typing(self, msg.chat.id, 'upload_photo')
|
||||||
local img_url, mimetype, context = gImages:get_image(URL.escape(input))
|
local img_url, mimetype, context = gImages:get_image(URL.escape(input))
|
||||||
|
|
||||||
if img_url == 403 then
|
if img_url == 403 then
|
||||||
utilities.send_reply(self, msg, config.errors.quotaexceeded, true)
|
utilities.send_reply(self, msg, config.errors.quotaexceeded, true)
|
||||||
return
|
return
|
||||||
@ -111,9 +115,12 @@ function gImages:action(msg, config, matches)
|
|||||||
if mimetype == 'image/gif' then
|
if mimetype == 'image/gif' then
|
||||||
local file = download_to_file(img_url, 'img.gif')
|
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)..'"}]]}')
|
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')
|
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:'..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
|
end
|
||||||
|
|
||||||
if not result then
|
if not result then
|
||||||
|
@ -53,6 +53,7 @@ end
|
|||||||
-- NOTE: Telegram currently only allows file uploads up to 50 MB
|
-- NOTE: Telegram currently only allows file uploads up to 50 MB
|
||||||
-- https://core.telegram.org/bots/api#sendphoto
|
-- https://core.telegram.org/bots/api#sendphoto
|
||||||
function utilities:send_photo(chat_id, file, text, reply_to_message_id, reply_markup)
|
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', {
|
local output = bindings.request(self, 'sendPhoto', {
|
||||||
chat_id = chat_id,
|
chat_id = chat_id,
|
||||||
caption = text or nil,
|
caption = text or nil,
|
||||||
@ -68,6 +69,7 @@ end
|
|||||||
|
|
||||||
-- https://core.telegram.org/bots/api#sendaudio
|
-- https://core.telegram.org/bots/api#sendaudio
|
||||||
function utilities:send_audio(chat_id, file, reply_to_message_id, duration, performer, title)
|
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', {
|
local output = bindings.request(self, 'sendAudio', {
|
||||||
chat_id = chat_id,
|
chat_id = chat_id,
|
||||||
duration = duration or nil,
|
duration = duration or nil,
|
||||||
@ -84,6 +86,7 @@ end
|
|||||||
|
|
||||||
-- https://core.telegram.org/bots/api#senddocument
|
-- https://core.telegram.org/bots/api#senddocument
|
||||||
function utilities:send_document(chat_id, file, text, reply_to_message_id, reply_markup)
|
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', {
|
local output = bindings.request(self, 'sendDocument', {
|
||||||
chat_id = chat_id,
|
chat_id = chat_id,
|
||||||
caption = text or nil,
|
caption = text or nil,
|
||||||
@ -99,6 +102,7 @@ end
|
|||||||
|
|
||||||
-- https://core.telegram.org/bots/api#sendvideo
|
-- https://core.telegram.org/bots/api#sendvideo
|
||||||
function utilities:send_video(chat_id, file, text, reply_to_message_id, duration, width, height)
|
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', {
|
local output = bindings.request(self, 'sendVideo', {
|
||||||
chat_id = chat_id,
|
chat_id = chat_id,
|
||||||
caption = text or nil,
|
caption = text or nil,
|
||||||
@ -117,6 +121,7 @@ end
|
|||||||
-- NOTE: Voice messages are .ogg files encoded with OPUS
|
-- NOTE: Voice messages are .ogg files encoded with OPUS
|
||||||
-- https://core.telegram.org/bots/api#sendvoice
|
-- https://core.telegram.org/bots/api#sendvoice
|
||||||
function utilities:send_voice(chat_id, file, text, reply_to_message_id, duration)
|
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', {
|
local output = bindings.request(self, 'sendVoice', {
|
||||||
chat_id = chat_id,
|
chat_id = chat_id,
|
||||||
duration = duration or nil,
|
duration = duration or nil,
|
||||||
|
Reference in New Issue
Block a user