yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.35k stars 1.52k forks source link

How to trigger bot function on start? #1063

Closed RasmonT closed 1 year ago

RasmonT commented 1 year ago

Hello guys,

I try to understand how to trigger action on the bot starts, I didn't see anything for this even in the documentation. How can I trigger let's say console.log once bot is online?

bot.on('ready', () => {

    console.log('Bot started!')

});

I was looking for something like this, but I don't know how to do it in this Library. Any ideas?

yokkkoso commented 1 year ago

const bot = new TelegramBot(token, {
  polling: false,
});

function start() {
  void bot.startPolling();
  console.log('Bot started');
}

start();
RasmonT commented 1 year ago

Thanks :-)

By the way I found another solution and this works also fine for me.

bot.getMe().then((me) => {
    console.log(`Bot started as @${me.username}`);
  }).catch((error) => {
    console.error(error);
  });