<?php
include('connection.php');
 // Telegram bot token
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);

$chatID = $update['message']['from']['id'];
$text = $update['message']['text'];
$firstName = $update['message']['from']['first_name'];

$result = mysqli_query($conn, "SELECT * FROM telegram_bot WHERE tb_chat_id='$chatID'");
$row = mysqli_fetch_assoc($result);

$action = $row['tb_action'];

if($action == "cancelSession"){
	$reply = "Your new reply message added! Thank you";
	$keyboard = '{"keyboard":
		[[{"text":"Add question"}],[{"text":"Exit"}]],
		"one_time_keyboard":true,"resize_keyboard":true}';
	
	if($text == "NULL"){$text = NULL;}
	mysqli_query($conn, "UPDATE telegram_reply SET tr_next='$text' WHERE tr_session='$chatID'");
	
	mysqli_query($conn, "UPDATE telegram_reply SET tr_session=NULL WHERE tr_session='$chatID'");
	
	mysqli_query($conn,"DELETE FROM telegram_bot WHERE tb_chat_id='$chatID'");
	
	sendMessage($chatID,$reply,$keyboard);
	
	exit();
}

if($action == "addNext"){
	$reply = "What's continue reply ID?";
	$keyboard = '{"keyboard":
		[[{"text":"NULL"}]],
		"remove_keyboard":true,"resize_keyboard":true}';
	
	if($text == "NULL"){$text = NULL;}
	mysqli_query($conn, "UPDATE telegram_reply SET tr_keyboard='$text' WHERE tr_session='$chatID'");
	
	$action = "cancelSession";
	mysqli_query($conn, "UPDATE telegram_bot SET tb_action='$action' WHERE tb_chat_id='$chatID'");
	
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = $action;
	sendMessage($chatID,$reply,$keyboard);
	
	exit();
}

if($action == "addKeyboard"){
	$reply = "Your keyboard markup?";
	$keyboard = '{"keyboard":
		[
			[{"text":"1x1"},{"text":"2x1"},{"text":"3x1"},{"text":"4x1"},{"text":"5x1"}],
			[{"text":"Kb"}]
			],
		"one_time_keyboard":false,"resize_keyboard":true}';
	
	mysqli_query($conn, "UPDATE telegram_reply SET tr_reply='$text' WHERE tr_session='$chatID'");
	
	$action = "addNext";
	mysqli_query($conn, "UPDATE telegram_bot SET tb_action='$action' WHERE tb_chat_id='$chatID'");
	
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = $action;
	sendMessage($chatID,$reply,$keyboard);
	
	exit();
}

if($action == "addQuestion"){
	$reply = "What's response message?";
	$keyboard = '{"keyboard":
		[],
		"remove_keyboard":true}';
	
	mysqli_query($conn, "INSERT INTO telegram_reply (tr_question,tr_session) VALUES ('$text','$chatID')");
	
	$action = "addKeyboard";
	mysqli_query($conn, "UPDATE telegram_bot SET tb_action='$action' WHERE tb_chat_id='$chatID'");
	
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "chat id : ".$chatID;
	sendMessage($chatID,$reply,$keyboard);
	$reply = "action : ".$action;
	sendMessage($chatID,$reply,$keyboard);
	
	exit();
}

if($action == "editKeyboard"){
	include('editkeyboard.php'); exit();
}

if($text == "Kb"){
	$reply = "Your keyboard look like";
	$keyboard = '{"keyboard":
		[
			[{"text":"1x1"},{"text":"2x1"},{"text":"3x1"},{"text":"4x1"},{"text":"5x1"}],
			[{"text":"Kb"}]
		],
		"one_time_keyboard":false,"resize_keyboard":true}';
	
	$action = "editKeyboard";
	mysqli_query($conn, "INSERT INTO telegram_bot (tb_chat_id,tb_action) VALUES ('$chatID','$action')");
	sendMessage($chatID,$reply,$keyboard);
	
	mysqli_query($conn,"INSERT INTO telegram_session (ts_note,ts_session,ts_chat_id) VALUES ('$grid',1,'$chatID')");
	
	exit();
}

if($text == "Add question"){
	$reply = "Ok. What's the question?";
	$keyboard = '{"keyboard":
		[],
		"remove_keyboard":true}';
	
	$action = "addQuestion";
	mysqli_query($conn, "INSERT INTO telegram_bot (tb_chat_id,tb_action) VALUES ('$chatID','$action')");
	
	sendMessage($chatID,$reply,$keyboard);
	
}else{
	$result = mysqli_query($conn, "SELECT * FROM telegram_reply WHERE tr_question='$text'");
	$numRows = mysqli_num_rows($result);
	
	if($numRows > 0){
		while($row = mysqli_fetch_assoc($result)){
			$reply = $row['tr_reply'];
			$keyboard = $row['tr_keyboard'];
		}
		
	}else{
		$reply = "Sorry I dont understand your question. Please try again :(";
		$keyboard = NULL;
	}
		
	sendMessage($chatID,$reply,$keyboard);
	
	exit();
}



function sendMessage($chat_id,$reply,$keyboard){
	//$bot_token = "887175490:AAE2MdfsVxde8PmkY4KROJrT_1PxPyBxl2w"; // @pasarjuadahbot
	$bot_token = "1206454586:AAEseAvFOAd0Xs3WobuUb2AVyRT2NXr7L44"; // @dzvhomestay_bot
	$url = "https://api.telegram.org/bot$bot_token/sendMessage";
	
	$postfields = array(
	'chat_id' => "$chat_id",
	'text' => "$reply",
	'reply_markup' => $keyboard
	);
	
	print_r($postfields);
	if (!$curld = curl_init()) {
	exit;
	}

	curl_setopt($curld, CURLOPT_POST, true);
	curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
	curl_setopt($curld, CURLOPT_URL,$url);
	curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);

	$output = curl_exec($curld);

	curl_close ($curld);
}

?>