Telegraf: Help trying to make bot respond with first name in group

Created on 18 Oct 2018  路  3Comments  路  Source: telegraf/telegraf

I'm trying to make my bot respond to the command "/hello" with "Hello, (first name)" in a group, however I am struggleing trying to figure out how to make it output the first name of the person who triggers the command. For example, if "Ja Jodi" typed /hello I want the bot to respond with "Hello, Ja".

Currently all I have set up for the bot is:

bot.hears('/hello', (ctx) => ctx.reply('Hello {from.first_name}'))

Obviously, it reponds with literally "Hello {from.first_name}" instead of the actual first name.

Most helpful comment

bot.command('hello', (ctx)=> ctx.reply('Hello ' + ctx.message.from.first_name))

All 3 comments

Try:
bot.hears('/hello', (ctx) => ctx.reply(`Hello ${from.first_name}`))

bot.command('hello', (ctx)=> ctx.reply('Hello ' + ctx.message.from.first_name))
bot.command('hello', (ctx)=> ctx.reply('Hello ' + ctx.message.from.first_name))

While this is correct it may cause a message like _"Hello undefined"_

I rather suggest that you use a helper function to ensure that you do not get an _undefined_ in your greeting.

bot.hears('/hello', (ctx)=> ctx.reply('Hello ' + getName(ctx.message.from))

function getName(user) {
    return user.first_name
               || user.last_name
               || user.username
               || user.id; // this always exists
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sijie123 picture sijie123  路  3Comments

bostrot picture bostrot  路  3Comments

supermomme picture supermomme  路  3Comments

agstover picture agstover  路  3Comments

trgwii picture trgwii  路  3Comments