Skip to content

Instantly share code, notes, and snippets.

@banghasan
Last active March 20, 2023 00:30
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save banghasan/0a3711290e6b6d621d7550e5653a4c01 to your computer and use it in GitHub Desktop.
Save banghasan/0a3711290e6b6d621d7550e5653a4c01 to your computer and use it in GitHub Desktop.
Test Send Message Pakai PHP - Curl
<?php
/*
Simple File untuk Ngetes Send Pesan ke Bot
Memiliki banyak kegunaan dan tujuan
misalnya ngetes pesan dengan format tertentu, line break, char khusus, dll.
bisa dipergunakan juga untuk test hosting, cronjob, dan segala test lainnya.
Jika menggunakan mode GET :
- Line break (ENTER) = %0A
- Space = %20
Atau rawurlencode($string);
Contoh dibawah ini menggunakan mode POST. Baris baru cukup dengan \n.
* -----------------------
* Grup @botphp
* Jika ada pertanyaan jangan via PM
* langsung di grup saja.
* ----------------------
*/
$TOKEN = "TOKENBOT"; // ganti token ini dengan token bot mu
$chatid = "213567634"; // ini id saya di telegram @hasanudinhs silakan diganti dan disesuaikan
$pesan = "Helo mas bambang.. \n\neh salah orang, ya maap!";
// ----------- code -------------
$method = "sendMessage";
$url = "https://api.telegram.org/bot" . $TOKEN . "/". $method;
$post = [
'chat_id' => $chatid,
// 'parse_mode' => 'HTML', // aktifkan ini jika ingin menggunakan format type HTML, bisa juga diganti menjadi Markdown
'text' => $pesan
];
$header = [
"X-Requested-With: XMLHttpRequest",
"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36"
];
// hapus 1 baris ini:
die('Hapus baris ini sebelum bisa berjalan, terimakasih.');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_REFERER, $refer);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$datas = curl_exec($ch);
$error = curl_error($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$debug['text'] = $pesan;
$debug['code'] = $status;
$debug['status'] = $error;
$debug['respon'] = json_decode($datas, true);
print_r($debug);
/*
* by @hasanudinhs
* Telegram @botphp
* Last update: 27 Sept 2017 22:53
*/
?>
@andini2460
Copy link

array (
'update_id' => 12345678,
'message' => array (
'message_id' => 246,
'from' => array (
'id' => 987654321,
'first_name' => 'andini',
'username' => 'andini',
),
'chat' => array (
'id' => 987654321,
'first_name' => 'andini',
'username' => 'andini',
'type' => 'private',
),
'date' => 123456789,
'text' => 'pesan biasa di private',
),
)

@macoding777
Copy link

Terima kasih 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment