Implementation von Callback-Querys (auf ein neues!)
This commit is contained in:
parent
52e0ea8125
commit
862335dc77
@ -85,6 +85,26 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function bot:on_callback_receive(callback, msg, config) -- whenever a new callback is received
|
||||||
|
-- remove comment to enable debugging
|
||||||
|
-- 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)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
print('Callback Query, ausgelöst von '..callback.from.first_name..' ('..callback.from.id..')')
|
||||||
|
|
||||||
|
msg = utilities.enrich_message(msg)
|
||||||
|
for _,plugin in ipairs(self.plugins) do
|
||||||
|
if plugin.callback and msg then
|
||||||
|
plugin:callback(callback, msg, self, config)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function bot:run(config)
|
function bot:run(config)
|
||||||
bot.init(self, config) -- Actually start the script.
|
bot.init(self, config) -- Actually start the script.
|
||||||
|
|
||||||
@ -95,7 +115,7 @@ function bot:run(config)
|
|||||||
for _,v in ipairs(res.result) do -- Go through every new message.
|
for _,v in ipairs(res.result) do -- Go through every new message.
|
||||||
self.last_update = v.update_id
|
self.last_update = v.update_id
|
||||||
if v.callback_query then
|
if v.callback_query then
|
||||||
print('callback_query wird noch nicht unterstützt! Erhaltener Wert: '..v.callback_query.data)
|
bot.on_callback_receive(self, v.callback_query, v.callback_query.message, config)
|
||||||
elseif v.message then
|
elseif v.message then
|
||||||
bot.on_msg_receive(self, v.message, config)
|
bot.on_msg_receive(self, v.message, config)
|
||||||
end
|
end
|
||||||
|
@ -29,24 +29,27 @@ end
|
|||||||
|
|
||||||
gImages.command = 'img <Suchbegriff>'
|
gImages.command = 'img <Suchbegriff>'
|
||||||
|
|
||||||
function gImages:action(msg, config)
|
function gImages:callback(callback, msg, self, config)
|
||||||
local input = utilities.input(msg.text)
|
local input = utilities.input(callback.data)
|
||||||
if not input then
|
utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..input..'"')
|
||||||
if msg.reply_to_message and msg.reply_to_message.text then
|
|
||||||
input = msg.reply_to_message.text
|
|
||||||
else
|
|
||||||
utilities.send_message(self, msg.chat.id, gImages.doc, true, msg.message_id, true)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
print ('Checking if search contains blacklisted word: '..input)
|
|
||||||
if is_blacklisted(input) then
|
|
||||||
utilities.send_reply(self, msg, 'Vergiss es! ._.')
|
|
||||||
return
|
|
||||||
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)
|
||||||
|
|
||||||
|
if mimetype == 'image/gif' then
|
||||||
|
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..'"}]]}')
|
||||||
|
else
|
||||||
|
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..'"}]]}')
|
||||||
|
end
|
||||||
|
|
||||||
|
if not result then
|
||||||
|
utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"'..input..'"}]]}')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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'
|
||||||
@ -71,14 +74,35 @@ function gImages:action(msg, config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local i = math.random(jdat.queries.request[1].count)
|
local i = math.random(jdat.queries.request[1].count)
|
||||||
local img_url = jdat.items[i].link
|
return jdat.items[i].link, jdat.items[i].mime
|
||||||
|
end
|
||||||
|
|
||||||
if jdat.items[i].mime == 'image/gif' then
|
function gImages:action(msg, config, matches)
|
||||||
|
local input = utilities.input(msg.text)
|
||||||
|
if not input then
|
||||||
|
if msg.reply_to_message and msg.reply_to_message.text then
|
||||||
|
input = msg.reply_to_message.text
|
||||||
|
else
|
||||||
|
utilities.send_message(self, msg.chat.id, gImages.doc, true, msg.message_id, true)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print ('Checking if search contains blacklisted word: '..input)
|
||||||
|
if is_blacklisted(input) then
|
||||||
|
utilities.send_reply(self, msg, 'Vergiss es! ._.')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
utilities.send_typing(self, msg.chat.id, 'upload_photo')
|
||||||
|
local img_url, mimetype = gImages:get_image(input)
|
||||||
|
|
||||||
|
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)
|
result = utilities.send_document(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"/img '..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)
|
result = utilities.send_photo(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"/img '..input..'"}]]}')
|
||||||
end
|
end
|
||||||
|
|
||||||
if not result then
|
if not result then
|
||||||
|
@ -52,11 +52,12 @@ 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)
|
function utilities:send_photo(chat_id, file, text, reply_to_message_id, reply_markup)
|
||||||
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,
|
||||||
reply_to_message_id = reply_to_message_id
|
reply_to_message_id = reply_to_message_id,
|
||||||
|
reply_markup = reply_markup
|
||||||
}, {photo = file} )
|
}, {photo = file} )
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
print("Deleted: "..file)
|
print("Deleted: "..file)
|
||||||
@ -78,11 +79,12 @@ function utilities:send_audio(chat_id, file, reply_to_message_id, duration, perf
|
|||||||
end
|
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)
|
function utilities:send_document(chat_id, file, text, reply_to_message_id, reply_markup)
|
||||||
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,
|
||||||
reply_to_message_id = reply_to_message_id
|
reply_to_message_id = reply_to_message_id,
|
||||||
|
reply_markup = reply_markup
|
||||||
}, {document = file} )
|
}, {document = file} )
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
print("Deleted: "..file)
|
print("Deleted: "..file)
|
||||||
@ -149,6 +151,15 @@ function utilities:send_typing(chat_id, action)
|
|||||||
} )
|
} )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- https://core.telegram.org/bots/api#answercallbackquery
|
||||||
|
function utilities:answer_callback_query(callback, text, show_alert)
|
||||||
|
return bindings.request(self, 'answerCallbackQuery', {
|
||||||
|
callback_query_id = callback.id,
|
||||||
|
text = text,
|
||||||
|
show_alert = show_alert
|
||||||
|
} )
|
||||||
|
end
|
||||||
|
|
||||||
-- get the indexed word in a string
|
-- get the indexed word in a string
|
||||||
function utilities.get_word(s, i)
|
function utilities.get_word(s, i)
|
||||||
s = s or ''
|
s = s or ''
|
||||||
|
Reference in New Issue
Block a user