Skip to content

Commit 6205948

Browse files
authored
Merge pull request #11 from devrabie/feature-webhook-secret-token-validation
Feature webhook secret token validation
2 parents 6aa52b9 + cb991b2 commit 6205948

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,51 @@ $redis = \Longman\TelegramBot\Telegram::getRedis();
116116

117117
---
118118

119+
## 🔐 Webhook Secret Token
120+
121+
For enhanced security, you can set a secret token when you [set your webhook](https://core.telegram.org/bots/api#setwebhook). Telegram will then send this token in the `X-Telegram-Bot-Api-Secret-Token` header with every update. This library can automatically validate this token for you.
122+
123+
### 1. Set the Webhook with a Secret Token
124+
125+
When setting your webhook, provide a `secret_token`:
126+
127+
```php
128+
$telegram->setWebhook('https://your-domain.com/hook.php', [
129+
'secret_token' => 'YOUR_SECRET_TOKEN',
130+
]);
131+
```
132+
133+
### 2. Configure Your Bot to Verify the Token
134+
135+
In your webhook handler (e.g., `hook.php`), set the same secret token on your `Telegram` object. The library will then automatically check the header on incoming requests and throw an exception if the token is missing or invalid.
136+
137+
```php
138+
<?php
139+
140+
require_once __DIR__ . '/vendor/autoload.php';
141+
142+
$bot_api_key = 'YOUR_BOT_API_KEY';
143+
$bot_username = 'YOUR_BOT_USERNAME';
144+
$bot_secret = 'YOUR_SECRET_TOKEN';
145+
146+
try {
147+
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
148+
149+
// Set the secret token for incoming webhook requests
150+
$telegram->setSecretToken($bot_secret);
151+
152+
// Handle the update
153+
$telegram->handle();
154+
155+
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
156+
// Log the error
157+
error_log($e->getMessage());
158+
}
159+
```
160+
161+
This ensures that only requests from Telegram with the correct secret token are processed by your bot.
162+
163+
---
164+
119165
🙏 Acknowledgments
120166
A huge thanks to the original developers of longman/php-telegram-bot for their incredible work that formed the foundation of this project.

src/Request.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,4 +1065,14 @@ public static function setChatMenuButton(array $data): ServerResponse
10651065
{
10661066
return static::send('setChatMenuButton', $data);
10671067
}
1068+
1069+
/**
1070+
* Get the secret token header from the request
1071+
*
1072+
* @return string|null
1073+
*/
1074+
public static function getSecretTokenHeader(): ?string
1075+
{
1076+
return $_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN'] ?? null;
1077+
}
10681078
}

src/Telegram.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Telegram
3636
*
3737
* @var string
3838
*/
39-
protected $version = '1.0.7';
39+
protected $version = '1.0.9';
4040

4141
/** @var \Redis|null */
4242
private static $redis_connection;
@@ -69,6 +69,13 @@ class Telegram
6969
*/
7070
protected $input = '';
7171

72+
/**
73+
* Secret token to authorise webhook requests
74+
*
75+
* @var string
76+
*/
77+
protected $secret_token = '';
78+
7279
/**
7380
* Custom commands paths
7481
*
@@ -502,6 +509,10 @@ public function handle(): bool
502509
throw new TelegramException('Bot Username is not defined!');
503510
}
504511

512+
if ($this->secret_token !== '' && $this->secret_token !== Request::getSecretTokenHeader()) {
513+
throw new TelegramException('Secret token is invalid!');
514+
}
515+
505516
$input = Request::getInput();
506517
if (empty($input)) {
507518
throw new TelegramException('Input is empty! The webhook must not be called manually, only by Telegram.');
@@ -1281,6 +1292,20 @@ public function getUpdateFilter(): ?callable
12811292
return $this->update_filter;
12821293
}
12831294

1295+
/**
1296+
* Set the secret token to be used for webhook verification
1297+
*
1298+
* @param string $secret_token
1299+
*
1300+
* @return Telegram
1301+
*/
1302+
public function setSecretToken(string $secret_token): Telegram
1303+
{
1304+
$this->secret_token = $secret_token;
1305+
1306+
return $this;
1307+
}
1308+
12841309
/**
12851310
* Converts the name of a class into the name of a command.
12861311
*

0 commit comments

Comments
 (0)