【问题标题】:How can i get UserID from Telegram Bot我如何从 Telegram Bot 获取用户 ID
【发布时间】:2019-02-14 11:03:48
【问题描述】:

我想在用户与我的机器人聊天时从用户那里获取用户 ID 和聊天 ID,因为我需要该 ID 来向 Sql 发送查询并注册该用户,但我不知道如何获取用户 ID :(

$data = file_get_contents("php://input");
$update = json_decode($data,true);

$message = $update["message"];
$text = $message["text"];
$cid = $update["message"]["from"]["id"];
$from = $message["from"];
$username = $from["username"];
$nome = $from["first_name"];
$cognome = $form["last_name"];

$cbid = $update["callback_query"]["from"]["id"];
$cbdata = $update["callback_query"]["data"];
$chat_id = $message->chat->id;
$user_id = $message->from->id;
$user = $message->from->username;

此代码用于将 UserID 和 ChatID 发送到聊天室

if($text == "Datos"){
$but = array(array(array("text" => "UserID", "callback_data" => "Info"),),);
inlineKeyboard($but, $cid, "Clicca uno di questi due pulsanti \nper andare su google o su facebook!");
}

if(callback($update)){
    if($cbdata == "Info"){
    send($cbid, "estos son los datos: $probando $user $userid $probando2");
    }
}

【问题讨论】:

  • 传入的数据类型是什么?杰森?您是否尝试过 var_dump() 或 print_r() 从源头获取用户信息的数据数组,以查看该属性是否可用?
  • 您好,感谢您的回答:var_dump($data);这是信息: string(0) ""
  • 因此您没有来自该变量的数据。返回一个空字符串。
  • 是的,我没有来自 user_id 的数据,我想知道当他/她向机器人发送消息时,机器人如何从一个人那里获取用户 ID

标签: php json bots telegram


【解决方案1】:

这是获取用户 ID 的最后一个代码,我希望我能对这个 gl 有所帮助。

$userid = $message["from"]["id"];


if ($text == '/hope') {
    $userid;

    file_get_contents($website."/sendmessage?chat_id=".$chatId.);
}

【讨论】:

    【解决方案2】:

    这就是我获取 userid 或 chat_id 的方式,我不太确定,但现在我需要该信息用于代码的另一部分,但是当我再次调用 $welcomemessage 时,userid 的值为空。我该怎么办?

    $userid = $message["from"]["id"];
    
    $welcomemessage = "Este es tu id: $userid";
    
    if ($text == '/hope') {
        $welcomemessage;
    
        // Send the generated message back to the Telegram user
        file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$welcomemessage);
    }
    

    【讨论】:

    • 你能进一步解释你的代码吗?为什么中间有一行只包含$welcomemessage
    猜你喜欢
    • 1970-01-01
    • 2022-07-28
    • 2015-12-02
    • 2019-10-01
    • 1970-01-01
    • 2018-11-04
    • 2020-04-14
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多