skip to Main Content

I’m using this package and I’ve created languages array and I’m trying to show them as buttons in my message but I receive this error

Unhandled rejection Error: ETELEGRAM: 400 Bad Request: can't parse inline keyboard button: InlineKeyboardButton must be an Object

Code

var languages = [
        'ENGLISH',
        'CHINESE',
        'FRENCH',
        'GERMAN',
        'DUTCH NL / BE',
        'SCANDINAVIAN(NORDISH / DANISH / SWEDISH)',
        'FINNISH / SUOMI',
        'ITALIAN',
        'HUNGARIAN',
        'BALKAN',
        'FILIPINO',
        'SPANISH',
        'RUSSIAN',
        'ARABIC',
        'TURKISH',
        'SLOVAKIAN',
        'ROUMANIAN',
        'PORTUGUESE',
        'HINDI / OURDOU / ENGLISH(PAKISTAN & INDIA)',
        'HINID / ENGLISH(ONLY INDIA)',
        'INDONESIAN',
        'MALAYSIAN',
        'ZIMBABWE',
        'NIGERIA',
        'Other'
    ];


    var options = languages.map(
        x => [{ text: x, callback_data: x }]
    );
    console.log(options);
    const languagesButtons = {
        parse_mode: "html",
        reply_markup: JSON.stringify({
            inline_keyboard: [
                options,
            ]
        })
    };
    console.log(languagesButtons);
    bot.sendMessage(chatId, "What language do you speak?", languagesButtons);

Results

console.log(options);

[
  [ { text: 'ENGLISH', callback_data: 'ENGLISH' } ],
  [ { text: 'CHINESE', callback_data: 'CHINESE' } ],
  [ { text: 'FRENCH', callback_data: 'FRENCH' } ],
  [ { text: 'GERMAN', callback_data: 'GERMAN' } ],
  [ { text: 'DUTCH NL / BE', callback_data: 'DUTCH NL / BE' } ],
  [
    {
      text: 'SCANDINAVIAN(NORDISH / DANISH / SWEDISH)',
      callback_data: 'SCANDINAVIAN(NORDISH / DANISH / SWEDISH)'
    }
  ],
  [ { text: 'FINNISH / SUOMI', callback_data: 'FINNISH / SUOMI' } ],
  [ { text: 'ITALIAN', callback_data: 'ITALIAN' } ],
  [ { text: 'HUNGARIAN', callback_data: 'HUNGARIAN' } ],
  [ { text: 'BALKAN', callback_data: 'BALKAN' } ],
  [ { text: 'FILIPINO', callback_data: 'FILIPINO' } ],
  [ { text: 'SPANISH', callback_data: 'SPANISH' } ],
  [ { text: 'RUSSIAN', callback_data: 'RUSSIAN' } ],
  [ { text: 'ARABIC', callback_data: 'ARABIC' } ],
  [ { text: 'TURKISH', callback_data: 'TURKISH' } ],
  [ { text: 'SLOVAKIAN', callback_data: 'SLOVAKIAN' } ],
  [ { text: 'ROUMANIAN', callback_data: 'ROUMANIAN' } ],
  [ { text: 'PORTUGUESE', callback_data: 'PORTUGUESE' } ],
  [
    {
      text: 'HINDI / OURDOU / ENGLISH(PAKISTAN & INDIA)',
      callback_data: 'HINDI / OURDOU / ENGLISH(PAKISTAN & INDIA)'
    }
  ],
  [
    {
      text: 'HINID / ENGLISH(ONLY INDIA)',
      callback_data: 'HINID / ENGLISH(ONLY INDIA)'
    }
  ],
  [ { text: 'INDONESIAN', callback_data: 'INDONESIAN' } ],
  [ { text: 'MALAYSIAN', callback_data: 'MALAYSIAN' } ],
  [ { text: 'ZIMBABWE', callback_data: 'ZIMBABWE' } ],
  [ { text: 'NIGERIA', callback_data: 'NIGERIA' } ],
  [ { text: 'Other', callback_data: 'Other' } ]
]

console.log(languagesButtons);

