From 862335dc776cb8d754a6116d32b9a0f363e47047 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sat, 2 Jul 2016 12:35:14 +0200 Subject: [PATCH 1/3] Implementation von Callback-Querys (auf ein neues!) --- otouto/bot.lua | 24 ++++++++++++-- otouto/plugins/gImages.lua | 64 ++++++++++++++++++++++++++------------ otouto/utilities.lua | 21 ++++++++++--- 3 files changed, 82 insertions(+), 27 deletions(-) diff --git a/otouto/bot.lua b/otouto/bot.lua index 9378400..4516b00 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -85,6 +85,26 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec 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) 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. self.last_update = v.update_id 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 bot.on_msg_receive(self, v.message, config) end @@ -320,4 +340,4 @@ function create_cred() print ('saved credentials into reds hash telegram:credentials') end -return bot +return bot \ No newline at end of file diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index cd1f13d..9bb770f 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -29,24 +29,27 @@ end gImages.command = 'img ' -function gImages:action(msg, config) - 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 +function gImages:callback(callback, msg, self, config) + local input = utilities.input(callback.data) + utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..input..'"') + utilities.send_typing(self, msg.chat.id, 'upload_photo') + local img_url, mimetype = gImages:get_image(input) - print ('Checking if search contains blacklisted word: '..input) - if is_blacklisted(input) then - utilities.send_reply(self, msg, 'Vergiss es! ._.') - return + 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 - utilities.send_typing(self, msg.chat.id, 'upload_photo') + 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 cseid = cred_data.google_cse_id local BASE_URL = 'https://www.googleapis.com/customsearch/v1' @@ -71,14 +74,35 @@ function gImages:action(msg, config) end 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 + +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 - if jdat.items[i].mime == 'image/gif' then + 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') - 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 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 if not result then @@ -87,4 +111,4 @@ function gImages:action(msg, config) end end -return gImages +return gImages \ No newline at end of file diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 13847fe..05fd49d 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -52,11 +52,12 @@ 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) +function utilities:send_photo(chat_id, file, text, reply_to_message_id, reply_markup) local output = bindings.request(self, 'sendPhoto', { chat_id = chat_id, 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} ) os.remove(file) print("Deleted: "..file) @@ -78,11 +79,12 @@ function utilities:send_audio(chat_id, file, reply_to_message_id, duration, perf end -- 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', { chat_id = chat_id, 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} ) os.remove(file) print("Deleted: "..file) @@ -149,6 +151,15 @@ function utilities:send_typing(chat_id, action) } ) 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 function utilities.get_word(s, i) s = s or '' @@ -775,4 +786,4 @@ function url_encode(str) return str end -return utilities +return utilities \ No newline at end of file From 88cc3f5e6b511ab47bd74c991f5944a06f8cf76e Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sat, 2 Jul 2016 14:58:04 +0200 Subject: [PATCH 2/3] Callback_Querys sollten nun wie ewartet funktionieren --- otouto/bot.lua | 26 +++++++++++++++++++++----- otouto/plugins/gImages.lua | 23 ++++++++++------------- otouto/utilities.lua | 2 +- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/otouto/bot.lua b/otouto/bot.lua index 4516b00..d0d4bf6 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -94,15 +94,31 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba 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..')') + + 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) - 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 plugin:callback(callback, msg, self, config) end - end + end]]-- end function bot:run(config) @@ -340,4 +356,4 @@ function create_cred() print ('saved credentials into reds hash telegram:credentials') end -return bot \ No newline at end of file +return bot diff --git a/otouto/plugins/gImages.lua b/otouto/plugins/gImages.lua index 9bb770f..ecc8eef 100644 --- a/otouto/plugins/gImages.lua +++ b/otouto/plugins/gImages.lua @@ -29,22 +29,20 @@ end gImages.command = 'img ' -function gImages:callback(callback, msg, self, config) - local input = utilities.input(callback.data) +function gImages:callback(callback, msg, self, config, input) utilities.answer_callback_query(self, callback, 'Suche nochmal nach "'..input..'"') 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..'"}]]}') + result = utilities.send_document(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal suchen","callback_data":"gImages:'..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..'"}]]}') + result = utilities.send_photo(self, msg.chat.id, file, img_url, msg.message_id, '{"inline_keyboard":[[{"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":"'..input..'"}]]}') + utilities.send_reply(self, msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"gImages:'..input..'"}]]}') return end end @@ -53,7 +51,7 @@ function gImages:get_image(input) local apikey = cred_data.google_apikey local cseid = cred_data.google_cse_id 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) if res == 403 then @@ -95,20 +93,19 @@ function gImages:action(msg, config, matches) end 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 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 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 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 end end -return gImages \ No newline at end of file +return gImages diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 05fd49d..05d40ed 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -786,4 +786,4 @@ function url_encode(str) return str end -return utilities \ No newline at end of file +return utilities From 340ca1f556acafccd5a1746aacb4e6e4483b840e Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Sat, 2 Jul 2016 15:03:54 +0200 Subject: [PATCH 3/3] removed echo and die statements, lolz. --- otouto/bot.lua | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/otouto/bot.lua b/otouto/bot.lua index d0d4bf6..eda4b5d 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -86,7 +86,7 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec end function bot:on_callback_receive(callback, msg, config) -- whenever a new callback is received - -- remove comment to enable debugging + -- remove comments to enable debugging -- vardump(msg) -- vardump(callback) @@ -104,21 +104,12 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba print('Callback Query "'..param..'" für Plugin "'..called_plugin..'" ausgelöst von '..callback.from.first_name..' ('..callback.from.id..')') msg = utilities.enrich_message(msg) - -- 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) + 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 - -- called_plugin:callback(callback, msg, self, config, param) - --[[ for _,plugin in ipairs(self.plugins) do - if plugin.callback and msg then - plugin:callback(callback, msg, self, config) - end - end]]-- + end end function bot:run(config)