Telegraf: /command@BotName_Bot is case sensitive

Created on 26 Dec 2017  路  3Comments  路  Source: telegraf/telegraf

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.

All 3 comments

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)
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Chibears85 picture Chibears85  路  3Comments

ghost picture ghost  路  3Comments

MohGanji picture MohGanji  路  3Comments

alkhimey picture alkhimey  路  3Comments

kuhel picture kuhel  路  3Comments