SQL-array to Telegram inline keyboard

Hello all!
I have a Telegrambot in Node Red, which requests data from a database via an SQL statement.
The result is an array.
I want to display each item of this array (via a function) as a button in an inline keyboard in Telegram.
I'm using the node-red-contrib chatbot.
Who can help me?
Thanks in advance!

What have you so far?
have you looked at the examples in the import >> examples library?

Press ctrl i to see the examples.


I checked the examples on the website.
For the 'chatbot', there are no examples in the node-red installation.

The payload I get from the SQL is:

payload: array[1]
0: object
type: "postback"
value: "blah"
label: "blah"

or more items in the array:

payload: array[2]
0: object
type: "postback"
value: "blah"
label: "blah"
1: object
type: "postback"
value: "blah2"
label: "blah2"

Here is how I can manualy set the buttons:

msg.array = msg.payload
msg.arraylengte = msg.array.length
msg.payload = [
    type: 'postback',
    value: msg.array[0].filter,
    label: msg.array[0].filter
return msg;

I would like to have a solution that works regardless of the number of items in the array...

Confusingly, there are two Telegram nodes.

I think @E1cid is talking about...

Whereas, I think you are using...

Personally, I've had a lot more success (with inline keyboards) using telegrambot.
As @E1cid pointed out, there are many examples in the node's examples library.

I can supply a function to take sql result and create the telegram markup and set it to msg.payload.options, but as you are using the chatbot i can not help with that as I use telegrambot. The chatbot i believe accepts the same options for inline_keyboard mark up

[{"id":"48ee80a7.aedf98","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload.messageId","v":"1234567","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":1140,"wires":[["6c83b60c.47c5a8"]]},{"id":"6c83b60c.47c5a8","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"sql_data","pt":"msg","to":"[{\"label\":\"option1\",\"value\":\"45\"},{\"label\":\"option2\",\"value\":\"49\"},{\"label\":\"option3\",\"value\":\"38\"}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":140,"y":1180,"wires":[["922f958f.6b2a18"]]},{"id":"922f958f.6b2a18","type":"function","z":"b779de97.b1b46","name":"confirmation message","func":"const keyboard = {\"inline_keyboard\":[msg.sql_data.map(obj => {\n    return { \"text\": obj.label, \"callback_data\": obj.value }})]\n};\nconst opts = {\n  reply_to_message_id: msg.payload.messageId,\n  reply_markup: JSON.stringify(keyboard)\n};\n\nmsg.payload.content = 'Please select a button';\nmsg.payload.options = opts;\n\nreturn msg;\n","outputs":"1","noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":1220,"wires":[["68861cc0636dd0a0"]]},{"id":"68861cc0636dd0a0","type":"debug","z":"b779de97.b1b46","name":"selection","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":1280,"wires":[]}]

which should produce an object in msg.payload similar to this.

    "messageId": "1234567",
    "content": "Please select a button",
    "options": {
        "reply_to_message_id": "1234567",
        "reply_markup": "{\"inline_keyboard\":[[{\"text\":\"option1\",\"callback_data\":\"45\"},{\"text\":\"option2\",\"callback_data\":\"49\"},{\"text\":\"option3\",\"callback_data\":\"38\"}]]}"

Thank you! I found something that works for me!

msg.payload.buttons = msg.sql_data.map(obj => {return { type: 'postback', value: obj.filter, label: obj.filter }});

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.