node.jstelegramtelegram-bot

can't parse inline keyboard button: InlineKeyboardButton


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?


Solution

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