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
Now new code return results like this
how can I have my languages list same as fist image?
3
Answers
Fixed
All I needed to do was to remove
[]
from myinline_keyboard
The
inline_keyboard
array expects object of typeInlineKeyboardButton
like these (not the same library, but you can see the structure).I can see you attempted to do that here:
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:
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:
…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):In my case
InlineKeyboardObject
was missing one of the optional parameters