refactor: share zalo send result handling

This commit is contained in:
Peter Steinberger 2026-03-13 21:42:19 +00:00
parent 158d970e2b
commit f5b9095108
1 changed files with 42 additions and 28 deletions

View File

@ -21,6 +21,28 @@ export type ZaloSendResult = {
error?: string;
};
function toZaloSendResult(response: {
ok?: boolean;
result?: { message_id?: string };
}): ZaloSendResult {
if (response.ok && response.result) {
return { ok: true, messageId: response.result.message_id };
}
return { ok: false, error: "Failed to send message" };
}
async function runZaloSend(
failureMessage: string,
send: () => Promise<{ ok?: boolean; result?: { message_id?: string } }>,
): Promise<ZaloSendResult> {
try {
const result = toZaloSendResult(await send());
return result.ok ? result : { ok: false, error: failureMessage };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
function resolveSendContext(options: ZaloSendOptions): {
token: string;
fetcher?: ZaloFetch;
@ -55,14 +77,21 @@ function resolveValidatedSendContext(
return { ok: true, chatId: trimmedChatId, token, fetcher };
}
function toInvalidContextResult(
context: ReturnType<typeof resolveValidatedSendContext>,
): ZaloSendResult | null {
return context.ok ? null : { ok: false, error: context.error };
}
export async function sendMessageZalo(
chatId: string,
text: string,
options: ZaloSendOptions = {},
): Promise<ZaloSendResult> {
const context = resolveValidatedSendContext(chatId, options);
if (!context.ok) {
return { ok: false, error: context.error };
const invalidResult = toInvalidContextResult(context);
if (invalidResult) {
return invalidResult;
}
if (options.mediaUrl) {
@ -73,24 +102,16 @@ export async function sendMessageZalo(
});
}
try {
const response = await sendMessage(
return await runZaloSend("Failed to send message", () =>
sendMessage(
context.token,
{
chat_id: context.chatId,
text: text.slice(0, 2000),
},
context.fetcher,
);
if (response.ok && response.result) {
return { ok: true, messageId: response.result.message_id };
}
return { ok: false, error: "Failed to send message" };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
),
);
}
export async function sendPhotoZalo(
@ -99,16 +120,17 @@ export async function sendPhotoZalo(
options: ZaloSendOptions = {},
): Promise<ZaloSendResult> {
const context = resolveValidatedSendContext(chatId, options);
if (!context.ok) {
return { ok: false, error: context.error };
const invalidResult = toInvalidContextResult(context);
if (invalidResult) {
return invalidResult;
}
if (!photoUrl?.trim()) {
return { ok: false, error: "No photo URL provided" };
}
try {
const response = await sendPhoto(
return await runZaloSend("Failed to send photo", () =>
sendPhoto(
context.token,
{
chat_id: context.chatId,
@ -116,14 +138,6 @@ export async function sendPhotoZalo(
caption: options.caption?.slice(0, 2000),
},
context.fetcher,
);
if (response.ok && response.result) {
return { ok: true, messageId: response.result.message_id };
}
return { ok: false, error: "Failed to send photo" };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
),
);
}