node-telegram-bot-api: setMyCommands not working properly

Bug Report

Expected Behavior

setMyCommands actual sets the commands of the bot

Actual Behavior

Not changes, but receiving a true

Steps to reproduce the Behavior

Using:

bot.setMyCommands([
  {command: "prueba", description: "Esto es una prueba"}
], { scope: {type: "all_group_chats"}, language_code: "es"});

returns true, but no changes applied.

I guess it’s the way is sending the info, because it uses internally x-www-form-urlencoded and sending it as JSON works:

const fetch = require('node-fetch');

// ....

bot.request = async function (command, json) {
  return fetch(`${bot.options.baseApiUrl}/bot${bot.token}/${command}`, {
    method: 'post',
    body: JSON.stringify(json),
    headers: { 'Content-Type': 'application/json' }
  });
}
  
bot.request('setMyCommands', {
   commands: [{command: "prueba", description: "Esto es una prueba"}],
   scope: {type: "all_group_chats"},
   language_code: "es"
});

I think is the mix of sending the array of commands as a stringified JSON while sending everything else as x-www-form-urlencoded format.

Maybe you should use JSON in the entire library? It seems to be the native way of Telegram API to work.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17

Most upvoted comments

I’m sending this for who doesn’t understand; ( P.S: If node-fetch not work, try with 2.6.7 version. You can install with; npm install node-fetch@2.6.7 , but don’t forget uninstall previous version with; npm uninstall node-fetch. ) Sorry for my poor English 😃

const telegramBot = require('node-telegram-bot-api');

const token = "YOUR_BOT_TOKEN_HERE";

const bot = new telegramBot(token, { polling: true });
const fetch = require('node-fetch');

bot.request = async function (command, json) {
    return fetch(`https://api.telegram.org/bot${token}/${command}`, {
      method: 'post',
      body: JSON.stringify(json),
      headers: { 'Content-Type': 'application/json' }
    });
}

bot.request('setMyCommands', {
    commands: [
        {
            command: "start",
            description: "This is start the game hehe..."
        }
    ],
    scope: {"type": "all_private_chats"},
    language_code: "en"
}).then(r => console.log(r));

bot.onText(/\/start/, (msg) => {

    bot.sendMessage(msg.chat.id, `Welcome.`, {
        "reply_markup": {
            "keyboard": [
                ["Sample text", "Second sample"]
            ]
        }
    });

});

I tried just now, working for me. ( setMyCommands with node-fetch )

Trying to pass the parameters in JSON.stringify() format does not recognize it either, although the server returns true

I tried restarting the client to see if the changes were made with no results.

error-bug