telegram-node-bot icon indicating copy to clipboard operation
telegram-node-bot copied to clipboard

How to get user's information?

Open ghost opened this issue 8 years ago • 6 comments

How can I get userId, first_name, last_name, etc from $ scope in controller? or is there any way to pass the User object to the controller ?

ghost avatar Apr 07 '17 07:04 ghost

I also have the same doubt.

ribeiroevandro avatar Apr 14 '17 07:04 ribeiroevandro

I do not know if this is the best way, but I was able to return the first name of the user, using scope._update._message._from._firstName, stored in a variable and returned where I needed it.

ribeiroevandro avatar Apr 14 '17 08:04 ribeiroevandro

Simply use scope.message._from. From that you can access ._id, ._firstName and so on.

riccardoscotti avatar Apr 28 '17 11:04 riccardoscotti

Hello guys, I'm pretty new in Telegram, do you have an example? I'm incorrectly making this: class StartController extends Telegram.TelegramBaseController { before(scope) { scope.someData = true

    return scope
}

startHandler($){ $.sendMessage('Good and bright day' + scope._update._message._from._firstName);

      }

sorry for english im from spain

RobotCharly avatar Feb 12 '18 18:02 RobotCharly

Scope is $. So for userId you can use: const userId = $.chatId; (because userId is same as chatId)

dobrosavljevicmilos avatar May 30 '18 11:05 dobrosavljevicmilos

@ghost This is how I get the userId, first_name, last_name from the scope ($)

const firstName = $.message.chat.firstName ? $.message.chat.firstName : $.message.chat.lastName;
const msg = $.message.text ? $.message.text : 'Not a text';
const userId = $.message.chat.id;

console.log(`First name: ${firstName}. Message: ${msg}. UserId: ${userId}`);

rotimi-best avatar Nov 08 '18 05:11 rotimi-best