mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 12:18:37 +00:00
refactor: share zalo send result handling
This commit is contained in:
@@ -21,6 +21,28 @@ export type ZaloSendResult = {
|
|||||||
error?: string;
|
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): {
|
function resolveSendContext(options: ZaloSendOptions): {
|
||||||
token: string;
|
token: string;
|
||||||
fetcher?: ZaloFetch;
|
fetcher?: ZaloFetch;
|
||||||
@@ -55,14 +77,21 @@ function resolveValidatedSendContext(
|
|||||||
return { ok: true, chatId: trimmedChatId, token, fetcher };
|
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(
|
export async function sendMessageZalo(
|
||||||
chatId: string,
|
chatId: string,
|
||||||
text: string,
|
text: string,
|
||||||
options: ZaloSendOptions = {},
|
options: ZaloSendOptions = {},
|
||||||
): Promise<ZaloSendResult> {
|
): Promise<ZaloSendResult> {
|
||||||
const context = resolveValidatedSendContext(chatId, options);
|
const context = resolveValidatedSendContext(chatId, options);
|
||||||
if (!context.ok) {
|
const invalidResult = toInvalidContextResult(context);
|
||||||
return { ok: false, error: context.error };
|
if (invalidResult) {
|
||||||
|
return invalidResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.mediaUrl) {
|
if (options.mediaUrl) {
|
||||||
@@ -73,24 +102,16 @@ export async function sendMessageZalo(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return await runZaloSend("Failed to send message", () =>
|
||||||
const response = await sendMessage(
|
sendMessage(
|
||||||
context.token,
|
context.token,
|
||||||
{
|
{
|
||||||
chat_id: context.chatId,
|
chat_id: context.chatId,
|
||||||
text: text.slice(0, 2000),
|
text: text.slice(0, 2000),
|
||||||
},
|
},
|
||||||
context.fetcher,
|
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(
|
export async function sendPhotoZalo(
|
||||||
@@ -99,16 +120,17 @@ export async function sendPhotoZalo(
|
|||||||
options: ZaloSendOptions = {},
|
options: ZaloSendOptions = {},
|
||||||
): Promise<ZaloSendResult> {
|
): Promise<ZaloSendResult> {
|
||||||
const context = resolveValidatedSendContext(chatId, options);
|
const context = resolveValidatedSendContext(chatId, options);
|
||||||
if (!context.ok) {
|
const invalidResult = toInvalidContextResult(context);
|
||||||
return { ok: false, error: context.error };
|
if (invalidResult) {
|
||||||
|
return invalidResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!photoUrl?.trim()) {
|
if (!photoUrl?.trim()) {
|
||||||
return { ok: false, error: "No photo URL provided" };
|
return { ok: false, error: "No photo URL provided" };
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return await runZaloSend("Failed to send photo", () =>
|
||||||
const response = await sendPhoto(
|
sendPhoto(
|
||||||
context.token,
|
context.token,
|
||||||
{
|
{
|
||||||
chat_id: context.chatId,
|
chat_id: context.chatId,
|
||||||
@@ -116,14 +138,6 @@ export async function sendPhotoZalo(
|
|||||||
caption: options.caption?.slice(0, 2000),
|
caption: options.caption?.slice(0, 2000),
|
||||||
},
|
},
|
||||||
context.fetcher,
|
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) };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user