I am using Telegram Bot API
for sending instant messages to users.
I have installed nuget package. This package is recommend by telegram developers.
I have created a telegram bot and successfully got access to it by using code. When I send messsage to bot, bot gets some info about sender.
I need the phone numbers of users to identify them in our system and send the information back to them.
My question is Can i get a user phone number by telegramUserId
?
I’m doing it for user convenience. If I could to get a user phone number I should’t have to ask for it from the user.
Now my command like this:
debt 9811201243
I want
debt
3
Answers
No, unfortunately
Telegram Bot API
doesn’t return phone number. You should either use Telegram API methods instead or ask it explicitly from the user. You cannot get “friends” of a user as well.You will definitely retrieve the following information:
userid
first_name
content
(whatever it is: text, photo, etc.)date
(unixtime)chat_id
If user configured it, you will also get
last_name
andusername
.It’s possible with bots 2.0 check out bot api docs.
https://core.telegram.org/bots/api#keyboardbutton
https://core.telegram.org/bots/2-0-intro#locations-and-numbers
With Telegram Bot API, you can get the phone number only when you request it from the user, but the user does not have to write the number, all he must do is to press a button in the conversation and the number will be sent to you.
When user clicks on
/myNumber
The user has to confirm:
You will get his number
This. is the console output:
Take a look at this Simple console application, but you need to do some changes to handle the number:
In
Handler.ch
add the following lines toBotOnMessageReceived
This is the piece of code needed in case the repository is deleted someday:
Program.cs
Handlers.cs