yagop / node-telegram-bot-api

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

setMyCommands not working properly #949

Closed AdelaFoofur82 closed 2 years ago

AdelaFoofur82 commented 2 years ago

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.

IscoV commented 2 years ago

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

gladunvv commented 2 years ago

I will assume that the method works as it should, but you do not have users for whom you specify language_code or scope is not suitable for the group Try to change these parameters so that they satisfy the group and the user through whom you check the changes It is also important to switch to a third-party chat and come back, only in this case the changes will take effect

IscoV commented 2 years ago

I don't know how but it was solved

I tried setting the commands via BotFather, I don't know if this is what fixed the problem.

danielperez9430 commented 2 years ago

@gladunvv The scope object need to parse a JSON.stringify(). We can do it internal in the library the fix and you don't need to put it the stringify. Fix in the next release

cleivson commented 2 years ago

I believe the scope part of the setMyCommands and getMyCommands is also broken. The GetMyCommands call doesn't send the scope at all

bigbigsir commented 1 year ago

+1

renardein commented 1 year ago

+1

deulizealand commented 1 year ago

+1 same problem

DeadLyBro commented 1 year ago

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

vadimkonovod commented 1 year ago

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

Could you please share the payload for setMyCommands request?

DeadLyBro commented 1 year ago

Could you please share the payload for setMyCommands request?

Sure try this;

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

const token = "BOT_TOKEN"

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: "Command 1",
            description: "Command 1 Description."
        },
        {
            command: "Command 2",
            description: "Command 2 Description."
        }  
    ],
    scope: {"type": "all_private_chats"}, // This is adding commands for Private Message with Bot. Find Scopes on API.
    language_code: "en" // Language; en = English.
});

Edit: If you don't see commands wait a bit, or select other chat and back to chat with bot.

DeadLyBro commented 1 year ago

Oh also you don't need to add slash ( / ) inside command string.

royaljacques commented 1 year ago
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: "Command 1",
            description: "Command 1 Description."
        },
        {
            command: "Command 2",
            description: "Command 2 Description."
        }
    ],
    scope: {"type": "all_private_chats"}, // This is adding commands for Private Message with Bot. Find Scopes on API.
    language_code: "en" // Language; en = English.
}).then(r => console.log(r));

  [Symbol(realm)]: null,
  [Symbol(state)]: {
    aborted: false,
    rangeRequested: false,
    timingAllowPassed: true,
    requestIncludesCredentials: true,
    type: 'default',
    status: 400,
    timingInfo: {
      startTime: 333.6870999969542,
      redirectStartTime: 0,
      redirectEndTime: 0,
      postRedirectStartTime: 333.6870999969542,
      finalServiceWorkerStartTime: 0,
      finalNetworkResponseStartTime: 0,
      finalNetworkRequestStartTime: 0,
      endTime: 0,
      encodedBodySize: 78,
      decodedBodySize: 78,
      finalConnectionTimingInfo: null
    },
    cacheState: '',
    statusText: 'Bad Request',
    headersList: HeadersList {
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    },
    urlList: [ [URL] ],
    body: { stream: undefined }
  },
      'connection' => 'keep-alive',
      'strict-transport-security' => 'max-age=31536000; includeSubDomains; preload',
      'access-control-allow-origin' => '*',
      'access-control-expose-headers' => 'Content-Length,Content-Type,Date,Server,Connection'
    },
    [Symbol(headers map sorted)]: null
  }
}```
DeadLyBro commented 1 year ago

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"]
            ]
        }
    });

});
michael-tordil commented 4 months ago

anyone tried doing this in C#?

teolex commented 4 weeks ago

I'm doing what you guys are trying to do with java. I found this and it gives me like,

image

It doesn't even give me true or false. It's because it failed sending the SetMyCommands, obviously.

It seems that using 'https://api.telegram.org/bot${token}/setMyCommands' is the only way to achieve what we want. Isn't it?

DeadLyBro commented 4 weeks ago

I'm doing what you guys are trying to do with java. I found this and it gives me like,

image

It doesn't even give me true or false. It's because it failed sending the SetMyCommands, obviously.

It seems that using 'https://api.telegram.org/bot${token}/setMyCommands' is the only way to achieve what we want. Isn't it?

I use it with fetch (Node Fetch v2.6.7) using this url:

https://api.telegram.org/bot${token}/${command}

and set body with JSON strings, you can see my previous messages.