With the recipe
const bot = new Telegraf(process.env.BOT_TOKEN, {username: 'your_bot'})
// Or you can get username from Telegram server
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.telegram.getMe().then((botInfo) => {
bot.options.username = botInfo.username
})
bot.command('foo', (ctx) => ctx.reply('Hello World'))
I can get /command@BotName_Bot, but this is case sensitive. The .hears or .command will still fail if user tries /command@botname_bot.
bot.hears(/^\/foo (.+)/i, (ctx) => console.log(ctx.match))
Ah, that's a clean way to resolve it.
This is what I used in the end, I guess I can leave it here for reference.
bot.use((ctx, next) => {
if (ctx.message.text !== undefined) {
var regex = new RegExp('\@' + bot.options.username.toLowerCase(), 'i');
ctx.message.text = ctx.message.text.replace(regex, '');
}
return next(ctx);
})
This is more right way, I believe
bot.use((ctx, next) => {
if (ctx.message && typeof ctx.message.text !== 'undefined') {
ctx.message.text = ctx.message.text.replace(
new RegExp('\@' + bot.options.username, 'i'),
''
)
}
return next(ctx)
})