<?php
$telegram_ip_ranges = [
['lower' => '149.154.160.0', 'upper' => '149.154.175.255'], // literally 149.154.160.0/20
['lower' => '91.108.4.0',    'upper' => '91.108.7.255'],    // literally 91.108.4.0/22
];
$ip_dec = (float) sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
$ok=false;
foreach ($telegram_ip_ranges as $telegram_ip_range) if (!$ok) {
    $lower_dec = (float) sprintf("%u", ip2long($telegram_ip_range['lower']));
    $upper_dec = (float) sprintf("%u", ip2long($telegram_ip_range['upper']));
    if ($ip_dec >= $lower_dec and $ip_dec <= $upper_dec) $ok=true;
}
if (!$ok) die("Sik!");
date_default_timezone_set('Asia/Tehran');
error_reporting(0);
#این پایین بجای Bot Token توکنتونو بزارید
define('API_KEY','1739162363:AAHb8nS08fm3bgtby2SUZCigR8hWweF-a44'); 
ini_set("log_errors","off");
#---------------Variables----------#
$update = json_decode(file_get_contents('php://input'));
$message = $update->message;
$chat_id = $message->chat->id;
$message_id = $message->message_id;
$from_id =  isset($update->message->from->id)?$update->message->from->id:$update->callback_query->from->id;
$text = isset($update->message->text)?$update->message->text:$update->callback_query->data;
$user = json_decode(file_get_contents("data/$from_id.json"),true);
$step = $user["step"];
$time = date('H:i:s');
$date = date('Y/m/d');
$now = date('Y/m/d H:i:s');
@mkdir("data");
# این پایین بجای ادمین یوزر ایدی ایدی عددی خودتونو قرار بدید
$admin = "389435672";
if ($chat_id != $admin) {
$menu = json_encode([
           'keyboard'=>[
   	[['text'=>"خرید سوال و کلید🛒"],['text'=>"ارتباط با مدیر🔰"]],
	[['text'=>"سوالات متداول🆘"]]],
		"resize_keyboard"=>true,
	 
	 ]);
}else{
$menu = json_encode([
           'keyboard'=>[
 	[['text'=>"خرید سوال و کلید🛒"],['text'=>"ارتباط با مدیر🔰"]],
   	[['text'=>"سوالات متداول🆘"]],[['text'=>"Admin panel 👨🏻‍💻"]],
   	],
		"resize_keyboard"=>true,
	 
	 ]); 
}
$panel = json_encode([
           'keyboard'=>[
           [['text'=>"Status 🤖"],['text'=>""]],
           [['text'=>"For all 📩"],['text'=>"PM all 📩"]],
           [['text'=>"Messages 📒"],['text'=>"Delete spam 🗑"]],
   	[['text'=>"لغو❌"],['text'=>"✅ | ❌"]],
	],
		"resize_keyboard"=>true,
	 
	 ]);
	 $onoff = json_encode([
           'keyboard'=>[
           [['text'=>"Block ❌"],['text'=>"Unblock ✅"]],
           [['text'=>"Bot off ❌"],['text'=>"Bot on ✅"]],
   	[['text'=>"Back to panel"],['text'=>""]],
	],
		"resize_keyboard"=>true,
	 
	 ]);
	 	 $back = json_encode([
           'keyboard'=>[
   	[['text'=>"لغو❌"]],
	],
		"resize_keyboard"=>true,
	 
	 ]);
	 	 	 $backP = json_encode([
           'keyboard'=>[
   	[['text'=>"Back to panel"]],
	],
		"resize_keyboard"=>true,
	 
	 ]);
	 	 $texts = json_encode([
           'keyboard'=>[
           [['text'=>"Set send 📍"],['text'=>"Set Profile📍"]],[['text'=>"Set Profile1📍"]],
           [['text'=>"Set Start📍"],['text'=>"Set PM📍"]],
   	[['text'=>"Back to panel"],['text'=>""]],
	],
		"resize_keyboard"=>true,
	 
	 ]);
	 	$inline_keyboard = json_encode([
		            'inline_keyboard'=>[
		[['text'=>"Answer 📩",'callback_data'=>"rep-$from_id"],['text'=>"⛔️ Block",'callback_data'=>"ban-$from_id"]],
              ],
              "resize_keyboard"=>true,
        ]);

	
