PHP Lesson: Telegram Bot


Menghantar mesej Telegram melalui php memerlukan Telegram Bot Token dan Telegram Chat ID. Cara mendaftarkan Telegram Bot boleh rujuk di ESP32 Telegram Bot. Contoh code php bagi menghantar mesej Telegram seperti di bawah.

<html>
<head>
<title>Send Telegram</title>
</head>
<body>   
<center>
   <form action="" method="post">
   Bot Token: <input type="password" name="apiToken" value="Your BotToken Here" required>
   <br>
   Chat ID: <input type="password" name="id" value="Your ChatID Here" required>
   <br>
   Message: <input type="text" name="message" required>
   <br>
   <input type="submit" name="submit">
   </form>  

   <?php
   extract($_POST);
   if(isset($_POST['submit']))
   {
         $time_start = microtime(true);
         $data = [
            'chat_id' => $id,
            'text' => $message
         ];
         $response = file_get_contents("https://api.telegram.org/bot".$apiToken."/sendMessage?".http_build_query($data));
         $time_end = microtime(true);
         $duration = $time_end - $time_start;
         echo "Executed in ".$duration."s";
      }    
   ?>
</center>
</body>
</html>

Berikut adalah video demo menghantar mesej Telegram.


Leave a Reply

Your email address will not be published. Required fields are marked *