Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $TOKEN = "token";
- $usernamebot= "@namabot";
- $debug = false;
- function request_url($method)
- {
- global $TOKEN;
- return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
- }
- function send_reply($chatid, $msgid, $text)
- {
- global $debug;
- $data = array(
- 'chat_id' => $chatid,
- 'text' => $text,
- 'parse_mode' => 'html',
- 'disable_web_page_preview' => true,
- );
- // use key 'http' even if you send the request to https://...
- $options = array(
- 'http' => array(
- 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
- 'method' => 'POST',
- 'content' => http_build_query($data),
- ),
- );
- $context = stream_context_create($options);
- $result = file_get_contents(request_url('sendMessage'), false, $context);
- if ($debug)
- print_r($result);
- }
- function send_group($chatid, $msgid2, $text)
- {
- global $debug;
- $data2 = array(
- 'chat_id' => $chatid,
- 'text' => $text,
- 'reply_to_message_id' => $msgid2, // <---- biar ada reply nya balasannya, opsional, bisa dihapus baris ini
- 'parse_mode' => 'html',
- );
- // use key 'http' even if you send the request to https://...
- $options2 = array(
- 'http' => array(
- 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
- 'method' => 'POST',
- 'content' => http_build_query($data2),
- ),
- );
- $context2 = stream_context_create($options2);
- $result2 = file_get_contents(request_url('sendMessage'), false, $context2);
- if ($debug)
- print_r($result2);
- }
- function send_group2($chatid, $msgid3, $text)
- {
- global $debug;
- $data3 = array(
- 'chat_id' => $chatid,
- 'text' => $text,
- 'parse_mode' => 'html',
- );
- // use key 'http' even if you send the request to https://...
- $options3 = array(
- 'http' => array(
- 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
- 'method' => 'POST',
- 'content' => http_build_query($data3),
- ),
- );
- $context3 = stream_context_create($options3);
- $result3 = file_get_contents(request_url('sendMessage'), false, $context3);
- if ($debug)
- print_r($result3);
- }
- // fungsi mengolahan pesan, menyiapkan pesan untuk dikirimkan
- function create_response($text, $message)
- {
- global $usernamebot;
- // inisiasi variable hasil yang mana merupakan hasil olahan pesan
- $hasil = '';
- $fromid = $message["from"]["id"]; // variable penampung id user
- $chatid = $message["chat"]["id"]; // variable penampung id chat
- $pesanid= $message['message_id']; // variable penampung id message
- // variable penampung username nya user
- isset($message["from"]["username"])
- ? $chatuser = $message["from"]["username"]
- : $chatuser = '';
- // variable penampung nama user
- isset($message["from"]["last_name"])
- ? $namakedua = $message["from"]["last_name"]
- : $namakedua = '';
- $namauser = $message["from"]["first_name"]. ' ' .$namakedua;
- //Disini di koreksi karena new chat member tidak menampung teks dibuat kondisi
- if(isset($message['new_chat_member'])){
- $hasil = $text;
- return $hasil; //Hasil langsung direturn supaya kode dibawah tidak dieksekusi
- }
- if(isset($message['promotion'])){
- $hasil = $text;
- return $hasil; //Hasil langsung direturn supaya kode dibawah tidak dieksekusi
- }
- // ini saya pergunakan untuk menghapus kelebihan pesan spasi yang dikirim ke bot.
- $textur = preg_replace('/\s\s+/', ' ', $text);
- // memecah pesan dalam 2 blok array, kita ambil yang array pertama saja
- $command = explode(' ',$textur,2); //
- // identifikasi perintah (yakni kata pertama, atau array pertamanya)
- switch ($command[0]) {
- // pesan selamat datang
- case '/start':
- $hasil = "<b>$namauser</b> \n";
- $hasil .= "Selamat datang di $usernamebot \n\n";
- $hasil .= "/start - Mulai bot \n";
- $hasil .= "/id - Untuk cek nama, username dan id Anda \n";
- $hasil .= "/timeworld - Cek waktu seluruh dunia, lihat disini format timezone nya <a href='https://www.w3schools.com/php/php_ref_timezones.asp'>Timezone list</a>. Penulisan (/timeworld Asia/Jakarta) \n";
- break;
- // jika ada pesan /id, bot akan membalas dengan menyebutkan idnya user
- case '/id':
- $hasil = "Name: <b>$namauser</b> \n";
- $hasil .= "Username: <b>@$chatuser</b> \n";
- $hasil .= "My ID: <b>$fromid</b>";
- break;
- case '/timeworld'.$input2;
- $input = "$text";
- $input2 = substr($input, 10);
- if ($input > "/timeworld $input2") {
- $regex = preg_match('/\/timeworld (.*)/', $input, $hasil);
- date_default_timezone_set($hasil[1]);
- $hasil = "Cek waktu seluruh dunia, lihat disini format timezone nya <a href='https://www.w3schools.com/php/php_ref_timezones.asp'>Timezone list</a>. Penulisan (/timeworld Asia/Jakarta) \n\n";
- $hasil .= "Tanggal: ";
- $hasil .= "<b>";
- $hasil .= date('d/m/Y', time());
- $hasil .= "</b>";
- $hasil .= "\n";
- $hasil .= "Waktu: ";
- $hasil .= "<b>";
- $hasil .= date('H:i:s', time());
- $hasil .= "</b>";
- $hasil .= "\n";
- $hasil .= "Zona: ";
- $hasil .= "<b>";
- $hasil .= date_default_timezone_get();
- $hasil .= "</b>";
- } else if ($input == "/timeworld") {
- $hasil = "Penulisan salah atau kurang lengkap, lihat disini format timezone nya <a href='https://www.w3schools.com/php/php_ref_timezones.asp'>Timezone list</a>. Penulisan (/timeworld Asia/Jakarta)";
- }
- break;
- }
- return $hasil;
- }
- function process_message($message)
- {
- $trustedid = "1100853905";
- $trustedgroup = "-1001345383575";
- $updateid = $message["update_id"];
- $message_data = $message["message"];
- $trusted = $message["message"]["chat"]["id"];
- $trustgroup = $message["message"]["chat"]["id"];
- $jenis = $message["chat"]["type"];
- if (isset($message_data["text"])) {
- /*
- if($trusted == $trustedid){
- */
- $chatid = $message_data["chat"]["id"];
- $message_id = $message_data["message_id"];
- $text = $message_data["text"];
- /*
- }
- */
- $response = create_response($text, $message_data);
- if (!empty($response))
- send_reply($chatid, $message_id, $response);
- }
- //welcome group
- if (isset($message_data["new_chat_member"])) {
- if($trustgroup == $trustedgroup){
- $chatid = $message_data["chat"]["id"];
- $message_id = $message_data["message_id"];
- $firstName = $message_data["new_chat_member"]["first_name"];
- $title = $message_data["chat"]["title"];
- $new_chat_member = "Selamat datang <b>$firstName</b> di $title \n\n ";
- $new_chat_member .= date("d/m/Y, H:i:s");
- }
- $response = create_response($new_chat_member, $message_data);
- if (!empty($response))
- send_group($chatid, $message_id, $response);
- }
- //sendMessage group
- if (isset($message_data["promotion"])) {
- if($trustgroup == $trustedgroup){
- $chatid = $message_data["chat"]["id"];
- $message_id = $message_data["message_id"];
- $promotion = "hallo";
- }
- $response = create_response($promotion, $message_data);
- if (!empty($response))
- send_group2($chatid, $message_id, $response);
- }
- return $updateid;
- }
- $entityBody = file_get_contents('php://input');
- $message = json_decode($entityBody, true);
- process_message($message);
- ?>
Add Comment
Please, Sign In to add comment