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.
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
}
Most helpful comment