Very Basic PHP Telegram Bot w/Webhooks
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:
- We are creating a script which will speak with Telegram by receiving JSON post variables and sending GET variables (ie in the URL).
- You will tell Telegram where to find this script is IE which URL to send new message info to (“Set Webhooks)
- 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
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.
10 December, 2015 @ 4:47 pm
Hi, nice tutorial, unfortunately I’ve problems with a bot that should work with a PHP server on Openshift.
If i try your code I get this error: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request….on line 15
I’ve used a different code:
$bottoken = “HATOUKEN”;
$website = “https://api.telegram.org/bot”.$bottoken;
$update = file_get_contents(“php://input”);
print_r($update);
Unfortunately it doesn’t print anything, but the webhook has been created correctly…I think there is some problem with HTTPS but I’m not sure.
More details are on stackoverflow:
http://stackoverflow.com/questions/32166562/create-own-bot-on-telegram-with-php/34175352#34175352
19 January, 2016 @ 7:49 pm
Hi,
Is it possible to use this with a PHP file hosted on a non secure (http://) website?
17 February, 2016 @ 11:08 am
Unfortunately not. That is as required by Telegram.
5 February, 2016 @ 7:07 pm
Step 2 is missing “bot” in the url.
Webhook url is build like this:
https://api.telegram.org/bot%5BYOURBOT:TOKEN%5D/setWebhook?url=%5ByourBotURL%5D
17 February, 2016 @ 11:09 am
You’re right. I’d fixed that initially then lost it in messing about with revisions. Thanks for spotting it.
PHP Telegram bot: Using Cron jobs | Area36 - Niels Beekhuis
9 April, 2016 @ 5:47 pm
[…] first part of the code I got from this blog. Just a basic piece of code. I suggest that you grab his code. I did and it helped me […]
9 June, 2016 @ 4:24 pm
Hi, It is a nice basicly tutorial on whole of the world wide web.
and ” U are BEST “. (Maybe, in Real World)
Best Regards.
Vahid
10 July, 2016 @ 3:07 am
Thank’s , it helped me alot .
23 August, 2016 @ 2:01 pm
hi, I ‘v tried this but nothing is passed to my webhook file.
my ssl certificate is self signed. is there a problem?
17 January, 2017 @ 4:03 am
I also have a selfsigned ssl and I get a handshake-error:
SSL routines:ssl23_read:ssl handshake failure.. I can’t seem to find anything on this.
13 March, 2017 @ 1:55 pm
Thanks a lot!
23 March, 2017 @ 2:37 am
Works flawlessly. +1
30 June, 2017 @ 10:20 pm
thanks for the quick tutorial. super to get setup in a quick way!