refactor(outbound): reuse message gateway call

This commit is contained in:
Peter Steinberger
2026-02-16 00:56:21 +00:00
parent 14fb2c05b1
commit 4ab25a2889

View File

@@ -124,6 +124,24 @@ function resolveGatewayOptions(opts?: MessageGatewayOptions) {
}; };
} }
async function callMessageGateway<T>(params: {
gateway?: MessageGatewayOptions;
method: string;
params: Record<string, unknown>;
}): Promise<T> {
const gateway = resolveGatewayOptions(params.gateway);
return await callGateway<T>({
url: gateway.url,
token: gateway.token,
method: params.method,
params: params.params,
timeoutMs: gateway.timeoutMs,
clientName: gateway.clientName,
clientDisplayName: gateway.clientDisplayName,
mode: gateway.mode,
});
}
export async function sendMessage(params: MessageSendParams): Promise<MessageSendResult> { export async function sendMessage(params: MessageSendParams): Promise<MessageSendResult> {
const cfg = params.cfg ?? loadConfig(); const cfg = params.cfg ?? loadConfig();
const channel = params.channel?.trim() const channel = params.channel?.trim()
@@ -210,10 +228,8 @@ export async function sendMessage(params: MessageSendParams): Promise<MessageSen
}; };
} }
const gateway = resolveGatewayOptions(params.gateway); const result = await callMessageGateway<{ messageId: string }>({
const result = await callGateway<{ messageId: string }>({ gateway: params.gateway,
url: gateway.url,
token: gateway.token,
method: "send", method: "send",
params: { params: {
to: params.to, to: params.to,
@@ -226,10 +242,6 @@ export async function sendMessage(params: MessageSendParams): Promise<MessageSen
sessionKey: params.mirror?.sessionKey, sessionKey: params.mirror?.sessionKey,
idempotencyKey: params.idempotencyKey ?? randomIdempotencyKey(), idempotencyKey: params.idempotencyKey ?? randomIdempotencyKey(),
}, },
timeoutMs: gateway.timeoutMs,
clientName: gateway.clientName,
clientDisplayName: gateway.clientDisplayName,
mode: gateway.mode,
}); });
return { return {
@@ -281,16 +293,14 @@ export async function sendPoll(params: MessagePollParams): Promise<MessagePollRe
}; };
} }
const gateway = resolveGatewayOptions(params.gateway); const result = await callMessageGateway<{
const result = await callGateway<{
messageId: string; messageId: string;
toJid?: string; toJid?: string;
channelId?: string; channelId?: string;
conversationId?: string; conversationId?: string;
pollId?: string; pollId?: string;
}>({ }>({
url: gateway.url, gateway: params.gateway,
token: gateway.token,
method: "poll", method: "poll",
params: { params: {
to: params.to, to: params.to,
@@ -306,10 +316,6 @@ export async function sendPoll(params: MessagePollParams): Promise<MessagePollRe
accountId: params.accountId, accountId: params.accountId,
idempotencyKey: params.idempotencyKey ?? randomIdempotencyKey(), idempotencyKey: params.idempotencyKey ?? randomIdempotencyKey(),
}, },
timeoutMs: gateway.timeoutMs,
clientName: gateway.clientName,
clientDisplayName: gateway.clientDisplayName,
mode: gateway.mode,
}); });
return { return {