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.

// 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
        Require ip

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";YOUR_BOT_ID>/setWebhookInfo

Check out via a browser if the webhook is set correctly;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.

$TELEGRAM_API = "$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

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!

2 thoughts on “Connecting Telegraming webhook on a PHP API

  1. Thanks …. Nice …. Now if we have the user’s mobile number ( and they are a telegram user ) could we message them directly ?
    How do we modify your code for this purpose ?

    Note – I dont need replies.. its just one way …

    1. Hi Marco,

      That is not possible. Telegram does NOT allow you to initiate a chat from the bot side.
      You will always need a ChatID to have communication. This is to pre fend companies to spam users with messages.
      I always start a chat and register the chatid in a database where you can fetch the ChatID from.

Leave a Reply

Your email address will not be published.