I’m working on a Telegram bot and I’m wondering if there’s a way to get a user’s phone number just by knowing their Telegram user ID. Here’s what I’ve done so far:
Set up a bot using the Telegram Bot API
Installed the recommended NuGet package for C#
Successfully connected to the bot and can receive messages
When users send messages, I can see some of their info, but not their phone number. I need this to match users in our system.
Right now, users have to type their phone number like this:
dept 9811201243
But I want them to just type:
dept
Is there a way to get their phone number automatically? It would make things much easier for our users. Any help would be great!
hey, i’ve messed with telegram bots before. sadly, you can’t just grab phone numbers from IDs. telegram’s pretty strict about privacy stuff. but there’s a trick - you can add a button that asks users to share their number. it’s not automatic, but way easier than typing. just make sure to tell 'em why you need it, ya know?
I’ve encountered this issue before in my own projects. Unfortunately, retrieving a user’s phone number directly from their Telegram ID isn’t possible through the Bot API due to privacy restrictions. However, there’s a method that might work for you.
You can implement a ‘request contact’ button in your bot’s interface. When users click this, they’ll be prompted to share their phone number. It requires user action, but it’s more streamlined than manually typing numbers.
To set this up, use the ReplyKeyboardMarkup with a KeyboardButton that has the ‘request_contact’ parameter set to true. This creates a button that, when tapped, allows users to share their contact info with one click.
Keep in mind that users can still opt not to share their number, so you’ll need to handle cases where the number isn’t provided. Also, always be transparent about why you’re requesting this information to maintain user trust.
As someone who’s worked extensively with the Telegram Bot API, I can confidently say that retrieving a user’s phone number solely from their Telegram ID isn’t possible through the Bot API. This is a deliberate privacy measure by Telegram to protect user data.
However, there’s a workaround that might suit your needs. You can use the ‘KeyboardButtonRequestUser’ feature to request the user’s phone number. This prompts the user to share their phone number with your bot, but it requires explicit user consent.
In my experience, it’s best to explain to users why you need their phone number and assure them about data privacy. You could implement a command like ‘/sharephonenum’ that triggers this request. Remember, though, that users can always decline to share their number, so have a fallback plan.
If phone number matching is crucial for your system, you might want to consider alternative authentication methods or rethink your user identification strategy. It’s always a balance between user convenience and data privacy.