{
  parse_mode: 'html',
  reply_markup: '{"inline_keyboard":[[[{"text":"ENGLISH","callback_data":"ENGLISH"}],[{"text":"CHINESE","callback_data":"CHINESE"}],[{"text":"FRENCH","callback_data":"FRENCH"}],[{"text":"GERMAN","callback_data":"GERMAN"}],[{"text":"DUTCH NL / BE","callback_data":"DUTCH NL / BE"}],[{"text":"SCANDINAVIAN(NORDISH / DANISH / SWEDISH)","callback_data":"SCANDINAVIAN(NORDISH / DANISH / SWEDISH)"}],[{"text":"FINNISH / SUOMI","callback_data":"FINNISH / SUOMI"}],[{"text":"ITALIAN","callback_data":"ITALIAN"}],[{"text":"HUNGARIAN","callback_data":"HUNGARIAN"}],[{"text":"BALKAN","callback_data":"BALKAN"}],[{"text":"FILIPINO","callback_data":"FILIPINO"}],[{"text":"SPANISH","callback_data":"SPANISH"}],[{"text":"RUSSIAN","callback_data":"RUSSIAN"}],[{"text":"ARABIC","callback_data":"ARABIC"}],[{"text":"TURKISH","callback_data":"TURKISH"}],[{"text":"SLOVAKIAN","callback_data":"SLOVAKIAN"}],[{"text":"ROUMANIAN","callback_data":"ROUMANIAN"}],[{"text":"PORTUGUESE","callback_data":"PORTUGUESE"}],[{"text":"HINDI / OURDOU / ENGLISH(PAKISTAN & INDIA)","callback_data":"HINDI / OURDOU / ENGLISH(PAKISTAN & INDIA)"}],[{"text":"HINID / ENGLISH(ONLY INDIA)","callback_data":"HINID / ENGLISH(ONLY INDIA)"}],[{"text":"INDONESIAN","callback_data":"INDONESIAN"}],[{"text":"MALAYSIAN","callback_data":"MALAYSIAN"}],[{"text":"ZIMBABWE","callback_data":"ZIMBABWE"}],[{"text":"NIGERIA","callback_data":"NIGERIA"}],[{"text":"Other","callback_data":"Other"}]]]}'
}

Can you tell me what’s wrong?

Update

Based on CherryDT answer I can get my buttons but:

I have other inline keyboard which works as expected here is sample of it:

const contactKeyboardTwo = {
    parse_mode: "html",
    reply_markup: JSON.stringify({
        inline_keyboard: [
            [{ text: 'Website', url: 'https://www.google.com' }],
            [{ text: 'Chart', url: 'https://www.google.com' }],
            [{ text: 'How to buy', url: 'https://www.google.com' }],
            [{ text: 'Contract', url: 'https://www.google.com' }],
            [{ text: 'Contract', url: 'https://www.google.com' }]
        ]
    })
};
bot.sendMessage(chatId, "Welcome", contactKeyboardTwo);

result

one

Now new code return results like this

two

how can I have my languages list same as fist image?

3

Answers


  1. Chosen as BEST ANSWER

    Fixed

    All I needed to do was to remove [] from my inline_keyboard

    var options = languages.map(
      x => [{ text: x, callback_data: x }]
    );
    
    const languagesButtons = {
      parse_mode: "html",
      reply_markup: JSON.stringify({
        inline_keyboard: options, // removed [] around option
      })
    };
    

  2. The inline_keyboard array expects object of type InlineKeyboardButton like these (not the same library, but you can see the structure).

    I can see you attempted to do that here:

    var options = languages.map(
        x => [{ text: x, callback_data: x }]
    );
    

    But the problem is, this maps each language to an array with one element with the object inside, so at the end you get an array of arrays:

    [
        [
            { text: 'AAA', callback_data: 'AAA' }
        ],
        [
            { text: 'BBB', callback_data: 'BBB' }
        ]
    ]
    

    So, the error says that the button object is not an InlineKeyboardObject object because it’s actually an array and hence invalid.

    What you need is this:

    [
        { text: 'AAA', callback_data: 'AAA' },
        { text: 'BBB', callback_data: 'BBB' }
    ]
    

    …which you’ll get by removing the square brackets [ ] in your map callback (replacing them with ( ) instead because otherwise it would be parsed as block and not as object literal):

    var options = languages.map(
        x => ({ text: x, callback_data: x })
    );
    
    Login or Signup to reply.
  3. In my case InlineKeyboardObject was missing one of the optional parameters

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search