# SQL-array to Telegram inline keyboard

**URL:** <https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609>\
**Category:** General\
**Created:** [2 July 2022 11:02 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609 "2022-07-02T11:02:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dajero](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@dajero](https://discourse.nodered.org/u/dajero)\
**Post date:** [2 July 2022 11:02 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/1 "2022-07-02T11:02:16Z")

</div>

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!  
Jeroen

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 July 2022 21:20 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/2 "2022-07-02T21:20:41Z")

</div>

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

 ![chrome_screenshot_1656796673535](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9dbe2c226cab8936ee9c196ef7bdbdc83cfb403d.jpeg)  
Press ctrl i to see the examples.

---

<div class="post-metadata">

**Author:** ![dajero](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@dajero](https://discourse.nodered.org/u/dajero)\
**Post date:** [4 July 2022 11:04 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/3 "2022-07-04T11:04:58Z")

</div>

Hi!

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:

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

```

or more items in the array:

```auto
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:

```auto
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...

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 July 2022 11:10 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/4 "2022-07-04T11:10:00Z")

</div>

Confusingly, there are two Telegram nodes.

I think @E1cid is talking about...

> **[node-red-contrib-telegrambot](https://flows.nodered.org/node/node-red-contrib-telegrambot)**
>
> Telegram bot nodes for Node-RED

Whereas, I think you are using...

> **[node-red-contrib-chatbot](https://flows.nodered.org/node/node-red-contrib-chatbot)**
>
> REDBot a Chat bot for a full featured chat bot for Telegram, Facebook Messenger and Slack. Almost no coding skills required

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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [4 July 2022 11:36 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/5 "2022-07-04T11:36:35Z")

</div>

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

```auto
[{"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.

```auto
{
    "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\"}]]}"
    }
}

```

> **[Telegram Bot API](https://core.telegram.org/bots/api#inlinekeyboardmarkup)**
>
> The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram.
> To learn how to create…

---

<div class="post-metadata">

**Author:** ![dajero](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@dajero](https://discourse.nodered.org/u/dajero)\
**Post date:** [5 July 2022 14:19 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/6 "2022-07-05T14:19:11Z")

</div>

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 }});`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 July 2022 14:19 UTC](https://discourse.nodered.org/t/sql-array-to-telegram-inline-keyboard/64609/7 "2022-07-19T14:19:45Z")

</div>

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