Connecting Telegraming webhook on a PHP API

When you have registered and setup your Telegram bot you can fetch updates sent to it by using the getUpdates request. In case you have some sort of API its not always the best way to manualy fetch updates.

You can setup a webhook which connects to your end when receiving a message.

First create a page where only telegram should connect to. In this example I will use “telegram_webhook.php” as the endpoint for telegram to connect to. Just create an empty page so that telegram does not receive an error when connecting to it.

<?php
// Telegram Webhook
?>

Now secure this page using an .htaccess file and only allow the ranges from Telegram to connect. Do NOT forget this part as nobody should be able to connect to this file other than Telegram!

<FilesMatch "telegram_webhook.php">
        # Telegram API
        Require ip 149.154.160.0/20
        Require ip 91.108.4.0/22
</FilesMatch>

Now set the bot to use the webhook by making a call to the Telegram Core API (Fill in your own domain and BOT ID which you should got when creating the bot.

curl -F "url=https://<YOUR_DOMAIN>/telegram_webhook.php" https://api.telegram.org/bot&lt;YOUR_BOT_ID>/setWebhookInfo

Check out via a browser if the webhook is set correctly

https://api.telegram.org/bot&lt;YOUR_BOT_ID>/getWebhookInfo

It should state a page where the URL shows the target where your set your webhook to. In this case https://<YOUR_DOMAIN>/telegram_webhook.php

If all of this is OK you can start fetching the data being send to your webhook page.

<?php
$TELEGRAM_BOT = '<YOUR_BOT_ID>'
$TELEGRAM_API = "https://api.telegram.org/bot$telegrambot";
$TELEGRAM_RCV = json_decode(file_get_contents("php://input"), TRUE);

// ChatID needed to reply in the same chat
$telegramchatid = $TELEGRAM_RCV["message"]["chat"]["id"];
// Firstname of the person
$FIRSTNAME = $TELEGRAM_RCV["message"]["chat"]["first_name"];
// The full message
$MESSAGE = $TELEGRAM_RCV["message"]["text"];
// Fetch the first word, ea for commands
$FIRST_WORD = explode(" ", strtolower($TELEGRAM_RCV["message"]["text"]));
?>

Please set your own BOT ID again as a variable and you are ready to receive data.

There is many more data which you can fetch which you can find on the Telegram Core API site.

Now with this data you can for example make a PING PONG reply using your own PHP skills. Also you can use the telegram function created before

<?php
switch ($FIRST_WORD) {
   // Call towards telegram function
   case 'ping': telegram("pong!"); break;
}

Now if you save this page and send a message to the bot using “ping” it should reply back with “pong”. You can also reply back by replying with the persons firstname

telegram("Hi $FIRSTNAME!");

In case you need any help just drop a message below! It is kinda tricky where as in the earlier post I used a static ChatID. You can register accounts, save ChatID’s into a database and reply to all the users by looping true all registered ChatID’s. Again if you need help, just let me know!

Leave a Reply

Your email address will not be published.