yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.12k stars 1.49k forks source link

I always get 401 unauthorized error when use test bot #1198

Open judy9712 opened 1 month ago

judy9712 commented 1 month ago

I have read:

I am using the latest version of the library.

I'm trying to connect my bot created in a test environment. However, even though I input the correct bot token, I keep getting a 401 unauthorized error.

this is my code:

import TelegramBot from 'node-telegram-bot-api';

require('dotenv').config();

const bot = new TelegramBot(TELEGRAM_TOKEN, {
  polling: true,
});

bot.on('message', (msg) => {
  const chatId = msg.chat.id;

  bot.sendMessage(chatId, 'Received your message');
});
bot.on('polling_error', (error) => {
  console.log({ error });
});

and this is my package.json

"dependencies": {
    "dotenv": "^16.4.5",
    "express": "^4.19.2",
    "node-telegram-bot-api": "^0.65.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/node": "^20.12.12",
    "@types/node-telegram-bot-api": "^0.64.6",
    "nodemon": "^3.1.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  }

I saw the testEnvironment option in api docs. However, when I try to input 'testEnvironment' in the options, it says that it's not a valid option. Has the 'testEnvironment' option been removed? How can I develop using a bot deployed in a test environment?

rock1nbvv commented 1 month ago

@judy9712 I've just bumped into this issue and suppressing the error helped, the API with test env still works

 // @ts-ignore
new TelegramBot(TOKEN || '', {polling: true, testEnvironment: true});

But it is still an issue with ConstructorOptions interface. I hope more comments will help devs to see it.