#---------------------------------#
#----------------Funtions-----------------#
function bot($method,$datas=[]){
    $url = "https://api.telegram.org/bot".API_KEY."/".$method;
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
    $res = curl_exec($ch);
    if(curl_error($ch)){
        var_dump(curl_error($ch));
    }else{
        return json_decode($res);
    }
}
function SendMessage($chat_id, $text, $mode = null, $reply = null, $keyboard = null)
{
	return bot('sendMessage', [
		'chat_id' => $chat_id,
		'text' => $text,
		'parse_mode' => $mode,
		'reply_to_message_id' => $reply,
		'reply_markup' => $keyboard,
		'disable_web_page_preview' => true
	]);
}


function Spam($user_id){
@mkdir("data/spam");
$spam_status = json_decode(file_get_contents("data/spam/$user_id.json"),true);
if($spam_status != null){
if(mb_strpos($spam_status[0],"time") !== false){
if(str_replace("time ",null,$spam_status[0]) >= time())
exit(false);
else
$spam_status = [1,time()+2];
}
function deletefolder($path){
 if($handle=opendir($path)){
  while (false!==($file=readdir($handle))){
   if($file<>"." AND $file<>".."){
    if(is_file($path.'/'.$file)){ 
     @unlink($path.'/'.$file);
    } 
    if(is_dir($path.'/'.$file)) { 
     deletefolder($path.'/'.$file); 
     @rmdir($path.'/'.$file); 
    }
   }
        }
    }
}

#---------------------------------#
# Dev : @FuckerDev - Channel : @DaieTeam
#-------------START---------------#
if(time() < $spam_status[1]){
if($spam_status[0]+1 > 3){
$time = time() + 60;
$spam_status = ["time $time"];
file_put_contents("data/spam/$user_id.json",json_encode($spam_status,true));
bot('SendMessage',[
'chat_id'=>$user_id,
'text'=>"⚠️کمی اهسته تر با ربات کار کنید

⛔️حساب شما به مدت 1 دقیقه محدود شد",
]);
exit(false);
}else{
$spam_status = [$spam_status[0]+1,$spam_status[1]];
}}else{
$spam_status = [1,time()+2];
}}else{
$spam_status = [1,time()+2];
}
file_put_contents("data/spam/$user_id.json",json_encode($spam_status,true));
}
Spam($from_id);
if($step == "ban"){
SendMessage($chat_id, "You are Blocked from bot ⛔️",null,null,null);
}else{

					if(!file_exists("data/settings.json")) {
$settings["mode"] = "on";
$settings["start"] = "Hello, welcome to my messenger 🖐🏻😄";
$settings["send"] = "Sent ✅";
$settings["profile"] = "empty..";
$settings["profile1"] = "empty..";
$settings["SendMessage"] = "لطفا پیام خود را بفرستید";
$settings["homeBack"] = "با موفقیت لغو گردید";
$outjsons = json_encode($settings,true);
file_put_contents("data/settings.json",$outjsons);
}
if (!file_exists("data/$from_id.json")) {
        $myfile2 = fopen("data/members.txt", "a") or die("Unable to open file!");
        fwrite($myfile2, "$from_id\n");
        fclose($myfile2);
		 $user["step"] = "sup";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
    }

        $settings = json_decode(file_get_contents("data/settings.json"),true);
    $mode = $settings["mode"];
    $start = $settings["start"];
    $send = $settings["send"];
    $profile = $settings["profile"];
	$profile1 = $settings["profile1"];
	$homeBack = $settings["homeBack"];
    $sendmessage = $settings["SendMessage"];
    if($mode != "on" and $chat_id != $admin){
SendMessage($chat_id, "Bot is off",null,null,null);
}
if($text == "/start"){
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($chat_id, "$start",null,null, $menu);
}
if($text == "لغو❌"){
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($chat_id, "$homeBack",null,null, $menu);
}
	if($text == "ارتباط با مدیر🔰"){
$user["step"] = "sup";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($chat_id, "$sendmessage",null,null, $back);
}
	if($step == "sup" and $text != "لغو❌"){
SendMessage($chat_id, "$send",null,null,null); 
		   bot('ForwardMessage',[
	'chat_id'=>$admin,
	'from_chat_id'=>$from_id,
	'message_id'=>$message_id
	]);
	bot('sendMessage',[
'chat_id'=>$admin,
'text'=>"new message
 -------------
• Name : $first_name
• id : <code>$from_id</code>
• Username : @$username
 -------------
$now",
'parse_mode'=>"HTML",
'reply_markup'=>json_encode([
'inline_keyboard'=>[
		[['text'=>"Answer 📩",'callback_data'=>"rep-$from_id"],['text'=>"⛔️ Block",'callback_data'=>"ban-$from_id"]],
],
"resize_keyboard"=>true,'one_time_keyboard' => true,
])
]);
}


elseif($text == "خرید سوال و کلید🛒"){
bot('sendMessage',[
'chat_id'=>$chat_id,
'text'=>"$profile",
'parse_mode'=>"MarkDown",  
]);
}
elseif($text == "سوالات متداول🆘"){
bot('sendMessage',[
'chat_id'=>$chat_id,
'text'=>"$profile1",
'parse_mode'=>"MarkDown",  
]);
}

#---------------------------------#
# Dev : @FuckerDev - Channel : @DaieTeam
#-------------PANEL---------------#
	 if($chat_id == "$admin" and $text == "Admin panel 👨🏻‍💻" or $text == "/panel" or $text == "Back to panel"){
	     $user["step"] = "panel";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($chat_id, "Welcome to your messaging robot panel \n help: /help",null,null, $panel);

	 }
	  elseif($chat_id == $admin and $text == "Status 🤖"){
$load = sys_getloadavg();
$alluser = file_get_contents("data/members.txt");
	$alaki = explode("\n",$alluser);
	$allusers = count($alaki)-1;
   $count = count($alaki)-12;
	$lastmember = null;
	foreach($alaki as $key => $value){
		if($count <= $key){
			$lastmember .= "[$value](tg://user?id=$value) | ";
			$key++;
	}
	}
		  bot('sendMessage',[
 'chat_id'=>$admin,
 'text'=>"Bot Status 📊 :
 Last Members(12) : 
 $lastmember
 -----------------
 Count Members : $allusers
 ",
 'parse_mode'=>"MarkDown",
  'reply_to_message_id'=>$message_id,
  'reply_markup'=>json_encode([
            'inline_keyboard'=>[
            [['text'=>"📍Ping : $load[0]",'callback_data'=>'none'],['text'=>"",'callback_data'=>'none']],
             [['text'=>"⏰ Time",'callback_data'=>'none'],['text'=>"$time",'callback_data'=>'none']],
              [['text'=>"📅 Date",'callback_data'=>'none'],['text'=>"$date",'callback_data'=>'none']],
              ]
        ])
   ]);
   }
	 if($chat_id == "$admin" and $text == "Messages 📒"){
SendMessage($chat_id, "Set bot messages",null,null, $texts);

	 }
	 	 if($chat_id == "$admin" and $text == "✅ | ❌"){
SendMessage($chat_id, "Set on and off and block unblocking ",null,null, $onoff);

	 }
	 	  elseif($chat_id == $admin and $text == "Set PM📍"){
$user["step"] = "set-mtner";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "set pm text",null,null, $backP);
}
elseif($step == "set-mtner" and $chat_id == $admin and $text != "Back to panel"){
$settings["SendMessage"] = "$text";
$outjsons = json_encode($settings,true);
file_put_contents("data/settings.json",$outjsons);
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "was set ✅",null,null, $backP);
}
	 elseif($chat_id == $admin and $text == "/help"){
SendMessage($admin, 'به راهنمای ربات خودتون خوش اومدیدبه راهنمای ربات خودتون خوش اومدید 
برای بلاک کردن و انبلاک کردن کاربر و خاموش و روشن کردن ربات دکمه "✅ | ❌"رو بزنید 
برای تغییر متن های ربات دکمه "Messages 📒" رو بزنید 
برای دریافت امار ربات دکمه "Status 🤖" رو بزنید
 برای پاکسازی پوشه اسپم دکمه "Delete spam 🗑"رو بزنید 
 برای پیام همگانی و فوروارد همگانی به ترتیب دکمه های "PM all 📩" و "For all 📩"
 بزنید  برنامه نویس :
@DevFucker
اپن شده در :
@DaieTeam',null,null, null);
}
	  elseif($chat_id == $admin and $text == "Set Profile📍"){
$user["step"] = "set-mtnpro";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "set profile text",null,null, $backP);
}
elseif($step == "set-mtnpro" and $chat_id == $admin and $text != "Back to panel"){
$settings["profile"] = "$text";
$outjsons = json_encode($settings,true);
file_put_contents("data/settings.json",$outjsons);
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "was set ✅",null,null, $backP);
} 
elseif($chat_id == $admin and $text == "Set Profile1📍"){
$user["step"] = "set-mtnpro1";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "set profile text",null,null, $backP);
}
elseif($step == "set-mtnpro1" and $chat_id == $admin and $text != "Back to panel"){
$settings["profile1"] = "$text";
$outjsons = json_encode($settings,true);
file_put_contents("data/settings.json",$outjsons);
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "was set ✅",null,null, $backP);
}
	  elseif($chat_id == $admin and $text == "Set send 📍"){
$user["step"] = "set-mtnsend";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "set send text",null,null, $backP);
}
elseif($step == "set-mtnsend" and $chat_id == $admin and $text != "Back to panel"){
$settings["send"] = "$text";
$outjsons = json_encode($settings,true);
file_put_contents("data/settings.json",$outjsons);
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "was set ✅",null,null, $backP);
}
	 elseif($chat_id == $admin and $text == "Set Start📍"){
$user["step"] = "set-mtnstart";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "set start text",null,null, $backP);
}
elseif($step == "set-mtnstart" and $chat_id == $admin and $text != "Back to panel"){
$settings["start"] = "$text";
$outjsons = json_encode($settings,true);
file_put_contents("data/settings.json",$outjsons);
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "was set ✅",null,null, $backP);
}
	 elseif($chat_id == $admin and $text == "Delete spam 🗑"){
	 deletefolder("data/spam");
SendMessage($admin, "delted ✅",null,null, $panel);
}

	 elseif($chat_id == $admin and $text == "Unblock ✅"){
$user["step"] = "set-unban-user";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "send user id",null,null, $backP);
}
elseif($chat_id == $admin and $text != "Back to panel" and $step == "set-unban-user"){
	if(file_exists("data/$text.json")) {
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
$user1 = json_decode(file_get_contents("data/$text.json"),true);
$user1["step"] = "none";
$outjson1 = json_encode($user1,true);
file_put_contents("data/$text.json",$outjson1);
SendMessage($chat_id, "User successfully Unblocked ✅","MarkDown",null,null);
SendMessage($text, "You are successfully Unblocked ✅","MarkDown",null,null);
}else{
SendMessage($text, "User not found❌","MarkDown",null,null);
}}
elseif($chat_id == $admin and $text == "Block ❌"){
$user["step"] = "set-ban-user";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "send user id",null,null, $backP);
}
elseif($chat_id == $admin and $text != "Back to panel" and $step == "set-ban-user"){
	if(file_exists("data/$text.json")) {
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
$user1 = json_decode(file_get_contents("data/$text.json"),true);
$user1["step"] = "ban";
$outjson1 = json_encode($user1,true);
file_put_contents("data/$text.json",$outjson1);
SendMessage($chat_id, "[User successfully blocked ⛔️!](tg://user?id=$text)","MarkDown",null,null);
SendMessage($text, "You were blocked by admin ⛔️","MarkDown",null,null);
}else{
SendMessage($text, "User not found❌","MarkDown",null,null);
}
}
elseif($chat_id == $admin and $text == "PM all 📩"){
$user["step"] = "send2all";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "send message for send to all","MarkDown",null,$backP);
}
elseif($chat_id == $admin && $step == "send2all" && $text != "Back to panel"){
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
    $all_member = fopen( "data/members.txt", 'r');
	while( !feof( $all_member)) {
 	$user = fgets( $all_member);
  SendMessage($user, "$text","MarkDown",null,null);
}
SendMessage($admin, "sent to all","MarkDown",null,null);
}
elseif($chat_id == $admin and $text == "For all 📩"){
	$user["step"] = "f2all";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($admin, "forward message for send to all","MarkDown",null,$backP);
}
elseif($text != "Back to panel" && $from_id == $admin && $step == "f2all"){
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
    $all_member = fopen( "data/members.txt", 'r');
		while( !feof( $all_member)) {
 			$user = fgets( $all_member);
		   bot('ForwardMessage',[
	'chat_id'=>$user,
	'from_chat_id'=>$admin,
	'message_id'=>$message_id
	]);
		}
SendMessage($admin, "forwarded to all","MarkDown",null,null);
}
	 elseif($chat_id == $admin and $text == "Bot on ✅"){
		  $settings["mode"] = "on";
$outjson = json_encode($settings,true);
file_put_contents("data/settings.json",$outjson);
 SendMessage($admin, "✅ Bot is on","MarkDown",null,null);
	 }
	 elseif($chat_id == $admin and $text == "Bot off ❌"){
		  $settings["mode"] = "off";
$outjson = json_encode($settings,true);
file_put_contents("data/settings.json",$outjson);
	 SendMessage($admin, "✅ Bot is off","MarkDown",null,null);
	 }
