<?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'];

if($text == "/start"){
	$reply = "Hi, ".$firstName;
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Saya Faiz, saya adalah BOT. Saya akan cuba membantu anda.";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Apa khabar?";$keyboard = array(
		"keyboard" => 
			array(
				array(
					array("text" => "Khabar baik, alhamdulillah"),
					array("text" => "Tidak sihat :(")
				)
			), "one_time_keyboard" => true, "resize_keyboard" => true // Can be FALSE (vertical resize)
		);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "Khabar baik, alhamdulillah"){
	$reply = "Alhamdulillah!";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Apa boleh saya bantu?";$keyboard = array(
		"keyboard" => 
			array(
				array(
					array("text" => "Nak tanya pasal booking saya"))
			), "one_time_keyboard" => true, "resize_keyboard" => true // Can be FALSE (vertical resize)
		);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "Tidak sihat :("){
	$reply = "Oh tidak! Semoga anda sembuh cepat.";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Apa boleh saya bantu?";$keyboard = array(
		"keyboard" => 
			array(
				array(
					array("text" => "Nak tanya pasal booking saya")),
				array(
					array("text" => "Tak ada apa-apa. Tanya je."))
			), "one_time_keyboard" => true, "resize_keyboard" => true // Can be FALSE (vertical resize)
		);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "Tak ada apa-apa. Tanya je."){
	$reply = "Oh baiklah";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Jika ada apa2 boleh bgtau ya";$keyboard = array(
		"keyboard" => 
			array(
				array(
					array("text" => "Baiklah, terima kasih")),
				array(
					array("text" => "Nak tanya pasal booking saya"))
			), "one_time_keyboard" => true, "resize_keyboard" => true // Can be FALSE (vertical resize)
		);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "Baiklah, terima kasih"){
	$reply = "Sama-sama :)";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "Nak tanya pasal booking saya"){
	$reply = "Baiklah";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Boleh saya tahu platform mana anda buat booking?";$keyboard = array(
		"keyboard" => 
			array(
				array(
					array("text" => "Airbnb"),
					array("text" => "Booking.com"),
					array("text" => "Agoda")),
				array(
					array("text" => "Saya belum buat booking lagi"))
			), "one_time_keyboard" => true, "resize_keyboard" => true // Can be FALSE (vertical resize)
		);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "Airbnb" | $text == "Agoda" | $text == "Booking.com"){
	$reply = "ok, terima kasih";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
	
	$reply = "Boleh beri saya booking no?";
	$keyboard = array("keyboard" => array(), "remove_keyboard" => true);
	sendMessage($chatID,$reply,$keyboard);
}

if($text == "/to"){
	$reply = "Hi";

	$keyboard = array(
		"keyboard" => 
			array(
				array(
					array("text" => "/button"),
					array("text" => "/button"),
					array("text" => "contact", "request_contact" => true),
					array("text" => "location", "request_location" => true),
					array("text" => "free", "request_location" => true)
				)
			), "one_time_keyboard" => true, "resize_keyboard" => true // Can be FALSE (vertical resize)
		);
	
	sendMessage($chatID,$reply,$keyboard);
}

include('housekeeper.php');

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' => json_encode($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);
}

?>