* Send a message with automatic retry on 429 (rate limit).
(
chatId: number,
text: string,
maxRetries = 3,
)
| 271 | * Send a message with automatic retry on 429 (rate limit). |
| 272 | */ |
| 273 | private async sendWithRetry( |
| 274 | chatId: number, |
| 275 | text: string, |
| 276 | maxRetries = 3, |
| 277 | ): Promise<void> { |
| 278 | for (let attempt = 0; attempt <= maxRetries; attempt++) { |
| 279 | try { |
| 280 | await this.bot!.sendMessage(chatId, formatTelegramMessage(text), { |
| 281 | parse_mode: "HTML", |
| 282 | }); |
| 283 | return; |
| 284 | } catch (err: any) { |
| 285 | if ( |
| 286 | err?.response?.statusCode === 429 && |
| 287 | attempt < maxRetries |
| 288 | ) { |
| 289 | const retryAfter = |
| 290 | err.response?.body?.parameters?.retry_after || 5; |
| 291 | console.log( |
| 292 | `[Telegram] Rate limited, retrying after ${retryAfter}s...`, |
| 293 | ); |
| 294 | await new Promise((resolve) => |
| 295 | setTimeout(resolve, retryAfter * 1000), |
| 296 | ); |
| 297 | continue; |
| 298 | } |
| 299 | throw err; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Safe send that catches and logs errors. |
no test coverage detected