oscar bonelli
oscar bonelli

Reputation: 31

How can i get UserID from Telegram Bot

I wanna get the userid and chatid from a user when he is in a chat with my bot because i need that Id for send querys to Sql and register that user but i dont have idea how can i get the userid :(

$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;

And this code for send the UserID and ChatID to the chat

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");
    }
}

Upvotes: 2

Views: 7163

Answers (2)

oscar bonelli
oscar bonelli

Reputation: 31

This is the last code for get User ID i hope i will help somebody with this gl.

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


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

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

Upvotes: 0

oscar bonelli
oscar bonelli

Reputation: 31

This is how I get userid or chat_id I'm not very sure but now I need that information for use in another part of the code but when I call again $welcomemessage the value of userid is empty. What should i do?

$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);
}

Upvotes: 1

Related Questions