From 3e8e1efb84d3c9dbf438bf6d0a68eec1873c6539 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Tue, 13 Oct 2026 20:44:29 -0500 Subject: [PATCH] refactor(server): isolate inbound channel message policies (#16230) ## Thinking Path > - Paperclip helps people manage AI agents for work. > - Chat channels turn provider messages into shared task workflows. > - The main service still implements provider details beside common authorization and persistence. > - These details belong in the existing channel folders. > - This pull request moves inbound message filtering, command rendering, reply metadata, and setup-reply settlement behind explicit typed functions. > - The service keeps its shared workflow and selects the provider implementation directly. ## Linked Issues or Issue Description Refs: #16192. Stack part 1/5. Base: `master`. **What existing behavior does this improve?** The ownership and readability of provider behavior in the chat channel service. **Current behavior** The service implements inbound message filtering, command rendering, reply metadata, and setup-reply settlement inline. **Proposed behavior** Channel modules own these details. The core calls their typed functions. Runtime and database effects retain their current order. **Reason and benefit** Maintainers can find channel behavior in its channel folder without following a generic provider framework. **Breaking changes** None intended. Public service entry points and durable data shapes stay compatible. ## What Changed - Extract inbound message filtering, command rendering, reply metadata, and setup-reply settlement into the existing channel folders. - Keep shared company scope, authorization, and transaction ownership in their current workflow. - Reduce `chat-channels.ts` from 4082 to 3970 lines. ## Verification - The same 14 selected chat/Photon integration cases passed before and after extraction, including Telegram forum controls, guidance retries, Teams setup replies, and Photon conversation reuse. - Server typecheck passed. - `node scripts/check-module-boundaries.mjs` passed. - Full workspace typecheck and build passed on the cumulative stack. All current-head CI gates passed. Greptile completed at 5/5 with no findings. - The full local `pnpm test:run` command exited with three database fixture-startup hook timeouts. It reported 19,529 passing tests and 250 skipped tests. No assertion failed. Its later test groups did not run because the driver stops on the first failing group. - The timed-out files were `environment-custom-images-service.test.ts`, `generic-mcp-connection.test.ts`, and `modules/wake-queue/adapters/postgres.test.ts`. All 166 tests in these files passed in isolation on both baseline `edcfcaf3f6` and stack head `faaca26d46`, with existing timeouts and assertions. The tests and database-startup helper are unchanged. - Provider fixtures prove application integration. They do not prove live third-party execution. ## Risks - Incorrect inputs or changed await order could affect provider handling and recovery. - Existing integration tests exercise the assembled service before and after extraction. - No database migration or API change is included. ## Model Used OpenAI GPT-6, running in Codex with reasoning, tool use, and code execution. The exact deployment identifier and context-window size are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge Co-authored-by: Paperclip --- server/src/services/chat-channels.ts | 152 +++--------------- .../chat-channels/imessage-photon/messages.ts | 57 +++++++ .../chat-channels/microsoft-teams/messages.ts | 10 ++ .../microsoft-teams/setup-replies.ts | 41 +++++ .../services/chat-channels/slack/messages.ts | 9 ++ .../chat-channels/telegram/commands.ts | 21 +++ .../chat-channels/telegram/messages.ts | 7 + 7 files changed, 165 insertions(+), 132 deletions(-) create mode 100644 server/src/services/chat-channels/imessage-photon/messages.ts create mode 100644 server/src/services/chat-channels/microsoft-teams/setup-replies.ts diff --git a/server/src/services/chat-channels.ts b/server/src/services/chat-channels.ts index 099880c1733..9e314fb9180 100644 --- a/server/src/services/chat-channels.ts +++ b/server/src/services/chat-channels.ts @@ -1,3 +1,6 @@ +import { photonControlTargetsConversation, photonReplyMetadata, recordPhotonInboundActivity } from "./chat-channels/imessage-photon/messages.js"; +import { deferTeamsSetupReply } from "./chat-channels/microsoft-teams/setup-replies.js"; +import { slackOrphanFollowUp, stripSlackTitleMentions } from "./chat-channels/slack/messages.js"; import { createChatPublicationResolution } from "./chat-channels/publications/resolution.js"; import { createChatPublicationRequests } from "./chat-channels/publications/requests.js"; import { createTeamsBoardSendPreparation } from "./chat-channels/microsoft-teams/board-send-preparation.js"; @@ -18,7 +21,7 @@ import { createMessageResourceAdmission } from "./chat-channels/inbound/resource import type { DiscordGatewayOwnership } from "./chat-channels/runtime-types.js"; import { createInboundAdmission } from "./chat-channels/inbound/admission.js"; import { createInboundEnvelope } from "./chat-channels/inbound/envelope.js"; -import { sameTeamsThreadIdentity, durableExternalThreadIdentity, teamsThreadRootMessageId, canonicalProviderResourceId, nonDirectDestinationAllowed, linearControlCommand, actionThreadMatchesConversation } from "./chat-channels/inbound/surfaces.js"; +import { sameTeamsThreadIdentity, durableExternalThreadIdentity, canonicalProviderResourceId, nonDirectDestinationAllowed, linearControlCommand, actionThreadMatchesConversation } from "./chat-channels/inbound/surfaces.js"; import { createChatInboundAuthority } from "./chat-channels/inbound/authorization.js"; import { createTeamsRoutes } from "./chat-channels/microsoft-teams/routes.js"; import type { LiveInboundMessage } from "./chat-channels/runtime-types.js"; @@ -49,7 +52,7 @@ import { createSlackTaskAuthorityRegistration } from "./chat-channels/slack/task import { createGitHubPublicationPolicy } from "./chat-channels/github/publication-policy.js"; import { createTelegramAttachmentFailure } from "./chat-channels/telegram/attachment-failure.js"; import { createSlackCommands } from "./chat-channels/slack/commands.js"; -import { createTelegramCommands, telegramGuidanceCommand } from "./chat-channels/telegram/commands.js"; +import { createTelegramCommands, telegramGuidanceCommand, telegramGuidanceResponse, telegramForumControlResponse } from "./chat-channels/telegram/commands.js"; import { createTeamsFileDelivery } from "./chat-channels/microsoft-teams/file-delivery.js"; import { createGitHubSetupSecret } from "./chat-channels/github/setup-secret.js"; import { createSlackInventory } from "./chat-channels/slack/inventory.js"; @@ -97,10 +100,10 @@ import { MAX_INBOUND_TEXT, stableExternalPrincipalId, } from "./chat-channels/message-lifecycle.js"; -import { telegramLifecycleEventFromPayload, isTelegramEditedMessageRaw, isTelegramMigrationRaw, telegramZeroMessageId } from "./chat-channels/telegram/messages.js"; +import { telegramLifecycleEventFromPayload, telegramIgnoresInboundMessage } from "./chat-channels/telegram/messages.js"; import { - microsoftTeamsLifecycleEventFromPayload, + microsoftTeamsLifecycleEventFromPayload, teamsOrphanFollowUp, teamsAttachmentReferenceText, } from "./chat-channels/microsoft-teams/messages.js"; import { type GitHubLifecycleEvent, @@ -461,8 +464,7 @@ export interface ChatChannelServiceOptions { } function safeTitle(text: string, fallback: string): string { - const line = text - .replace(/<@[A-Z0-9]+>/gi, "") + const line = stripSlackTitleMentions(text) .replace(/@[\w.-]+(?:\[bot\])?/gi, "") .trim() .split(/\r?\n/)[0]; @@ -1432,25 +1434,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { suppressSetupDestinationActivation = false, slackSlashControl = false, ) { - if ( - endpoint.provider === "telegram" && - (telegramZeroMessageId(message.id) || - (message.raw !== null && - typeof message.raw === "object" && - (message.raw as { message_id?: unknown }).message_id === 0)) - ) - return; - // The Telegram adapter currently emits edited_message through the normal - // message callback with the original message id. Paperclip records that - // verified payload through its supplemental message_updated lifecycle - // ledger instead; letting it reach normal dedupe would falsely report the - // edit as a duplicate provider delivery. - if ( - endpoint.provider === "telegram" && - (isTelegramEditedMessageRaw(message.raw) || - isTelegramMigrationRaw(message.raw)) - ) - return; + if (endpoint.provider === "telegram" && telegramIgnoresInboundMessage(message)) return; if ( message.author.isMe || message.author.isBot === true || @@ -1619,35 +1603,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { try { if (admission.provisionalTeamsSetupReply) { - const resolutionAt = new Date(); - const waitForSetupRoot = - activeDelivery.attempts <= ORPHAN_FOLLOW_UP_MAX_ATTEMPTS; - await db.transaction(async (tx) => { - await notifyChatDeliveryWork(tx); - return tx - .update(chatDeliveries) - .set( - waitForSetupRoot - ? { - state: "retry", - nextAttemptAt: new Date( - resolutionAt.getTime() + ORPHAN_FOLLOW_UP_GRACE_MS, - ), - redactedError: "Waiting briefly for an earlier root mention", - updatedAt: resolutionAt, - } - : { - state: "filtered", - normalizedEvent: redactedDestinationNormalized, - principalId: null, - nextAttemptAt: null, - processedAt: resolutionAt, - redactedError: "Destination is not enabled in Paperclip", - updatedAt: resolutionAt, - }, - ) - .where(eq(chatDeliveries.id, activeDelivery.id)); - }); + await deferTeamsSetupReply(db, activeDelivery, redactedDestinationNormalized, ORPHAN_FOLLOW_UP_MAX_ATTEMPTS, ORPHAN_FOLLOW_UP_GRACE_MS); return; } // A committed inbound message link proves that a prior attempt completed @@ -1890,23 +1846,10 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { destinationAllowed && principalAllowed && activationAllowed; - const slackRootMessageId = - endpoint.provider === "slack" - ? /^slack:[^:]+:(.+)$/.exec(thread.id)?.[1] - : null; - const teamsRootMessageId = - endpoint.provider === "microsoft-teams" - ? teamsThreadRootMessageId(thread.id) - : null; const isPlausibleOrphanFollowUp = - endpoint.provider === "github" || - endpoint.provider === "discord" || - (endpoint.provider === "slack" && - Boolean(slackRootMessageId) && - slackRootMessageId !== message.id) || - (endpoint.provider === "microsoft-teams" && - Boolean(teamsRootMessageId) && - teamsRootMessageId !== message.id); + endpoint.provider === "github" || endpoint.provider === "discord" || + (endpoint.provider === "slack" && slackOrphanFollowUp(thread.id, message.id)) || + (endpoint.provider === "microsoft-teams" && teamsOrphanFollowUp(thread.id, message.id)); const setupDestinationCanBeEnabledByEarlierMention = (endpoint.provider === "github" || endpoint.provider === "discord" || @@ -2022,19 +1965,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { ? photonReplyReference(message.raw) : null; if (controlCommand && photonQuote) { - const source = await db - .select({ conversationId: chatMessageLinks.conversationId }) - .from(chatMessageLinks) - .where( - and( - eq(chatMessageLinks.companyId, endpoint.companyId), - eq(chatMessageLinks.endpointId, endpoint.id), - eq(chatMessageLinks.providerMessageId, photonQuote.guid), - ), - ) - .limit(1) - .then((rows) => rows[0]); - if (!source || source.conversationId !== existingConversation?.id) { + if (!(await photonControlTargetsConversation(db, endpoint, photonQuote.guid, existingConversation?.id))) { await db.transaction(async (tx) => { await notifyChatDeliveryWork(tx); return tx @@ -2067,12 +1998,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { ) .then((rows) => rows[0]?.name ?? "this agent") : null; - const responseText = - guidanceCommand === "start" - ? `Send a direct message to start work with ${assignedAgentName}. In a group, use /task@${endpoint.botUsername ?? "your_bot"} followed by your request. Use /status, /new, or /close to manage the active task in this chat.` - : guidanceCommand === "task" - ? `Please include a request after /task@${endpoint.botUsername ?? "your_bot"}.` - : `Available commands: /task@${endpoint.botUsername ?? "your_bot"} followed by your request, /status, /new, and /close.`; + const responseText = telegramGuidanceResponse(guidanceCommand, endpoint.botUsername, assignedAgentName); const publicationBinding = existingConversation && existingIssue ? { conversation: existingConversation, issue: existingIssue } @@ -2169,13 +2095,11 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { : taskLabel : controlCommand === "new" ? isTelegramForumTopic - ? existingIssue - ? `${taskLabel} stays bound to this forum topic. Open a new Telegram forum topic to start a new Paperclip task.` - : "Open a new Telegram forum topic to start a new Paperclip task." + ? telegramForumControlResponse("new", taskLabel, Boolean(existingIssue), Boolean(existingConversation)) : "Send your request to start a new Paperclip task." : existingConversation ? isTelegramForumTopic - ? "This chat conversation is closed. A later message here will continue the same Paperclip task." + ? telegramForumControlResponse("close", taskLabel, Boolean(existingIssue), true) : "This chat conversation is closed. Send another message to start a new task." : "No task is active. Send a message to start one."; const publicationBinding = @@ -2437,7 +2361,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { ? taskEndpoint.provider === "microsoft-teams" && !thread.isDM && nativeInboundAttachments.length === 0 - ? `Shared ${message.attachments.length} Microsoft Teams file reference${message.attachments.length === 1 ? "" : "s"}.${providerUrl ? ` Open in Microsoft Teams: ${providerUrl}` : ""}` + ? teamsAttachmentReferenceText(message.attachments.length, providerUrl) : `Shared ${message.attachments.length} file${message.attachments.length === 1 ? "" : "s"}.` : taskEndpoint.provider === "slack" && (trigger === "mention" || message.isMention === true) ? "Started a conversation by mentioning the agent." @@ -2475,26 +2399,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { { title: `${PROVIDER_LABELS[endpoint.provider]} sender`, rows: [ - ...(endpoint.provider === "imessage-photon" && - photonReplyReference(message.raw) - ? [ - { - type: "key_value" as const, - label: "Reply to message", - value: photonReplyReference(message.raw)!.guid, - }, - ...(photonReplyReference(message.raw)!.part - ? [ - { - type: "key_value" as const, - label: "Reply part", - value: photonReplyReference(message.raw)! - .part!, - }, - ] - : []), - ] - : []), + ...(endpoint.provider === "imessage-photon" ? photonReplyMetadata(message.raw) : []), { type: "key_value", label: "Name", @@ -2594,24 +2499,7 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { actorUserId: taskUserId, }); if (taskEndpoint.provider === "imessage-photon") { - await logActivity( - taskTx as Db, - { - companyId: taskEndpoint.companyId, - actorType: taskUserId ? "user" : "system", - actorId: taskUserId ?? "chat:imessage-photon", - action: "issue.comment_added", - entityType: "issue", - entityId: issue.id, - details: { - commentId: comment.id, - issueIdentifier: issue.identifier, - source: "chat:imessage-photon", - endpointId: taskEndpoint.id, - }, - }, - inboundActivityPublications, - ); + await recordPhotonInboundActivity(taskTx, taskEndpoint, issue, comment.id, taskUserId, inboundActivityPublications); } return { actorUserId: taskUserId, comment, conversation, issue }; }; diff --git a/server/src/services/chat-channels/imessage-photon/messages.ts b/server/src/services/chat-channels/imessage-photon/messages.ts new file mode 100644 index 00000000000..3a591f798c1 --- /dev/null +++ b/server/src/services/chat-channels/imessage-photon/messages.ts @@ -0,0 +1,57 @@ +import { chatMessageLinks, type Db, type issues } from "@paperclipai/db"; +import { and, eq } from "drizzle-orm"; +import { photonReplyReference } from "../../photon/adapter.js"; +import { logActivity, type ActivityPublication } from "../../activity-log.js"; +import type { DbOrTransaction, EndpointRow } from "../runtime-types.js"; + +export async function photonControlTargetsConversation(db: Db, endpoint: EndpointRow, guid: string, conversationId: string | undefined): Promise { + const source = await db + .select({ conversationId: chatMessageLinks.conversationId }) + .from(chatMessageLinks) + .where( + and( + eq(chatMessageLinks.companyId, endpoint.companyId), + eq(chatMessageLinks.endpointId, endpoint.id), + eq(chatMessageLinks.providerMessageId, guid), + ), + ) + .limit(1) + .then((rows) => rows[0]); + return Boolean(source && source.conversationId === conversationId); +} + +export function photonReplyMetadata(raw: unknown) { + const reference = photonReplyReference(raw); + return reference ? [ + { type: "key_value" as const, label: "Reply to message", value: reference.guid }, + ...(reference.part ? [{ type: "key_value" as const, label: "Reply part", value: reference.part }] : []), + ] : []; +} + +export async function recordPhotonInboundActivity( + taskTx: DbOrTransaction, + taskEndpoint: EndpointRow, + issue: Pick, + commentId: string, + taskUserId: string | null, + inboundActivityPublications: ActivityPublication[], +): Promise { + await logActivity( + taskTx as Db, + { + companyId: taskEndpoint.companyId, + actorType: taskUserId ? "user" : "system", + actorId: taskUserId ?? "chat:imessage-photon", + action: "issue.comment_added", + entityType: "issue", + entityId: issue.id, + details: { + commentId: commentId, + issueIdentifier: issue.identifier, + source: "chat:imessage-photon", + endpointId: taskEndpoint.id, + }, + }, + inboundActivityPublications, + ); +} diff --git a/server/src/services/chat-channels/microsoft-teams/messages.ts b/server/src/services/chat-channels/microsoft-teams/messages.ts index c5ed87cebf9..9c27c732e67 100644 --- a/server/src/services/chat-channels/microsoft-teams/messages.ts +++ b/server/src/services/chat-channels/microsoft-teams/messages.ts @@ -1,3 +1,4 @@ +import { teamsThreadRootMessageId } from "../inbound/surfaces.js"; import { createHash } from "node:crypto"; import type { ChatEndpointRuntime } from "../../voice/voice-runtime.js"; import { MAX_INBOUND_TEXT, lifecycleActorFromAuthor, type LifecycleActor } from "../message-lifecycle.js"; @@ -97,3 +98,12 @@ export function teamsActivitySentAt(raw: unknown): Date | null { const sentAt = new Date(value); return Number.isFinite(sentAt.getTime()) ? sentAt : null; } + +export function teamsOrphanFollowUp(threadId: string, messageId: string): boolean { + const rootMessageId = teamsThreadRootMessageId(threadId); + return Boolean(rootMessageId) && rootMessageId !== messageId; +} + +export function teamsAttachmentReferenceText(count: number, providerUrl: string | null): string { + return `Shared ${count} Microsoft Teams file reference${count === 1 ? "" : "s"}.${providerUrl ? ` Open in Microsoft Teams: ${providerUrl}` : ""}`; +} diff --git a/server/src/services/chat-channels/microsoft-teams/setup-replies.ts b/server/src/services/chat-channels/microsoft-teams/setup-replies.ts new file mode 100644 index 00000000000..1b25888c44a --- /dev/null +++ b/server/src/services/chat-channels/microsoft-teams/setup-replies.ts @@ -0,0 +1,41 @@ +import { chatDeliveries, type Db } from "@paperclipai/db"; +import { eq } from "drizzle-orm"; +import { notifyChatDeliveryWork } from "../../chat-work-notifications.js"; + +export async function deferTeamsSetupReply( + db: Db, + activeDelivery: Pick, + redactedDestinationNormalized: typeof chatDeliveries.$inferSelect["normalizedEvent"], + ORPHAN_FOLLOW_UP_MAX_ATTEMPTS: number, + ORPHAN_FOLLOW_UP_GRACE_MS: number, +): Promise { + const resolutionAt = new Date(); + const waitForSetupRoot = + activeDelivery.attempts <= ORPHAN_FOLLOW_UP_MAX_ATTEMPTS; + await db.transaction(async (tx) => { + await notifyChatDeliveryWork(tx); + return tx + .update(chatDeliveries) + .set( + waitForSetupRoot + ? { + state: "retry", + nextAttemptAt: new Date( + resolutionAt.getTime() + ORPHAN_FOLLOW_UP_GRACE_MS, + ), + redactedError: "Waiting briefly for an earlier root mention", + updatedAt: resolutionAt, + } + : { + state: "filtered", + normalizedEvent: redactedDestinationNormalized, + principalId: null, + nextAttemptAt: null, + processedAt: resolutionAt, + redactedError: "Destination is not enabled in Paperclip", + updatedAt: resolutionAt, + }, + ) + .where(eq(chatDeliveries.id, activeDelivery.id)); + }); +} diff --git a/server/src/services/chat-channels/slack/messages.ts b/server/src/services/chat-channels/slack/messages.ts index dece4360827..5536c335055 100644 --- a/server/src/services/chat-channels/slack/messages.ts +++ b/server/src/services/chat-channels/slack/messages.ts @@ -87,3 +87,12 @@ export function slackThreadChannelId(threadId: string): string | null { separator === -1 ? channelAndThread : channelAndThread.slice(0, separator); return channelId || null; } + +export function slackOrphanFollowUp(threadId: string, messageId: string): boolean { + const rootMessageId = /^slack:[^:]+:(.+)$/.exec(threadId)?.[1]; + return Boolean(rootMessageId) && rootMessageId !== messageId; +} + +export function stripSlackTitleMentions(text: string): string { + return text.replace(/<@[A-Z0-9]+>/gi, ""); +} diff --git a/server/src/services/chat-channels/telegram/commands.ts b/server/src/services/chat-channels/telegram/commands.ts index 70ce398dfa0..055496f7de3 100644 --- a/server/src/services/chat-channels/telegram/commands.ts +++ b/server/src/services/chat-channels/telegram/commands.ts @@ -107,3 +107,24 @@ export function createTelegramCommands(options: TelegramCommandsOptions) { } return { handleTelegramSlashCommand }; } + +export function telegramGuidanceResponse( + guidanceCommand: NonNullable>, + botUsername: string | null, + assignedAgentName: string | null, +): string { + return guidanceCommand === "start" + ? `Send a direct message to start work with ${assignedAgentName}. In a group, use /task@${botUsername ?? "your_bot"} followed by your request. Use /status, /new, or /close to manage the active task in this chat.` + : guidanceCommand === "task" + ? `Please include a request after /task@${botUsername ?? "your_bot"}.` + : `Available commands: /task@${botUsername ?? "your_bot"} followed by your request, /status, /new, and /close.`; +} + +export function telegramForumControlResponse(command: "new" | "close", taskLabel: string, hasIssue: boolean, hasConversation: boolean): string { + if (command === "new") return hasIssue + ? `${taskLabel} stays bound to this forum topic. Open a new Telegram forum topic to start a new Paperclip task.` + : "Open a new Telegram forum topic to start a new Paperclip task."; + return hasConversation + ? "This chat conversation is closed. A later message here will continue the same Paperclip task." + : "No task is active. Send a message to start one."; +} diff --git a/server/src/services/chat-channels/telegram/messages.ts b/server/src/services/chat-channels/telegram/messages.ts index ac75997b145..c81115797d8 100644 --- a/server/src/services/chat-channels/telegram/messages.ts +++ b/server/src/services/chat-channels/telegram/messages.ts @@ -233,3 +233,10 @@ export function telegramMarkdownAttachment(text: string): Attachment { type: "file", }; } + +export function telegramIgnoresInboundMessage(message: { id: string; raw: unknown }): boolean { + return telegramZeroMessageId(message.id) || + (message.raw !== null && typeof message.raw === "object" && + (message.raw as { message_id?: unknown }).message_id === 0) || + isTelegramEditedMessageRaw(message.raw) || isTelegramMigrationRaw(message.raw); +}