mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 09:48:39 +00:00
refactor: share zalo send context validation
This commit is contained in:
@@ -77,15 +77,30 @@ function resolveValidatedSendContext(
|
|||||||
return { ok: true, chatId: trimmedChatId, token, fetcher };
|
return { ok: true, chatId: trimmedChatId, token, fetcher };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveSendContextOrFailure(
|
||||||
|
chatId: string,
|
||||||
|
options: ZaloSendOptions,
|
||||||
|
):
|
||||||
|
| { context: { chatId: string; token: string; fetcher?: ZaloFetch } }
|
||||||
|
| { failure: ZaloSendResult } {
|
||||||
|
const context = resolveValidatedSendContext(chatId, options);
|
||||||
|
return context.ok
|
||||||
|
? { context }
|
||||||
|
: {
|
||||||
|
failure: { 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 resolved = resolveSendContextOrFailure(chatId, options);
|
||||||
if (!context.ok) {
|
if ("failure" in resolved) {
|
||||||
return { ok: false, error: context.error };
|
return resolved.failure;
|
||||||
}
|
}
|
||||||
|
const { context } = resolved;
|
||||||
|
|
||||||
if (options.mediaUrl) {
|
if (options.mediaUrl) {
|
||||||
return sendPhotoZalo(context.chatId, options.mediaUrl, {
|
return sendPhotoZalo(context.chatId, options.mediaUrl, {
|
||||||
@@ -112,10 +127,11 @@ export async function sendPhotoZalo(
|
|||||||
photoUrl: string,
|
photoUrl: string,
|
||||||
options: ZaloSendOptions = {},
|
options: ZaloSendOptions = {},
|
||||||
): Promise<ZaloSendResult> {
|
): Promise<ZaloSendResult> {
|
||||||
const context = resolveValidatedSendContext(chatId, options);
|
const resolved = resolveSendContextOrFailure(chatId, options);
|
||||||
if (!context.ok) {
|
if ("failure" in resolved) {
|
||||||
return { ok: false, error: context.error };
|
return resolved.failure;
|
||||||
}
|
}
|
||||||
|
const { context } = resolved;
|
||||||
|
|
||||||
if (!photoUrl?.trim()) {
|
if (!photoUrl?.trim()) {
|
||||||
return { ok: false, error: "No photo URL provided" };
|
return { ok: false, error: "No photo URL provided" };
|
||||||
|
|||||||
Reference in New Issue
Block a user