Skip to content

Commit 40125be

Browse files
feat: Add request timeout and Redis duplicate update prevention
- Added `Request::$timeout` and `Request::setClientTimeout` to control Guzzle client timeout. - Added Redis update de-duplication in `Telegram::processUpdate`. - Added `Telegram::setRedis` for injecting Redis instance. - Added `Telegram::$update_retention_time` and `Telegram::setUpdateRetentionTime` to configure de-duplication TTL. - Updated README.md with instructions. - Default request timeout is set to 60 seconds. - Duplicate updates are cached in Redis for 60 seconds (default) and return a fake success response.
1 parent 71313bc commit 40125be

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ You can also access the Redis instance statically from anywhere in your project:
114114
$redis = \Longman\TelegramBot\Telegram::getRedis();
115115
```
116116

117+
### 3. Prevent Duplicate Updates
118+
119+
Enabling Redis also activates the built-in duplicate update prevention mechanism. This is useful when the bot receives the same update multiple times due to timeouts or retries from Telegram.
120+
121+
When Redis is enabled:
122+
1. The library checks if the `update_id` exists in Redis.
123+
2. If it exists, the update is ignored, and a fake success response is returned to Telegram to stop retries.
124+
3. If it doesn't exist, the `update_id` is stored in Redis with a retention time (TTL) of 60 seconds.
125+
126+
You can customize the retention time (in seconds):
127+
128+
```php
129+
// Set retention time to 120 seconds
130+
Longman\TelegramBot\Telegram::setUpdateRetentionTime(120);
131+
```
132+
133+
---
134+
135+
## ⚙️ Advanced Configuration
136+
137+
### Request Timeout
138+
139+
To prevent your server from hanging on slow Telegram API requests (e.g., cURL error 28), you can set a custom timeout for the HTTP client. The default timeout is 60 seconds.
140+
141+
```php
142+
use Longman\TelegramBot\Request;
143+
144+
// Set request timeout to 30 seconds
145+
Request::setClientTimeout(30);
146+
```
147+
117148
---
118149

119150
## 🔐 Webhook Secret Token

src/Telegram.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class Telegram
4141
/** @var \Redis|null */
4242
private static $redis_connection;
4343

44+
/**
45+
* Update retention time in Redis (in seconds)
46+
*
47+
* @var int
48+
*/
49+
private static $update_retention_time = 60;
50+
4451
/**
4552
* Telegram API key
4653
*
@@ -566,7 +573,7 @@ public function processUpdate(Update $update): ServerResponse
566573
return new ServerResponse(['ok' => true, 'result' => true], $this->bot_username);
567574
}
568575
// Store the update ID in Redis with a TTL of 60 seconds (matching the default timeout).
569-
self::$redis_connection->setex($redis_key, 60, '1');
576+
self::$redis_connection->setex($redis_key, self::$update_retention_time, '1');
570577
}
571578

572579
if (is_callable($this->update_filter)) {
@@ -1151,6 +1158,17 @@ public static function setRedis(\Redis $redis): void
11511158
self::$redis_connection = $redis;
11521159
}
11531160

1161+
/**
1162+
* Set the update retention time in Redis
1163+
*
1164+
* @param int $seconds
1165+
* @return void
1166+
*/
1167+
public static function setUpdateRetentionTime(int $seconds): void
1168+
{
1169+
self::$update_retention_time = $seconds;
1170+
}
1171+
11541172
/**
11551173
* Get the shared Redis client instance.
11561174
*

0 commit comments

Comments
 (0)