Ad

Telegram Bot : Button Text Is Not Readable

- 1 answer

I have developed telegram bot which responds to users input.But when bot send buttons to user, text of the button is not readable.

Below is the screenshot of chat: enter image description here

I think telegram does not resize button when number of buttons are more or button text length is more.

I am using below code to send buttons:

buttonoptions.forEach(buttonElement => {

    var buttonText = buttonElement;
    if (buttonText.length > 30) {
        buttonText = buttonText.substring(0, 27);
        buttonText = buttonText + "...";
    }

    var buttonItem = {
        text: buttonText,
        callback_data: JSON.stringify({
            'type': 'button',
            'text': buttonText
        })
    }

    buttons.push(buttonItem);
});

var message = {
    parse_mode: 'Markdown',
    reply_markup: JSON.stringify({
        inline_keyboard: [buttons]
    })
};

//send message code

So is there any way to force telegram bot to show complete text of button?

Ad

Answer

Telegram Bot API takes an array of arrays of buttons as a value of inline_keyboard field. Every array represents a row of buttons in inline keyboard.

So, you have many buttons on the same row, the text looks truncated. To avoid this you can place buttons on several rows.

E.g.:

const options = {
  reply_markup: JSON.stringify({
      inline_keyboard: [
          [{ text: 'Button 1', callback_data: '1' }],
          [{ text: 'Button 2', callback_data: 'data 2' }],
          [{ text: 'Button 3', callback_data: 'text 3' }]
      ]
   })
};

This results in three rows with single button on each. I believe the text will not be truncated.

Cheers!

Ad
source: stackoverflow.com
Ad