<?php

$bot_token = "7712681015:AAGbAzTvKnOn97pX0SgnGOYbfiP0O_QJuDE";

/* DATABASE */

$conn = mysqli_connect("localhost","verrifyi_unique","verrifyi_unique","verrifyi_unique");

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

/* MESSAGE */

if(isset($update["message"])){

$chat_id = $update["message"]["chat"]["id"];
$text = $update["message"]["text"];

$username = $update["message"]["from"]["username"];
$firstname = $update["message"]["from"]["first_name"];

if($username==""){
$username = $firstname;
}


/* USER MESSAGE TO ADMIN */

$admin = "7740857770"; // YOUR TELEGRAM ID

if(isset($update["message"]["text"])){

$chat_id = $update["message"]["chat"]["id"];
$text = $update["message"]["text"];

/* IGNORE ADMIN MESSAGE */

if($chat_id != $admin){

/* IGNORE START COMMAND */

if(strpos($text,"/start") === false){

$getUser = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM telegram_users WHERE chat_id='$chat_id'"));

if($getUser){

$phone = $getUser['phone'];
$username = $getUser['username'];

$msg = "📩 New Message From User\n\n";
$msg .= "👤 User: $username\n";
$msg .= "📱 Phone: $phone\n";
$msg .= "🆔 Chat ID: $chat_id\n\n";
$msg .= "💬 Message:\n$text";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$admin&text=".urlencode($msg));

}

}

}

}


/* ADMIN REPLY SYSTEM */

$admin = "7740857770"; // YOUR TELEGRAM ID

if(isset($update["message"]["reply_to_message"])){

$chat_id = $update["message"]["chat"]["id"];
$text = $update["message"]["text"];

if($chat_id == $admin){

$reply_msg = $update["message"]["reply_to_message"]["text"];

preg_match('/Chat ID: ([0-9]+)/',$reply_msg,$match);

$user_chat_id = $match[1];

$msg = "📩 Admin Reply\n\n$text";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$user_chat_id&text=".urlencode($msg));

}

}






/* CONNECT TELEGRAM */

if(strpos($text,"/start ") !== false){

$phone = str_replace("/start ","",$text);

$check = mysqli_query($conn,"SELECT * FROM telegram_users WHERE chat_id='$chat_id'");

if(mysqli_num_rows($check)==0){

mysqli_query($conn,"INSERT INTO telegram_users(chat_id,username,phone) VALUES('$chat_id','$username','$phone')");

$msg = "✅ Welcome to SAB FIND Bot 🙂\n\n";
$msg .= "✅ Your Telegram Connected Successfully.\n\n";
$msg .= "📌 Now you will receive notifications.";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=".urlencode($msg));

}

}

/* CHECK USER CONNECTED */

$checkUser = mysqli_query($conn,"SELECT * FROM telegram_users WHERE chat_id='$chat_id'");

if(mysqli_num_rows($checkUser)==0){

$msg = "❌ You are not connected with SAB FIND.\n\n";
$msg .= "Please login to website and connect Telegram.";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=".urlencode($msg));

exit;

}

/* GET PHONE */

$getPhone = mysqli_fetch_assoc(mysqli_query($conn,"SELECT phone FROM telegram_users WHERE chat_id='$chat_id'"));
$phone = $getPhone['phone'];

/* USER DATA */

$user = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM users WHERE phone='$phone'"));

$name = $user['name'];
$balance = $user['balance'];

/* START MENU */

$msg = "Hi $name\n\nCurrent Balance: ₹$balance";

$keyboard = [
"inline_keyboard"=>[
[
["text"=>"💰 Check Balance","callback_data"=>"balance"]
],
[
["text"=>"📄 Today Transactions","callback_data"=>"today"]
],
[
["text"=>"📑 Transaction History","callback_data"=>"history"]
]
]
];

$data = [
"chat_id"=>$chat_id,
"text"=>$msg,
"reply_markup"=>json_encode($keyboard)
];

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?".http_build_query($data));

}


/* BUTTON CLICK */

if(isset($update["callback_query"])){

$chat_id = $update["callback_query"]["message"]["chat"]["id"];
$data = $update["callback_query"]["data"];

/* CHECK USER */

$getPhone = mysqli_fetch_assoc(mysqli_query($conn,"SELECT phone FROM telegram_users WHERE chat_id='$chat_id'"));

if(!$getPhone){

$msg="❌ You are not connected.";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=".urlencode($msg));

exit;

}

$phone = $getPhone['phone'];

$user = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM users WHERE phone='$phone'"));


/* BALANCE */

if($data=="balance"){

$msg = "💰 Wallet Balance\n\n";
$msg .= "Name: ".$user['name']."\n";
$msg .= "User ID: ".$user['phone']."\n";
$msg .= "Balance: ₹".$user['balance'];

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=".urlencode($msg));

}


/* TODAY TRANSACTIONS */

if($data=="today"){

$q = mysqli_query($conn,"SELECT * FROM wallethistory WHERE userid='$phone' AND DATE(date)=CURDATE() ORDER BY id DESC LIMIT 20");

$msg = "Today's Transactions:\n\n";
$msg .= "No | Type | Amount | Closing\n";
$msg .= "| Time\n";
$msg .= "------------------------------------------------------------\n\n";

$i=1;

while($row=mysqli_fetch_assoc($q)){

$msg .= $i." | ".$row['purpose']." | ".$row['amount']." | ".$row['balance']."\n";
$msg .= "| ".$row['date']."\n\n";

$i++;

}

$text = "```\n".$msg."\n```";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=".urlencode($text)."&parse_mode=Markdown");

}


/* HISTORY */

if($data=="history"){

$q = mysqli_query($conn,"SELECT * FROM wallethistory WHERE userid='$phone' ORDER BY id DESC LIMIT 20");

$msg = "Transaction History\n\n";
$msg .= "No | Type | Amount | Closing\n";
$msg .= "| Time\n";
$msg .= "------------------------------------------------------------\n\n";

$i=1;

while($row=mysqli_fetch_assoc($q)){

$msg .= $i." | ".$row['purpose']." | ".$row['amount']." | ".$row['balance']."\n";
$msg .= "| ".$row['date']."\n\n";

$i++;

}

$text = "```\n".$msg."\n```";

file_get_contents("https://api.telegram.org/bot$bot_token/sendMessage?chat_id=$chat_id&text=".urlencode($text)."&parse_mode=Markdown");

}

}

?>