* 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
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import type { ReplyToMode } from "../../../../src/config/config.js";
|
|
|
|
export type DeliveryProgress = {
|
|
hasReplied: boolean;
|
|
hasDelivered: boolean;
|
|
};
|
|
|
|
export function createDeliveryProgress(): DeliveryProgress {
|
|
return {
|
|
hasReplied: false,
|
|
hasDelivered: false,
|
|
};
|
|
}
|
|
|
|
export function resolveReplyToForSend(params: {
|
|
replyToId?: number;
|
|
replyToMode: ReplyToMode;
|
|
progress: DeliveryProgress;
|
|
}): number | undefined {
|
|
return params.replyToId && (params.replyToMode === "all" || !params.progress.hasReplied)
|
|
? params.replyToId
|
|
: undefined;
|
|
}
|
|
|
|
export function markReplyApplied(progress: DeliveryProgress, replyToId?: number): void {
|
|
if (replyToId && !progress.hasReplied) {
|
|
progress.hasReplied = true;
|
|
}
|
|
}
|
|
|
|
export function markDelivered(progress: DeliveryProgress): void {
|
|
progress.hasDelivered = true;
|
|
}
|
|
|
|
export async function sendChunkedTelegramReplyText<
|
|
TChunk,
|
|
TReplyMarkup = unknown,
|
|
TProgress extends DeliveryProgress = DeliveryProgress,
|
|
>(params: {
|
|
chunks: readonly TChunk[];
|
|
progress: TProgress;
|
|
replyToId?: number;
|
|
replyToMode: ReplyToMode;
|
|
replyMarkup?: TReplyMarkup;
|
|
replyQuoteText?: string;
|
|
quoteOnlyOnFirstChunk?: boolean;
|
|
markDelivered?: (progress: TProgress) => void;
|
|
sendChunk: (opts: {
|
|
chunk: TChunk;
|
|
isFirstChunk: boolean;
|
|
replyToMessageId?: number;
|
|
replyMarkup?: TReplyMarkup;
|
|
replyQuoteText?: string;
|
|
}) => Promise<void>;
|
|
}): Promise<void> {
|
|
const applyDelivered = params.markDelivered ?? markDelivered;
|
|
for (let i = 0; i < params.chunks.length; i += 1) {
|
|
const chunk = params.chunks[i];
|
|
if (!chunk) {
|
|
continue;
|
|
}
|
|
const isFirstChunk = i === 0;
|
|
const replyToMessageId = resolveReplyToForSend({
|
|
replyToId: params.replyToId,
|
|
replyToMode: params.replyToMode,
|
|
progress: params.progress,
|
|
});
|
|
const shouldAttachQuote =
|
|
Boolean(replyToMessageId) &&
|
|
Boolean(params.replyQuoteText) &&
|
|
(params.quoteOnlyOnFirstChunk !== true || isFirstChunk);
|
|
await params.sendChunk({
|
|
chunk,
|
|
isFirstChunk,
|
|
replyToMessageId,
|
|
replyMarkup: isFirstChunk ? params.replyMarkup : undefined,
|
|
replyQuoteText: shouldAttachQuote ? params.replyQuoteText : undefined,
|
|
});
|
|
markReplyApplied(params.progress, replyToMessageId);
|
|
applyDelivered(params.progress);
|
|
}
|
|
}
|