From b37e5f29a39fb55c40ea75256afb81dc7b105d66 Mon Sep 17 00:00:00 2001 From: NecatiY Date: Sun, 9 Aug 2026 22:10:22 +0300 Subject: [PATCH] fix(telegram): tolerate out-of-range message timestamps Prevent malformed Telegram timestamps from crashing event normalization. Preserve valid timestamps and fall back to the Unix epoch for invalid dates, mirroring the landed fixes for discord (#933) and signal (#932). - export toBotEvent and compute the Date once - use Number.isNaN(date.getTime()) fallback to new Date(0).toISOString() - add regression coverage for an out-of-range value (Number.NEGATIVE_INFINITY) Verified: packages/bots/telegram builds clean and all 16 tests pass locally (core built first, then telegram: 15 existing + 1 new). --- packages/bots/telegram/src/index.test.ts | 17 ++++++++++++++++- packages/bots/telegram/src/index.ts | 5 +++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/bots/telegram/src/index.test.ts b/packages/bots/telegram/src/index.test.ts index ec3b120a..2ee0657c 100644 --- a/packages/bots/telegram/src/index.test.ts +++ b/packages/bots/telegram/src/index.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; import { contractTestBot } from '@profullstack/sh1pt-core/testing'; -import bot, { loadConfig, parseTelegramChatId } from './index.js'; +import bot, { loadConfig, parseTelegramChatId, toBotEvent } from './index.js'; contractTestBot(bot, { sampleConfig: {}, sampleChannel: '1234567890' }); @@ -45,3 +45,18 @@ describe('loadConfig', () => { expect(config.maxConcurrentSessions).toBe(5); }); }); + +describe('toBotEvent', () => { + it('falls back when Telegram provides an out-of-range timestamp', () => { + expect(toBotEvent({ + source: 'user-1', + sourceName: 'User', + text: 'hello', + timestamp: Number.NEGATIVE_INFINITY, + chatId: 1234567890, + isGroup: false, + attachments: [], + raw: undefined as never, + }).timestamp).toBe('1970-01-01T00:00:00.000Z'); + }); +}); diff --git a/packages/bots/telegram/src/index.ts b/packages/bots/telegram/src/index.ts index c446c6a6..86f3f4f1 100644 --- a/packages/bots/telegram/src/index.ts +++ b/packages/bots/telegram/src/index.ts @@ -126,7 +126,8 @@ export function parseTelegramChatId(value: string): number { return chatId; } -function toBotEvent(msg: IncomingMessage): BotEvent { +export function toBotEvent(msg: IncomingMessage): BotEvent { + const date = new Date(msg.timestamp); return { type: "message", channel: String(msg.chatId), @@ -136,7 +137,7 @@ function toBotEvent(msg: IncomingMessage): BotEvent { }, text: msg.text, attachments: msg.attachments.map((a) => ({ url: a.url, filename: a.filename })), - timestamp: new Date(msg.timestamp).toISOString(), + timestamp: Number.isNaN(date.getTime()) ? new Date(0).toISOString() : date.toISOString(), raw: msg.raw, }; }