通过 Telegram Bot API 获取用户名和名字以及可用的匹配 phone 号码

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

我有一个包含 phone 个号码的列表。

我需要他们在 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();

}

简而言之,不可能。但是完整阅读post,它可能对你有用。

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

电报中获取用户phone号的方式只有两种:

1- Get it with specific KeyboardButton button. In this way Telegram sends users phone number after user accepted .( Note: User MUST be your Bot subscriber before)

2- Ask user him/her phone number, but it is not reliable and you should check it with other ways(like SMS approve and so on)

毕竟这两种方式虽然可以抢到用户的phone号,但是当你抢到phone号的时候,你的列表就没有用了。

但是你想用 phone 号码获取 username,由于 Telegram Bot API 基础设施,这是不可能的。

好吧,当您拥有 phone 号码时,您可以从用户那里获取用户名和 user_id。我不知道有什么方法可以得到名字。

只需发送一个联系人 (sendContact) 与任何 first_name 和 phone 号码。 Telegram 回复已发送的联系人,其中包括 user_id(如果用户有 Telegram)和 username(如果用户已设置)。

一步一步:

  1. 将联系人的号码和任何名字发送到任何聊天(您自己、群组,都无所谓...)
  2. Telegram API 将通过发送消息或一些错误代码进行响应。如果消息发送成功,回复中的联系人可能有更多详细信息(用户名和 user_id)。