Telegram Bot PHP Webhook Issues with CloudFlare SSL Setup

I’m having trouble getting my Telegram bot to work properly with webhooks. I’ve been trying to figure this out for a while now but can’t seem to get it working.

I set up a Telegram bot and got my token. Then I configured the webhook using this URL format:

https://api.telegram.org/bot[MY_BOT_TOKEN]/setWebhook?url=https://mydomain.com/handler.php

The API response shows:

{"ok": true, "result": true, "description": "Webhook was set"}

Here’s my handler.php file:

<?php

function logData($userID, $requestData) {
    $logFile = "debug.txt";
    $dataString = print_r($requestData, TRUE);
    $fileHandle = fopen($logFile, 'a') or die("Cannot open file");
    fwrite($fileHandle, $userID . "\n\n");
    fwrite($fileHandle, $dataString . "\n\n");
    fclose($fileHandle);
}

function createResponse() {
    $text = "Hello! I'm a test bot.";
    return $text;
}

define('TOKEN', '[MY_BOT_TOKEN]');
define('BASE_URL', 'https://api.telegram.org/bot' . TOKEN . '/');

$input = file_get_contents("php://input");
$data = json_decode($input, true);
$userID = $data["message"]["chat"]["id"];

$response = createResponse();

$apiCall = BASE_URL . "sendMessage?chat_id=" . $userID . "&text=" . $response;
file_get_contents($apiCall);

logData($userID, $data);

?>

The problem is that when I send messages to the bot, nothing happens. No responses come back and the log file stays empty. I’m using CloudFlare with Flexible SSL on my domain. How can I troubleshoot this issue?

had the same issue last month. check if your server’s actually getting the requests - add error_log("webhook hit: " . date('Y-m-d H:i:s')); at the top of handler.php to see if anything comes through. also, your code might crash on empty messages, so add some null checks before accessing $data["message"]["chat"]["id"].

Had this exact issue six months ago. Your webhook looks right, but there’s probably a mismatch between CloudFlare and what Telegram expects. The handler.php code seems fine - I bet the requests aren’t even hitting your server. First, try bypassing CloudFlare. Switch your DNS record from ‘Proxied’ (orange cloud) to ‘DNS only’ (gray cloud). If messages start coming through, you’ve found your problem. Also check your webhook URL with the getWebhookInfo API. Sometimes it gets corrupted during SSL handshake. Delete the webhook with deleteWebhook, wait a few minutes, then set it up fresh. Don’t forget to check your server error logs in cPanel. PHP errors could be killing the script before it writes to your debug file.

CloudFlare’s Flexible SSL is interfering with your webhooks. I encountered a similar problem when I set up my first Telegram bot. The core issue is that while Telegram sends HTTPS requests, CloudFlare often converts them to HTTP before reaching your server, which disrupts the delivery of webhooks.

To resolve this, change your SSL setting in CloudFlare to Full SSL mode, or better yet, to Full SSL Strict if you can verify a valid SSL certificate on your server. Additionally, consider implementing error handling in your PHP script by wrapping the json_decode in a try-catch block. This way, you’ll be able to log any errors that might occur.

Lastly, after updating your SSL settings, make sure to delete and recreate your webhook. Telegram may have marked your endpoint as inactive due to the previous issues.