<?php
$tiga=$this->db->query("SELECT * FROM web_bot WHERE id='1'");
foreach ($tiga->result() as $rt) {
$token=$rt->token;
$userbot=$rt->username;
}
require_once __DIR__.'/src/PHPTelebot.php';
$bot = new PHPTelebot($token, $userbot);

// Simple answer
$bot->cmd('*', 'Hi, human! I am a bot.');

// Simple echo command
$bot->cmd('/echo|/say', function ($text) {
    if ($text == '') {
        $text = 'Command usage: /echo [text] or /say [text]';
    }

    return Bot::sendMessage($text);
});

// Simple whoami command
$bot->cmd('/siapa', function () {
    // Get message properties
    $message = Bot::message();
    $name = $message['from']['first_name'];
    $userId = $message['from']['id'];
    $text = 'You are <b>'.$name.'</b> and your ID is <code>'.$userId.'</code>';
    $options = [
        'parse_mode' => 'html',
        'reply' => true,
    ];

    return Bot::sendMessage($text, $options);
});
$bot->cmd('/help|/tolong|!Help', function () {
    // Get message properties
    $message = Bot::message();
    $name = $message['from']['first_name'];
    $userId = $message['from']['id'];
	Bot::sendChatAction('typing');
		 $text .= "/help - Menu Bot\n";
		 $text .= "/absen[spasi]kodeabsen - melihat absen datang\n";
         $text .= "/pulang[spasi]kodeabsen - melihat absen pulang\n";
         $text .= "/ijin[spasi]kodeabsen - melihat ijin\n";
         $text .= "/rekap[spasi]kodeabsen - melihat rekap absen\n";
    $options = [
        'parse_mode' => 'html',
        'reply' => true,
    ];

    return Bot::sendMessage($text, $options);
});
// slice text by space
$bot->cmd('/absen', function ($one, $two, $three) {
	$hariini=date('Y-m-d');
	$cek1=$this->db->query("select * from web_absen where nip='".$one."' AND status='1' AND tgl ='".$hariini."' LIMIT 1");
	$hasilcek1=$cek1->num_rows();
	if ($hasilcek1<>0){
		foreach ($cek1->result() as $rscek1) {
			$jam=$rscek1->jam;
			$ket=$rscek1->ket;
			$cek2=$this->db->query("select * from web_siswa where absen='".$one."'");
			foreach ($cek2->result() as $rscek2) {
				$namanya=$rscek2->NM_PEGAWAI;
			}
			$hasil =$namanya." Telah Absen pada tanggal : ".$hariini." Pukul ".$jam." Ket: ".$ket."";
		}
	} else {
		$hasil = "Belum melakukan absen pada hari ini ".$hariini;
	}
    $text = $hasil;
    return Bot::sendMessage($text);
});
$bot->cmd('/pulang', function ($one, $two, $three) {
	$hariini=date('Y-m-d');
	$cek1=$this->db->query("select * from web_absen where nip='".$one."' AND status='2' AND tgl ='".$hariini."' LIMIT 1");
	$hasilcek1=$cek1->num_rows();
	if ($hasilcek1<>0){
		foreach ($cek1->result() as $rscek1) {
			$jam=$rscek1->jam;
			$ket=$rscek1->ket;
			$cek2=$this->db->query("select * from web_siswa where absen='".$one."'");
			foreach ($cek2->result() as $rscek2) {
				$namanya=$rscek2->NM_PEGAWAI;
			}
			$hasil =$namanya." Telah Pulang pada tanggal : ".$hariini." Pukul ".$jam." Ket: ".$ket."";
		}
	} else {
		$hasil = "Belum melakukan absen pulang pada hari ini ".$hariini;
	}
    $text = $hasil;
    return Bot::sendMessage($text);
});
$bot->cmd('/ijin', function ($one, $two, $three) {
	$hariini=date('Y-m-d');
	$cek1=$this->db->query("select * from web_ijin where absen='".$one."' AND status >= '3' AND tgl ='".$hariini."' LIMIT 1");
	$hasilcek1=$cek1->num_rows();
	if ($hasilcek1<>0){
		foreach ($cek1->result() as $rscek1) {
			$jam=$rscek1->jam;
			$ket=$rscek1->ket;
			$cek2=$this->db->query("select * from web_siswa where absen='".$one."'");
			foreach ($cek2->result() as $rscek2) {
				$namanya=$rscek2->NM_PEGAWAI;
			}
			$hasil =$namanya." Melakukan pada tanggal : ".$hariini." Pukul ".$jam." Ket: ".$ket."";
		}
	} else {
		$hasil = "Tidak ijin pada hari ini ".$hariini;
	}
    $text = $hasil;
    return Bot::sendMessage($text);
});


$bot->run();
