Cara Membuat Pesan API Bot Telegram Bagian 2
Sebelumnya kita sudah konfigurasi tentang cara booting di aplikasi telegram . Kalau yang belum bisa , harap baca bagian pertama agar bisa tau cara nya gimana . Cara Membuat Konfigurasi Pesan API Bot Telegram , nah sekarang gimana cara codingnya.* function sendMessage untuk mengirim data pesan sajaclass DataBase{function sendMessage($chatID, $messaggio, $token) {echo "sending message to " . $chatID . "\n";$url = "https://api.telegram.org/bot" . $token .
"/sendMessage?chat_id=" . $chatID;$url = $url . "&text=" . urlencode($messaggio);$ch = curl_init();$optArray = array(CURLOPT_URL => $url,CURLOPT_RETURNTRANSFER => true);curl_setopt_array($ch, $optArray);$result = curl_exec($ch);curl_close($ch);return $result;}function sendPhoto($chatID, $photo, $token){echo "sending photo to " . $chatID . "\n";$bot_url = "https://api.telegram.org/bot" . $token . "/";$url = $bot_url . "sendPhoto?chat_id=" . $chatID ;$post_fields = array('chat_id' => $chatID,'photo' => new CURLFile(realpath("".$photo."")));$ch = curl_init();curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);$output = curl_exec($ch);curl_close($ch);return $output;}
}
* function sendPhoto untuk mengirim data image atau gambar
3. Kemudian buat form sederhana, index.php
4. Buat proses.php<form action="proses.php" method="POST">
<input type="text" name="pesan">
<input type="file" name="file">
<button type="submit" name="send">Kirim</button>
</form>
<?phpinclude "connection.php";if (isset($_POST['send'])){$pesan=$_POST['pesan'];if (!empty($_FILES["file"]["tmp_name"])):$gambar = basename($_FILES['file']['name']);$chatID = "toked chatID pastekan disini";$messaggio = "PESANAN DARI KAMIKajian: ".$pesan.";/*--------------------------------Isi TOKEN dibawah ini:--------------------------------*/$token = "token dari telegram paste disini";$koneksi->sendMessage($chatID, $messaggio, $token);$koneksi->sendPhoto($chatID, $gambar, $token);header('location:index.php');else:echo "tidak terkirim";endif;
Kurang lebih seperti itu semoga bermanfaat terima kasih :)