Very Basic PHP Telegram Bot w/Webhooks

2 min read

Note:

  • This uses PHP and is hosted on the web
  • You need to be able to save the script to a secure https URL ie you need a valid SSL certificate.
  • You need an authorization token. Follow the instructions in Step 1 here.
  • You will also need your botname @yourbotname

Overview:

  1. We are creating a script which will speak with Telegram by receiving JSON post variables and sending GET variables (ie in the URL).
  2. You will tell Telegram where to find this script is IE which URL to send new message info to (“Set Webhooks)
  3. You will test the script by messaging your new bot.

 

Step 1a: The Script

Replace YOURBOT:TOKEN with your token. The token looks something like
164354723:AAEjT6-IyNoXjt7miD0dwa-P5VmDTtHQC8

<?php 
define('BOT_TOKEN', 'YOURBOT:TOKEN');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
	
// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
		
// compose reply
$reply =  sendMessage();
		
// send reply
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);

function sendMessage(){
$message = "I am a baby bot.";
return $message;
}

Step 1b: Upload to your server

It is advisable that choose a “secret” URL for example include the token in the URL:

https://my.webhost.com/164354723:AAEjT6-IyNoXjt7miD0dwa-P5VmDTtHQC8/telbot.php;

IMPORTANT: Your url MUST start with HTTPS.
IE You need a valid SSL certificate for webhooks to work.

 

Step 2: Tell Telegram about your bot

(1) Using a text editor, write out the following URL with your own info:

https://api.telegram.org/bot[myauthorization-token]/setwebhook?url=[myboturl]

For example using the variables above, this would look like

https://api.telegram.org/bot164354723:AAEjT6-IyNoXjt7miD0dwa-P5VmDTtHQC8/setwebhook?url=https://my.webhost.com/164354723:AAEjT6-IyNoXjt7miD0dwa-P5VmDTtHQC8/telbot.php;

(2) Copy the link into your browser and hit return
You should get a confirmation message that the webhook is recognised.

 

Step 3: Go test your bot

Through the telegram app, send @yourbotname a new message.

 

THAT’S IT

 

Troubleshooting

If you need to diagnose problems with reading the input or want to see what info Telegram is sending you:

(1) add this function to your code,
(2) send your bot a test message and
(3) read the results in the log.txt file.

 

        checkJSON($chatID,$update);

	function checkJSON($chatID,$update){
	
		$myFile = "log.txt";
		$updateArray = print_r($update,TRUE);
		$fh = fopen($myFile, 'a') or die("can't open file");
		fwrite($fh, $chatID ."\n\n");
		fwrite($fh, $updateArray."\n\n");
		fclose($fh);
	}
  • If log.txt has no content after the test message, your webhooks may be setup wrong.
  • If there is an updateArray but the chatID is blank, the variable for the chatID may have changed from $update[“message”][“chat”][“id”] – play around with it.