Callback_Querys sollten nun wie ewartet funktionieren

This commit is contained in:
Andreas Bielawski 2016-07-02 14:58:04 +02:00
parent 862335dc77
commit 88cc3f5e6b
3 changed files with 32 additions and 19 deletions

View File

@ -95,14 +95,30 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba
return return
end end
print('Callback Query, ausgelöst von '..callback.from.first_name..' ('..callback.from.id..')') if not callback.data:find(':') then
return
end
local called_plugin = callback.data:match('(.*):.*')
local param = callback.data:sub(callback.data:find(':')+1)
print('Callback Query "'..param..'" für Plugin "'..called_plugin..'" ausgelöst von '..callback.from.first_name..' ('..callback.from.id..')')
msg = utilities.enrich_message(msg) msg = utilities.enrich_message(msg)
for _,plugin in ipairs(self.plugins) do -- called_plugin:callback(callback, msg, self, config, param)
-- if is_plugin_disabled_on_chat(plugin.name, msg) then return end
for _, plugin in ipairs(self.plugins) do
if plugin.name == called_plugin then
if is_plugin_disabled_on_chat(plugin.name, msg) then return end
plugin:callback(callback, msg, self, config, param)
end
--plugin.callback(callback, msg, self, config, para)
end
-- called_plugin:callback(callback, msg, self, config, param)
--[[ for _,plugin in ipairs(self.plugins) do
if plugin.callback and msg then if plugin.callback and msg then
plugin:callback(callback, msg, self, config) plugin:callback(callback, msg, self, config)
end end
end end]]--
end end
function bot:run(config) function bot:run(config)

View File

@ -29,22 +29,20 @@ end
gImages.command = 'img <Suchbegriff>' gImages.command = 'img <Suchbegriff>'
function gImages:callback(callback, msg, self, config) function gImages:callback(callback, msg, self, config, input)
local input = utilities.input(callback.data)
utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..input..'"') utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..input..'"')
utilities.send_typing(self, msg.chat.id, 'upload_photo') utilities.send_typing(self, msg.chat.id, 'upload_photo')
local img_url, mimetype = gImages:get_image(input) local img_url, mimetype = gImages:get_image(input)
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, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"'..input..'"}]]}') result = utilities.send_document(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"gImages:'..input..'"}]]}')
else else
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, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"'..input..'"}]]}') result = utilities.send_photo(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"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":"'..input..'"}]]}') utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"gImages:'..input..'"}]]}')
return return
end end
end end
@ -53,7 +51,7 @@ function gImages:get_image(input)
local apikey = cred_data.google_apikey local apikey = cred_data.google_apikey
local cseid = cred_data.google_cse_id local cseid = cred_data.google_cse_id
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=' .. URL.escape(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)
if res == 403 then if res == 403 then
@ -95,18 +93,17 @@ function gImages:action(msg, config, matches)
end end
utilities.send_typing(self, msg.chat.id, 'upload_photo') utilities.send_typing(self, msg.chat.id, 'upload_photo')
local img_url, mimetype = gImages:get_image(input) local img_url, mimetype = gImages:get_image(URL.escape(input))
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, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"/img '..input..'"}]]}') result = utilities.send_document(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}')
else else
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, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"/img '..input..'"}]]}') result = utilities.send_photo(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}')
end end
if not result then if not result then
utilities.send_reply(self, msg, config.errors.connection, true) utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"gImages:'..URL.escape(input)..'"}]]}')
return return
end end
end end