通过Telegram Bot API获取用户名和名字,并提供匹配的电话号码


Get username and first name via Telegram Bot API with available matching phone number

我有一个电话号码列表。

我需要他们在Telegram中匹配的usernamefirst_name(如果他们订阅了Telegram)。

我想通过Telegram BOT API获取这些信息(用户名和first_name)。

有可能吗?我该怎么做?(带PHP)

这是我的示例代码:

<?php
//telegram sdk and configuration
$client = new Zelenin'Telegram'Bot'Api('someRandomToken');
$update = json_decode(file_get_contents('php://input'));
//your app
try {
    if($update->message->text != '/next_event_datetime')
        exit;
    $dom = Sunra'PhpSimple'HtmlDomParser::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 ('Zelenin'Telegram'Bot'NotOkException $e) {
    //echo error message ot log it
    //echo $e->getMessage();
}

好吧,当你有电话号码时,你可以从用户那里获得用户名和user_id。我不知道该怎么取名字。

只需发送一个包含任意first_name和电话号码的联系人(sendContact)。Telegram以发送的联系人进行响应,其中包括user_id(如果用户有Telegram)和username(如果用户设置了一个)。

循序渐进:

  1. 在任何聊天中发送一个带有号码和任何名字的联系人(你自己,一个群,无关紧要…)
  2. Telegram API将以发送消息或一些错误代码进行响应。如果消息发送成功,则响应中的联系人可能具有更多详细信息(用户名和user_id)

简而言之,这是不可能的。但阅读整篇文章,它可能对你们有用。

从Telegram和通过Telegram Bot API获取电话号码是单向的,这意味着你不能发送电话号码,也不能从电报中获取用户数据。(注意:我们假设您之前没有通过电话号码添加用户)

只有两种方法可以在电报中获取用户的电话号码:

1-使用特定的KeyboardButton按钮获取。通过这种方式,Telegram在用户接受后向用户发送电话号码。(注意:用户必须是您之前的Bot用户)

2-询问用户他/她的电话号码,但它不可靠,你应该用其他方式检查(如短信批准等)

毕竟,虽然这两种方法可以用来获取用户的电话号码,但当您获取您的电话号码时,您的列表就没有用处了。

但你想用可用的电话号码获取username,这是不可能的,因为Telegram Bot API基础设施。