gcfabri
gcfabri

Reputation: 574

How to send Object in a postback button using Telegram channel via Bot Framework?

I am developing a bot using BotFramework (nodeJS) with four connected channels, one of these is the Telegram. Seems that Bot Framework translates the 'value' property field to 'callback_data' field from Telegram API, 'InlineKeyboardButton' method (docs). I am trying to send a payload from post-back button larger than 64B, resulting in an error like: 'Bad request: BUTTON_DATA_INVALID'

Take a look at snippet below at 'value' field in 'buttons' array, where the problem persists. Note: Other channels work properly sending this object as payload using the structure below.

This is the payload to be sent:

const foobar = {
  "d": {
    "a": {
      "b": "192.168.0.12",
      "c": "12345678",
      "d": "123123"
    },
    "e": {
      "f": "1",
      "g": "Test User",
      "h": [
        {
          "i": "1",
          "j": "Foobar"
        }
      ]
    }
  }
}

And this is the rich message structure used in Bot Framework to send a carousel through any channel that supports it, including the Telegram:

{
    type: `message`,
    attachmentLayout: `carousel`,
    text: ``,
    attachments: [
      {
        contentType: `application/vnd.microsoft.card.hero`,
        content: {
          text: `Test message text`,
          buttons: [
            {
              type: `postBack`,
              title: `Send Object`,
              value: `${JSON.stringify(foobar)}`
            }
          ]
        }
      }
    ]
  }

Once the user clicks on the button, I can 'hear' the payload on my bot controller and do my stuff. Eg.: Facebook Messenger, it works nicely.

Is there some alternative to send payload data using another Bot Framework rich component or a specific Telegram component copying the behavior of a post-back button? That is, the text on value is not shown to the user and the bot controller can 'hear' it once sent.

Upvotes: 2

Views: 1718

Answers (1)

Roman Daud
Roman Daud

Reputation: 11

you can convert object to string

Example:

events.on('news',function(data) {
    var options = {
        parse_mode :'Markdown',
        reply_markup : JSON.stringify({
            inline_keyboard : [
                [{ 
                    text : 'Read', 
                    callback_data : JSON.stringify({                   
                        'action' : 'read',
                        'url' : data.url
                    })
                }]
            ]
        })
    };
    bot.sendMessage(chat,data.text, options);
});

and inversely:

bot.on('callback_query', function (msg) {
    var data = JSON.parse(msg.data);
    //make action
}); 

Upvotes: 1

Related Questions