2026-01-13 06:16:43 +00:00
|
|
|
import { getChannelPlugin } from "../../channels/plugins/index.js";
|
|
|
|
|
import type { ChannelId } from "../../channels/plugins/types.js";
|
2026-01-07 01:26:09 +00:00
|
|
|
import type { OutboundDeliveryResult } from "./deliver.js";
|
|
|
|
|
|
|
|
|
|
export type OutboundDeliveryJson = {
|
2026-01-13 06:16:43 +00:00
|
|
|
channel: string;
|
2026-01-07 01:26:09 +00:00
|
|
|
via: "direct" | "gateway";
|
|
|
|
|
to: string;
|
|
|
|
|
messageId: string;
|
|
|
|
|
mediaUrl: string | null;
|
|
|
|
|
chatId?: string;
|
|
|
|
|
channelId?: string;
|
2026-01-09 09:56:36 +01:00
|
|
|
conversationId?: string;
|
2026-01-07 01:26:09 +00:00
|
|
|
timestamp?: number;
|
|
|
|
|
toJid?: string;
|
2026-01-11 11:45:25 +00:00
|
|
|
meta?: Record<string, unknown>;
|
2026-01-07 01:26:09 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-07 01:43:02 +00:00
|
|
|
type OutboundDeliveryMeta = {
|
|
|
|
|
messageId?: string;
|
|
|
|
|
chatId?: string;
|
|
|
|
|
channelId?: string;
|
2026-01-09 09:56:36 +01:00
|
|
|
conversationId?: string;
|
2026-01-07 01:43:02 +00:00
|
|
|
timestamp?: number;
|
|
|
|
|
toJid?: string;
|
2026-01-11 11:45:25 +00:00
|
|
|
meta?: Record<string, unknown>;
|
2026-01-07 01:43:02 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-13 06:16:43 +00:00
|
|
|
const resolveChannelLabel = (channel: string) =>
|
|
|
|
|
getChannelPlugin(channel as ChannelId)?.meta.label ?? channel;
|
2026-01-07 01:26:09 +00:00
|
|
|
|
|
|
|
|
export function formatOutboundDeliverySummary(
|
2026-01-13 06:16:43 +00:00
|
|
|
channel: string,
|
2026-01-07 01:26:09 +00:00
|
|
|
result?: OutboundDeliveryResult,
|
|
|
|
|
): string {
|
|
|
|
|
if (!result) {
|
2026-01-13 06:16:43 +00:00
|
|
|
return `✅ Sent via ${resolveChannelLabel(channel)}. Message ID: unknown`;
|
2026-01-07 01:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-13 06:16:43 +00:00
|
|
|
const label = resolveChannelLabel(result.channel);
|
2026-01-07 01:26:09 +00:00
|
|
|
const base = `✅ Sent via ${label}. Message ID: ${result.messageId}`;
|
|
|
|
|
|
|
|
|
|
if ("chatId" in result) return `${base} (chat ${result.chatId})`;
|
|
|
|
|
if ("channelId" in result) return `${base} (channel ${result.channelId})`;
|
2026-01-14 14:31:43 +00:00
|
|
|
if ("conversationId" in result) return `${base} (conversation ${result.conversationId})`;
|
2026-01-07 01:26:09 +00:00
|
|
|
return base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function buildOutboundDeliveryJson(params: {
|
2026-01-13 06:16:43 +00:00
|
|
|
channel: string;
|
2026-01-07 01:26:09 +00:00
|
|
|
to: string;
|
2026-01-07 01:43:02 +00:00
|
|
|
result?: OutboundDeliveryMeta | OutboundDeliveryResult;
|
2026-01-07 01:26:09 +00:00
|
|
|
via?: "direct" | "gateway";
|
|
|
|
|
mediaUrl?: string | null;
|
|
|
|
|
}): OutboundDeliveryJson {
|
2026-01-13 06:16:43 +00:00
|
|
|
const { channel, to, result } = params;
|
2026-01-07 01:26:09 +00:00
|
|
|
const messageId = result?.messageId ?? "unknown";
|
|
|
|
|
const payload: OutboundDeliveryJson = {
|
2026-01-13 06:16:43 +00:00
|
|
|
channel,
|
2026-01-07 01:26:09 +00:00
|
|
|
via: params.via ?? "direct",
|
|
|
|
|
to,
|
|
|
|
|
messageId,
|
|
|
|
|
mediaUrl: params.mediaUrl ?? null,
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-07 02:19:09 +00:00
|
|
|
if (result && "chatId" in result && result.chatId !== undefined) {
|
|
|
|
|
payload.chatId = result.chatId;
|
|
|
|
|
}
|
|
|
|
|
if (result && "channelId" in result && result.channelId !== undefined) {
|
|
|
|
|
payload.channelId = result.channelId;
|
|
|
|
|
}
|
2026-01-14 14:31:43 +00:00
|
|
|
if (result && "conversationId" in result && result.conversationId !== undefined) {
|
2026-01-09 09:56:36 +01:00
|
|
|
payload.conversationId = result.conversationId;
|
|
|
|
|
}
|
2026-01-07 02:19:09 +00:00
|
|
|
if (result && "timestamp" in result && result.timestamp !== undefined) {
|
|
|
|
|
payload.timestamp = result.timestamp;
|
|
|
|
|
}
|
|
|
|
|
if (result && "toJid" in result && result.toJid !== undefined) {
|
|
|
|
|
payload.toJid = result.toJid;
|
|
|
|
|
}
|
2026-01-11 11:45:25 +00:00
|
|
|
if (result && "meta" in result && result.meta !== undefined) {
|
|
|
|
|
payload.meta = result.meta;
|
|
|
|
|
}
|
2026-01-07 01:26:09 +00:00
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
|
}
|
2026-01-07 01:43:02 +00:00
|
|
|
|
|
|
|
|
export function formatGatewaySummary(params: {
|
|
|
|
|
action?: string;
|
2026-01-13 06:16:43 +00:00
|
|
|
channel?: string;
|
2026-01-07 01:43:02 +00:00
|
|
|
messageId?: string | null;
|
|
|
|
|
}): string {
|
|
|
|
|
const action = params.action ?? "Sent";
|
2026-01-13 06:16:43 +00:00
|
|
|
const channelSuffix = params.channel ? ` (${params.channel})` : "";
|
2026-01-07 01:43:02 +00:00
|
|
|
const messageId = params.messageId ?? "unknown";
|
2026-01-13 06:16:43 +00:00
|
|
|
return `✅ ${action} via gateway${channelSuffix}. Message ID: ${messageId}`;
|
2026-01-07 01:43:02 +00:00
|
|
|
}
|