windkh / node-red-contrib-telegrambot

Telegram bot nodes for node-red.
Other
265 stars 118 forks source link

new: answerInlineQuery can pass optional options #194

Closed MorbidDevil closed 3 years ago

MorbidDevil commented 3 years ago

telegramBot.answerInlineQuery(inlineQueryId, results, [options]) ⇒ Promise

needed for inline_result pagination and other options

eg

msg.payload.options = { next_offset: nextOffset.toString(), cache_time: 30 };

MorbidDevil commented 3 years ago

example flow

[{"id":"3b0db5d830a948bd","type":"telegram event","z":"95afda0e0dc1a9a1","name":"","bot":"38996105.8d2c4e","event":"inline_query","autoanswer":false,"x":330,"y":260,"wires":[["45b00d85c952c03c","e594b655e5ad17e2"]]},{"id":"45b00d85c952c03c","type":"debug","z":"95afda0e0dc1a9a1","name":"inline_query","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":330,"y":220,"wires":[]},{"id":"6cf911dcd0b8099e","type":"telegram sender","z":"95afda0e0dc1a9a1","name":"answer inline_query","bot":"38996105.8d2c4e","haserroroutput":false,"outputs":1,"x":760,"y":260,"wires":[["aeb263b15dfd8532"]]},{"id":"aeb263b15dfd8532","type":"debug","z":"95afda0e0dc1a9a1","name":"answer inline_query","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1000,"y":260,"wires":[]},{"id":"8f4f184c6941072a","type":"telegram event","z":"95afda0e0dc1a9a1","d":true,"name":"","bot":"38996105.8d2c4e","event":"chosen_inline_result","autoanswer":true,"x":350,"y":460,"wires":[["12fc3b2c3e97de33"]]},{"id":"12fc3b2c3e97de33","type":"debug","z":"95afda0e0dc1a9a1","d":true,"name":"choosen inline result","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":360,"y":420,"wires":[]},{"id":"dbabe7eb65cc7bdf","type":"telegram event","z":"95afda0e0dc1a9a1","name":"","bot":"38996105.8d2c4e","event":"callback_query","autoanswer":true,"x":340,"y":360,"wires":[["e90d20918beafd35","92c019d97d5fa554"]]},{"id":"e90d20918beafd35","type":"function","z":"95afda0e0dc1a9a1","name":"set  answer options","func":"if (msg.payload.inlineMessageId) {\n    msg.payload.options = {\n        inline_message_id: msg.payload.inlineMessageId,\n    };\n\n    if ( msg.payload.content === \"XCANCEL\") {\n        msg.payload.type = \"editMessageReplyMarkup\";\n        msg.payload.content = JSON.stringify({\n            \"inline_keyboard\": []\n        });\n        return [ msg ];\n    }\n\n    // do cool stuff\n\n    return [ msg ];\n}\n\n\nmsg.payload.options = false;\nreturn [ msg ];\n","outputs":"1","noerr":0,"initialize":"","finalize":"","libs":[{"var":"fetch","module":"node-fetch"}],"x":590,"y":360,"wires":[["a1e28e4c82943a43","92fbc958a5eb1fb9"]]},{"id":"a1e28e4c82943a43","type":"telegram sender","z":"95afda0e0dc1a9a1","name":"answer callback query","bot":"38996105.8d2c4e","haserroroutput":false,"outputs":1,"x":880,"y":360,"wires":[["5d6a3d72e0455c2d"]]},{"id":"92c019d97d5fa554","type":"debug","z":"95afda0e0dc1a9a1","name":"callback query","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":340,"y":320,"wires":[]},{"id":"92fbc958a5eb1fb9","type":"debug","z":"95afda0e0dc1a9a1","name":"answer callback query","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":600,"y":320,"wires":[]},{"id":"5d6a3d72e0455c2d","type":"debug","z":"95afda0e0dc1a9a1","name":"callback query done","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":320,"wires":[]},{"id":"d00920cead4dcc7d","type":"debug","z":"95afda0e0dc1a9a1","name":"ERROR","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":120,"wires":[]},{"id":"ef6469249b2e7216","type":"catch","z":"95afda0e0dc1a9a1","name":"","scope":null,"x":320,"y":120,"wires":[["d00920cead4dcc7d"]]},{"id":"e594b655e5ad17e2","type":"function","z":"95afda0e0dc1a9a1","name":"create results","func":"// create mock data\nconst data = [];\nlet x = -1;\n[...Array(150).keys()].forEach((v,i) => {\n    if(i%25 === 0) {\n        x= x+1;\n        data[x] = [];\n    }\n    data[x].push({ id: i, title: `Title No ${i}`, url: \"https://www.google.de\" });\n})\n\n// handle pagination\nconst offset = msg.payload.offset;\nconst page = offset ? parseInt(offset, 10) : 0;\nconst nextPage = page < data.length ? page + 1 : \"\";\n\nmsg.payload.options = {\n    next_offset: nextPage.toString(),\n    cache_time: 30\n}\n\n// get x results per page\nconst results = data[page].map( (x) => { \n    return {\n        id: x.id,\n        type: \"article\",\n        title: `${x.title}`,\n        thumb_url: `https://freesvg.org/img/Placeholder.png`,\n\n        input_message_content: {\n            message_text: `${x.title}`,\n            parse_mode: \"Markdown\",\n            disable_web_page_preview: true\n        },\n        \n        reply_markup: {\n            inline_keyboard: [\n                [\n                    { text: \"request\", callback_data: x.id },\n                    { text: \"cancel\", callback_data: \"XCANCEL\" },\n                ],\n                x.url ? [{ text: \"visit web\", url: x.url }] : [],\n            ],\n        },\n    }\n});\n\n// return results\nmsg.payload.results = results;\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":260,"wires":[["6cf911dcd0b8099e"]]},{"id":"38996105.8d2c4e","type":"telegram bot","botname":"MorbidHomeBot","usernames":"MorbidDevil","chatids":"-1001452878568","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]