How can I stop my bot?


#1

Hello guys,

I am creating a Telegram bot using Telegraf and it won’t stop even when I just type the command once, for example:

bot.command('tip', pay(1))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))

bot.startPolling()

I have the function pay() and it is executing even when I haven’t typed in Telegram /tip and also when it keeps saying “Hey there” until I stop myself the bot. It is thought to say just once when I say hi in chat, any suggestion?

thanks in advance…


#2

The problem here is that you’re calling pay with the argument 1, which will happen right away, and then the result is passed to bot.command. If you want pay to be called on the tip command, then you need to give it the function itself:

bot.command('tip', pay);

If you want it to pay 1 when you tip, then you can create another function:

function pay1() {
  pay(1);
}

bot.command('tip', pay1);