mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-17 13:00:48 +00:00
* refactor: move Telegram channel implementation to extensions/telegram/src/ Move all Telegram channel code (123 files + 10 bot/ files + 8 channel plugin files) from src/telegram/ and src/channels/plugins/*/telegram.ts to extensions/telegram/src/. Leave thin re-export shims at original locations so cross-cutting src/ imports continue to resolve. - Fix all relative import paths in moved files (../X/ -> ../../../src/X/) - Fix vi.mock paths in 60 test files - Fix inline typeof import() expressions - Update tsconfig.plugin-sdk.dts.json rootDir to "." for cross-directory DTS - Update write-plugin-sdk-entry-dts.ts for new rootDir structure - Move channel plugin files with correct path remapping * fix: support keyed telegram send deps * fix: sync telegram extension copies with latest main * fix: correct import paths and remove misplaced files in telegram extension * fix: sync outbound-adapter with main (add sendTelegramPayloadMessages) and fix delivery.test import path
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { resolveGlobalMap } from "../../../src/shared/global-singleton.js";
|
|
|
|
/**
|
|
* In-memory cache of sent message IDs per chat.
|
|
* Used to identify bot's own messages for reaction filtering ("own" mode).
|
|
*/
|
|
|
|
const TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
|
|
type CacheEntry = {
|
|
timestamps: Map<number, number>;
|
|
};
|
|
|
|
/**
|
|
* Keep sent-message tracking shared across bundled chunks so Telegram reaction
|
|
* filters see the same sent-message history regardless of which chunk recorded it.
|
|
*/
|
|
const TELEGRAM_SENT_MESSAGES_KEY = Symbol.for("openclaw.telegramSentMessages");
|
|
|
|
const sentMessages = resolveGlobalMap<string, CacheEntry>(TELEGRAM_SENT_MESSAGES_KEY);
|
|
|
|
function getChatKey(chatId: number | string): string {
|
|
return String(chatId);
|
|
}
|
|
|
|
function cleanupExpired(entry: CacheEntry): void {
|
|
const now = Date.now();
|
|
for (const [msgId, timestamp] of entry.timestamps) {
|
|
if (now - timestamp > TTL_MS) {
|
|
entry.timestamps.delete(msgId);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Record a message ID as sent by the bot.
|
|
*/
|
|
export function recordSentMessage(chatId: number | string, messageId: number): void {
|
|
const key = getChatKey(chatId);
|
|
let entry = sentMessages.get(key);
|
|
if (!entry) {
|
|
entry = { timestamps: new Map() };
|
|
sentMessages.set(key, entry);
|
|
}
|
|
entry.timestamps.set(messageId, Date.now());
|
|
// Periodic cleanup
|
|
if (entry.timestamps.size > 100) {
|
|
cleanupExpired(entry);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a message was sent by the bot.
|
|
*/
|
|
export function wasSentByBot(chatId: number | string, messageId: number): boolean {
|
|
const key = getChatKey(chatId);
|
|
const entry = sentMessages.get(key);
|
|
if (!entry) {
|
|
return false;
|
|
}
|
|
// Clean up expired entries on read
|
|
cleanupExpired(entry);
|
|
return entry.timestamps.has(messageId);
|
|
}
|
|
|
|
/**
|
|
* Clear all cached entries (for testing).
|
|
*/
|
|
export function clearSentMessageCache(): void {
|
|
sentMessages.clear();
|
|
}
|