I have a list of phone numbers.
I need their matching username
and first_name
in Telegram(if they subscribed to Telegram).
I want to get these info(username and first_name) with Telegram BOT API.
Is it possible? How can I do this? (with PHP)
This is my sample code:
<?php
//telegram sdk and configuration
$client = new ZeleninTelegramBotApi('someRandomToken');
$update = json_decode(file_get_contents('php://input'));
//your app
try {
if($update->message->text != '/next_event_datetime')
exit;
$dom = SunraPhpSimpleHtmlDomParser::file_get_html('http://laratalks.com/');
$element = $dom->find('#location header.section-header h3', 0);
$dateTime = $element->plaintext;
$response = $client->sendMessage([
'chat_id' => $update->message->chat->id,
'text' => $dateTime
]);
} catch (ZeleninTelegramBotNotOkException $e) {
//echo error message ot log it
//echo $e->getMessage();
}
2
Answers
In short words it is IMPOSSIBLE. But read whole post, it may be useful for you.
Getting phone number from Telegram and via Telegram Bot API is one-way,
that means you can NOT send phone number and get users data from telegram.(Note: We assume you do not added users by phone number before)
There is only two ways to get users phone number in telegram:
after all, although these two ways can be used to grab user’s phone number but when you grab phone number you have, so your list is not useful.
BUT you want to grab
username
with phone number available,this is IMPOSSIBLE due to Telegram Bot API infrastructur .Well, you can get the username and
user_id
from a user when you have the phone number. I don’t know of any way to get the first name.Just send a contact (
sendContact
) with any first_name and the phone number. Telegram responds with the sent contact, which includes theuser_id
(if the user has Telegram) andusername
if the user has set one.Step by step: