<?php
	ob_start();
	define('token','267127840:AAEFoTBG52p-WDgHmua2fW4S6N_3E9LJB7k');
	$json = file_get_contents('php://input');
	$telegram = urldecode ($json);
	$results = json_decode($telegram);
	//======================================================
	include ('jdf.php');
	date_default_timezone_set("Asia/Tehran");
	$jadate = jdate("Y/m/d",time());
	$jatime = jdate("H:i:s",time());

        include ("dalers.php");
	include ("functions.php");
    	include ('kament.php');
    	//include ("mashin.php");
    	
	//------------------------- آغاز پنل مدیریت ---------------------------
	if (in_array($chat_id,$admin2) or in_array($chat_id2,$admin2)){

        include ('Karbar_Admin.php');
        include ('Karbari.php');
        include ('Panel_Admin.php');
        include ('Panel_Add_Admin.php');   
		//------------------------------------------------------------------------
        include ('panel_film2.php');        
        include ('panel_image.php');		
        include ('panel_gif.php');		
        include ('panel_file.php');		
        include ('panel_voice.php');		
        include ('panel_amar.php');		
        include ('panel_forward_all.php');		
        include ('panel_pm_all.php');		
        include ('panel_set_channel.php');		
        include ('panel_vip.php');		
        include ('panel_backup.php');		
		//----------- پایان بخش پنل مدیریت - و آغاز بخش کاربری -------------

	}else{
		//mkdir ("data/users/$from_id");
		if (strpos($textmessage,"/start")!== false){
			$textmessage = str_replace ("/start ","",$textmessage);
			$invitedd = file_get_contents ("data/users/$textmessage/invited.txt");
			$added = file_get_contents ("data/users/$textmessage/added.txt");
			$addeds = explode (",",$added);
			if (!in_array($from_id,$addeds)){
				$member = file_get_contents ("data/users/$textmessage/added.txt");
				$nmember = $member.$from_id.",";
				file_put_contents("data/users/$textmessage/added.txt",$nmember);
				file_put_contents ("data/users/$textmessage/invited.txt",$invitedd+1);
				sendMessage ($textmessage,"یک نفر با لینک شما وارد ربات شد.");
			}
		}
		
		//++++++++++++
		$member = file_get_contents ('data/admin/members.txt');
		$members = explode (",",$member);
		if (!in_array($from_id,$members)){
			$member = file_get_contents ('data/admin/members.txt');
			$nmember = $member.$from_id.",";
			file_put_contents('data/admin/members.txt',$nmember);
		}
		$channel = file_get_contents ("data/admin/channel.txt");
		$fch = 'https://api.telegram.org/bot'.token.'/getChatMember?chat_id=@'.$channel.'&user_id='.$from_id;
		$t = json_decode(file_get_contents($fch));
		$tch = $t->result->status;
		$fch2 = 'https://api.telegram.org/bot'.token.'/getChatMember?chat_id=@'.$channel.'&user_id='.$from_id2;
		$t2 = json_decode(file_get_contents($fch2));
		$tch2 = $t2->result->status;
		
		if ($data == "testjoin"){
			if ($tch2 != "creator" and $tch2 != "administrator" and $tch2 != "member"){
				Saeed ('answerCallbackQuery',[
					'callback_query_id'=>$callback_query_id,
					'text'=>"شما عضو کانال اسپانسر  @$channel نشده اید.",
					'show_alert'=>true,
				]);
			}else{
				//mkdir ("data/users/$from_id");
				sendAction ($from_id2,'typing');
				Saeed ('sendMessage',[
					'chat_id'=>$from_id2,
					'parse_mode'=>"Markdown",
					'text'=>"به ربات [$myfname](tg://user?id=$myid) خوش آمدید.",
					'reply_markup'=>json_encode([
						'resize_keyboard'=>true,
						'one_time_keyboard'=>true,
						'keyboard'=>[
							[['text'=>'فیلم🎥'],['text'=>'عکس 🌇'],['text'=>'گیف 🔫']],
							[['text'=>'😎 VIP 😎']],
						]
					])
				]);
			}
		}
		//++++++++++++
		
		//**********
		if ($tch != "creator" and $tch != "administrator" and $tch != "member"){
			sendAction ($from_id,'typing');
			Saeed ('sendMessage',[
				'chat_id'=>$from_id,
				'text'=>"سلام خدمت شما کاربر گرامی $first_name
				اول در کانال اسپانسر ما عضو شوید سپس بر روی دکمه عضو شدم کلیک کنید😄🌹
				ایدی کانال:
				@$channel
				@$channel
				لینک کانال:
				$channellink
				عضو شوید تا بتوانید از ربات استفاده کنید.
				⚠️ بعد از عضو شدن بزن رو دکمه عضو شدم",
				'disable_web_page_preview'=>true,
				'reply_markup'=>json_encode([
					'inline_keyboard'=>[
						[['text'=>'عضو شدن در کانال 📢','url'=>$channellink]],
						[['text'=>'عضو شدم ✅','callback_data'=>"testjoin"]]
					]
				])
			]);
		}else{
	    	if ($textmessage == "/start" or $textmessage == "برگشت 🔙" or $textmessage == "بازگشت 🔙"){
        		sendAction ($from_id,'typing');
        		Saeed ('sendMessage',[
        			'chat_id'=>$from_id,
        			'parse_mode'=>"Markdown",
        			'text'=>"سلام به ربات [$myfname](tg://user?id=$myid) خوش آمدید.",
        			'reply_markup'=>json_encode([
        				'resize_keyboard'=>true,
        				'one_time_keyboard'=>false,
        				'keyboard'=>[
        					[['text'=>'فیلم🎥'],['text'=>'عکس 🌇'],['text'=>'گیف 🔫']],
        					[['text'=>'😎 VIP 😎'],['text'=>'پیشنهادات']],
        				]
        			])
        		]);
        	}
	    	//=================== ارتباط با فایل فرعی =================
            include ('Karbari.php');
		}
		//**********
	}


file_put_contents ('e.txt',$json);
Delfile (error_log);
?>
