feat: add telegram topic delivery for cron
This commit is contained in:
@@ -50,6 +50,36 @@ import { resolveTelegramToken } from "../telegram/token.js";
|
||||
import { normalizeE164 } from "../utils.js";
|
||||
import type { CronJob } from "./types.js";
|
||||
|
||||
/**
|
||||
* Parse a Telegram delivery target into chatId and optional topicId.
|
||||
* Supports formats:
|
||||
* - `chatId` (plain chat ID or @username)
|
||||
* - `chatId:topicId` (chat ID with topic/thread ID)
|
||||
* - `chatId:topic:topicId` (alternative format with explicit "topic" marker)
|
||||
*/
|
||||
export function parseTelegramTarget(to: string): {
|
||||
chatId: string;
|
||||
topicId: number | undefined;
|
||||
} {
|
||||
const trimmed = to.trim();
|
||||
|
||||
// Try format: chatId:topic:topicId
|
||||
const topicMatch = /^(.+?):topic:(\d+)$/.exec(trimmed);
|
||||
if (topicMatch) {
|
||||
return { chatId: topicMatch[1], topicId: parseInt(topicMatch[2], 10) };
|
||||
}
|
||||
|
||||
// Try format: chatId:topicId (where topicId is numeric)
|
||||
// Be careful not to match @username or other non-numeric suffixes
|
||||
const colonMatch = /^(.+):(\d+)$/.exec(trimmed);
|
||||
if (colonMatch) {
|
||||
return { chatId: colonMatch[1], topicId: parseInt(colonMatch[2], 10) };
|
||||
}
|
||||
|
||||
// Plain chatId, no topic
|
||||
return { chatId: trimmed, topicId: undefined };
|
||||
}
|
||||
|
||||
export type RunCronAgentTurnResult = {
|
||||
status: "ok" | "error" | "skipped";
|
||||
summary?: string;
|
||||
@@ -487,7 +517,7 @@ export async function runCronIsolatedAgentTurn(params: {
|
||||
summary: "Delivery skipped (no Telegram chatId).",
|
||||
};
|
||||
}
|
||||
const chatId = resolvedDelivery.to;
|
||||
const { chatId, topicId } = parseTelegramTarget(resolvedDelivery.to);
|
||||
const textLimit = resolveTextChunkLimit(params.cfg, "telegram");
|
||||
try {
|
||||
for (const payload of payloads) {
|
||||
@@ -501,6 +531,7 @@ export async function runCronIsolatedAgentTurn(params: {
|
||||
await params.deps.sendMessageTelegram(chatId, chunk, {
|
||||
verbose: false,
|
||||
token: telegramToken || undefined,
|
||||
messageThreadId: topicId,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -512,6 +543,7 @@ export async function runCronIsolatedAgentTurn(params: {
|
||||
verbose: false,
|
||||
mediaUrl: url,
|
||||
token: telegramToken || undefined,
|
||||
messageThreadId: topicId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user