elseif(strpos($text,'rep-') !== false) {
$idm = str_replace('rep-','',$text);
$usere = json_decode(file_get_contents("data/$from_id.json"),true);
$usere["step"] = "mestoka2";
$outjson = json_encode($usere,true);
file_put_contents("data/$from_id.json",$outjson);
file_put_contents("data/id.txt",$idm);
SendMessage($admin, "Send message to $idm \n for cancel : /cancel
");
}
if($step == "mestoka2" and $chat_id == $admin and $text != "/cancel"){	
$id_K = file_get_contents("data/id.txt");
$user["step"] = "none";
$outjson = json_encode($user,true);
file_put_contents("data/$from_id.json",$outjson);
SendMessage($id_K, "$text",null,null,null);
SendMessage($admin, "Sent ✅",null,null,null);
@unlink(data/id.txt);
}
 	   if(strpos($text,'ban-') !== false ){
	   $idm = str_replace('ban-','',$text);
	   $usere = json_decode(file_get_contents("data/$idm.json"),true);
$stepee = $usere["step"];
$usere["step"] = "ban";
$outjson = json_encode($usere,true);
file_put_contents("data/$idm.json",$outjson);
SendMessage($chatid, "[User successfully blocked ⛔️!](tg://user?id=$idm)","MarkDown",null,null);
 SendMessage($idm, "You were blocked by admin ⛔️",null,null,null);
	   }
   }
	 unlink('error_log');
	 ?>
