yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.45k stars 1.53k forks source link

Cannot get reply_markup by array #871

Closed robertnicjoo closed 3 years ago

robertnicjoo commented 3 years ago

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 point the issue?