php - 我如何从 Telegram Bot 获取用户 ID
问题描述
我想在用户与我的机器人聊天时从用户那里获取用户 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");
}
}
解决方案
这就是我获取 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);
}
推荐阅读
- django - Django:如何请求数据并同时使其可编辑
- flutter - 如何在 Flutter 中一次运行多个下载?
- prolog - 如何在 Prolog 的主体中动态地断言具有条件的规则?
- javascript - 使用 Puppeteer 从当前页面重定向到另一个页面后如何获取 URL?
- php - Laravel strtotime没有显示正确的时间
- javascript - 使用Reducer React 方法
- dplyr - 相交和变异
- c# - 如何在不阻塞主线程的情况下实现UDP
- java - Java HttpURLConnection - 发送密码表单帖子不起作用
- javascript - 我应该如何向其他本地服务器请求?