fix: align discord types
This commit is contained in:
@@ -501,6 +501,7 @@ const HeartbeatSchema = z
|
|||||||
z.literal("last"),
|
z.literal("last"),
|
||||||
z.literal("whatsapp"),
|
z.literal("whatsapp"),
|
||||||
z.literal("telegram"),
|
z.literal("telegram"),
|
||||||
|
z.literal("discord"),
|
||||||
z.literal("none"),
|
z.literal("none"),
|
||||||
])
|
])
|
||||||
.optional(),
|
.optional(),
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (isDirectMessage) {
|
if (isDirectMessage) {
|
||||||
const sessionCfg = cfg.inbound?.reply?.session;
|
const sessionCfg = cfg.session;
|
||||||
const mainKey = (sessionCfg?.mainKey ?? "main").trim() || "main";
|
const mainKey = (sessionCfg?.mainKey ?? "main").trim() || "main";
|
||||||
const storePath = resolveStorePath(sessionCfg?.store);
|
const storePath = resolveStorePath(sessionCfg?.store);
|
||||||
await updateLastRoute({
|
await updateLastRoute({
|
||||||
@@ -274,16 +274,12 @@ async function resolveMedia(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
const buffer = Buffer.from(await res.arrayBuffer());
|
const buffer = Buffer.from(await res.arrayBuffer());
|
||||||
const saved = await saveMediaBuffer(
|
const mime = await detectMime({
|
||||||
buffer,
|
buffer,
|
||||||
detectMime({
|
headerMime: attachment.contentType ?? res.headers.get("content-type"),
|
||||||
buffer,
|
filePath: attachment.name ?? attachment.url,
|
||||||
headerMime: attachment.contentType ?? res.headers.get("content-type"),
|
});
|
||||||
filePath: attachment.name ?? attachment.url,
|
const saved = await saveMediaBuffer(buffer, mime, "inbound", maxBytes);
|
||||||
}),
|
|
||||||
"inbound",
|
|
||||||
maxBytes,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
path: saved.path,
|
path: saved.path,
|
||||||
contentType: saved.contentType,
|
contentType: saved.contentType,
|
||||||
|
|||||||
@@ -1361,7 +1361,7 @@ export async function startGatewayServer(
|
|||||||
wakeMode: "now" | "next-heartbeat";
|
wakeMode: "now" | "next-heartbeat";
|
||||||
sessionKey: string;
|
sessionKey: string;
|
||||||
deliver: boolean;
|
deliver: boolean;
|
||||||
channel: "last" | "whatsapp" | "telegram";
|
channel: "last" | "whatsapp" | "telegram" | "discord";
|
||||||
to?: string;
|
to?: string;
|
||||||
thinking?: string;
|
thinking?: string;
|
||||||
timeoutSeconds?: number;
|
timeoutSeconds?: number;
|
||||||
@@ -1445,7 +1445,7 @@ export async function startGatewayServer(
|
|||||||
wakeMode: "now" | "next-heartbeat";
|
wakeMode: "now" | "next-heartbeat";
|
||||||
sessionKey: string;
|
sessionKey: string;
|
||||||
deliver: boolean;
|
deliver: boolean;
|
||||||
channel: "last" | "whatsapp" | "telegram";
|
channel: "last" | "whatsapp" | "telegram" | "discord";
|
||||||
to?: string;
|
to?: string;
|
||||||
thinking?: string;
|
thinking?: string;
|
||||||
timeoutSeconds?: number;
|
timeoutSeconds?: number;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
saveSessionStore,
|
saveSessionStore,
|
||||||
} from "../config/sessions.js";
|
} from "../config/sessions.js";
|
||||||
|
import { sendMessageDiscord } from "../discord/send.js";
|
||||||
import { formatErrorMessage } from "../infra/errors.js";
|
import { formatErrorMessage } from "../infra/errors.js";
|
||||||
import { createSubsystemLogger } from "../logging.js";
|
import { createSubsystemLogger } from "../logging.js";
|
||||||
import { getQueueSize } from "../process/command-queue.js";
|
import { getQueueSize } from "../process/command-queue.js";
|
||||||
@@ -28,10 +29,15 @@ import {
|
|||||||
setHeartbeatWakeHandler,
|
setHeartbeatWakeHandler,
|
||||||
} from "./heartbeat-wake.js";
|
} from "./heartbeat-wake.js";
|
||||||
|
|
||||||
export type HeartbeatTarget = "last" | "whatsapp" | "telegram" | "none";
|
export type HeartbeatTarget =
|
||||||
|
| "last"
|
||||||
|
| "whatsapp"
|
||||||
|
| "telegram"
|
||||||
|
| "discord"
|
||||||
|
| "none";
|
||||||
|
|
||||||
export type HeartbeatDeliveryTarget = {
|
export type HeartbeatDeliveryTarget = {
|
||||||
channel: "whatsapp" | "telegram" | "none";
|
channel: "whatsapp" | "telegram" | "discord" | "none";
|
||||||
to?: string;
|
to?: string;
|
||||||
reason?: string;
|
reason?: string;
|
||||||
};
|
};
|
||||||
@@ -40,6 +46,7 @@ type HeartbeatDeps = {
|
|||||||
runtime?: RuntimeEnv;
|
runtime?: RuntimeEnv;
|
||||||
sendWhatsApp?: typeof sendMessageWhatsApp;
|
sendWhatsApp?: typeof sendMessageWhatsApp;
|
||||||
sendTelegram?: typeof sendMessageTelegram;
|
sendTelegram?: typeof sendMessageTelegram;
|
||||||
|
sendDiscord?: typeof sendMessageDiscord;
|
||||||
getQueueSize?: (lane?: string) => number;
|
getQueueSize?: (lane?: string) => number;
|
||||||
nowMs?: () => number;
|
nowMs?: () => number;
|
||||||
};
|
};
|
||||||
@@ -126,6 +133,7 @@ export function resolveHeartbeatDeliveryTarget(params: {
|
|||||||
const target: HeartbeatTarget =
|
const target: HeartbeatTarget =
|
||||||
rawTarget === "whatsapp" ||
|
rawTarget === "whatsapp" ||
|
||||||
rawTarget === "telegram" ||
|
rawTarget === "telegram" ||
|
||||||
|
rawTarget === "discord" ||
|
||||||
rawTarget === "none" ||
|
rawTarget === "none" ||
|
||||||
rawTarget === "last"
|
rawTarget === "last"
|
||||||
? rawTarget
|
? rawTarget
|
||||||
@@ -146,10 +154,10 @@ export function resolveHeartbeatDeliveryTarget(params: {
|
|||||||
: undefined;
|
: undefined;
|
||||||
const lastTo = typeof entry?.lastTo === "string" ? entry.lastTo.trim() : "";
|
const lastTo = typeof entry?.lastTo === "string" ? entry.lastTo.trim() : "";
|
||||||
|
|
||||||
const channel: "whatsapp" | "telegram" | undefined =
|
const channel: "whatsapp" | "telegram" | "discord" | undefined =
|
||||||
target === "last"
|
target === "last"
|
||||||
? lastChannel
|
? lastChannel
|
||||||
: target === "whatsapp" || target === "telegram"
|
: target === "whatsapp" || target === "telegram" || target === "discord"
|
||||||
? target
|
? target
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
@@ -216,11 +224,13 @@ function normalizeHeartbeatReply(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function deliverHeartbeatReply(params: {
|
async function deliverHeartbeatReply(params: {
|
||||||
channel: "whatsapp" | "telegram";
|
channel: "whatsapp" | "telegram" | "discord";
|
||||||
to: string;
|
to: string;
|
||||||
text: string;
|
text: string;
|
||||||
mediaUrls: string[];
|
mediaUrls: string[];
|
||||||
deps: Required<Pick<HeartbeatDeps, "sendWhatsApp" | "sendTelegram">>;
|
deps: Required<
|
||||||
|
Pick<HeartbeatDeps, "sendWhatsApp" | "sendTelegram" | "sendDiscord">
|
||||||
|
>;
|
||||||
}) {
|
}) {
|
||||||
const { channel, to, text, mediaUrls, deps } = params;
|
const { channel, to, text, mediaUrls, deps } = params;
|
||||||
if (channel === "whatsapp") {
|
if (channel === "whatsapp") {
|
||||||
@@ -239,17 +249,31 @@ async function deliverHeartbeatReply(params: {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mediaUrls.length === 0) {
|
if (channel === "telegram") {
|
||||||
for (const chunk of chunkText(text, 4000)) {
|
if (mediaUrls.length === 0) {
|
||||||
await deps.sendTelegram(to, chunk, { verbose: false });
|
for (const chunk of chunkText(text, 4000)) {
|
||||||
|
await deps.sendTelegram(to, chunk, { verbose: false });
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
let first = true;
|
||||||
|
for (const url of mediaUrls) {
|
||||||
|
const caption = first ? text : "";
|
||||||
|
first = false;
|
||||||
|
await deps.sendTelegram(to, caption, { verbose: false, mediaUrl: url });
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mediaUrls.length === 0) {
|
||||||
|
await deps.sendDiscord(to, text, { verbose: false });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let first = true;
|
let first = true;
|
||||||
for (const url of mediaUrls) {
|
for (const url of mediaUrls) {
|
||||||
const caption = first ? text : "";
|
const caption = first ? text : "";
|
||||||
first = false;
|
first = false;
|
||||||
await deps.sendTelegram(to, caption, { verbose: false, mediaUrl: url });
|
await deps.sendDiscord(to, caption, { verbose: false, mediaUrl: url });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,6 +378,7 @@ export async function runHeartbeatOnce(opts: {
|
|||||||
const deps = {
|
const deps = {
|
||||||
sendWhatsApp: opts.deps?.sendWhatsApp ?? sendMessageWhatsApp,
|
sendWhatsApp: opts.deps?.sendWhatsApp ?? sendMessageWhatsApp,
|
||||||
sendTelegram: opts.deps?.sendTelegram ?? sendMessageTelegram,
|
sendTelegram: opts.deps?.sendTelegram ?? sendMessageTelegram,
|
||||||
|
sendDiscord: opts.deps?.sendDiscord ?? sendMessageDiscord,
|
||||||
};
|
};
|
||||||
await deliverHeartbeatReply({
|
await deliverHeartbeatReply({
|
||||||
channel: delivery.channel,
|
channel: delivery.channel,
|
||||||
|
|||||||
Reference in New Issue
Block a user