mirror of https://github.com/openclaw/openclaw.git
refactor: share telegram outbound send options
This commit is contained in:
parent
868fd32ee7
commit
a57c590a71
|
|
@ -78,6 +78,61 @@ function formatDuplicateTelegramTokenReason(params: {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TelegramSendFn = ReturnType<
|
||||||
|
typeof getTelegramRuntime
|
||||||
|
>["channel"]["telegram"]["sendMessageTelegram"];
|
||||||
|
type TelegramSendOptions = NonNullable<Parameters<TelegramSendFn>[2]>;
|
||||||
|
|
||||||
|
function buildTelegramSendOptions(params: {
|
||||||
|
cfg: OpenClawConfig;
|
||||||
|
mediaUrl?: string;
|
||||||
|
mediaLocalRoots?: readonly string[];
|
||||||
|
accountId?: string;
|
||||||
|
replyToId?: string;
|
||||||
|
threadId?: string;
|
||||||
|
silent?: boolean;
|
||||||
|
}): TelegramSendOptions {
|
||||||
|
return {
|
||||||
|
verbose: false,
|
||||||
|
cfg: params.cfg,
|
||||||
|
...(params.mediaUrl ? { mediaUrl: params.mediaUrl } : {}),
|
||||||
|
...(params.mediaLocalRoots?.length ? { mediaLocalRoots: params.mediaLocalRoots } : {}),
|
||||||
|
messageThreadId: parseTelegramThreadId(params.threadId),
|
||||||
|
replyToMessageId: parseTelegramReplyToMessageId(params.replyToId),
|
||||||
|
accountId: params.accountId ?? undefined,
|
||||||
|
silent: params.silent ?? undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendTelegramOutbound(params: {
|
||||||
|
cfg: OpenClawConfig;
|
||||||
|
to: string;
|
||||||
|
text: string;
|
||||||
|
mediaUrl?: string;
|
||||||
|
mediaLocalRoots?: readonly string[];
|
||||||
|
accountId?: string;
|
||||||
|
deps?: { sendTelegram?: TelegramSendFn };
|
||||||
|
replyToId?: string;
|
||||||
|
threadId?: string;
|
||||||
|
silent?: boolean;
|
||||||
|
}) {
|
||||||
|
const send =
|
||||||
|
params.deps?.sendTelegram ?? getTelegramRuntime().channel.telegram.sendMessageTelegram;
|
||||||
|
return await send(
|
||||||
|
params.to,
|
||||||
|
params.text,
|
||||||
|
buildTelegramSendOptions({
|
||||||
|
cfg: params.cfg,
|
||||||
|
mediaUrl: params.mediaUrl,
|
||||||
|
mediaLocalRoots: params.mediaLocalRoots,
|
||||||
|
accountId: params.accountId,
|
||||||
|
replyToId: params.replyToId,
|
||||||
|
threadId: params.threadId,
|
||||||
|
silent: params.silent,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const telegramMessageActions: ChannelMessageActionAdapter = {
|
const telegramMessageActions: ChannelMessageActionAdapter = {
|
||||||
listActions: (ctx) =>
|
listActions: (ctx) =>
|
||||||
getTelegramRuntime().channel.telegram.messageActions?.listActions?.(ctx) ?? [],
|
getTelegramRuntime().channel.telegram.messageActions?.listActions?.(ctx) ?? [],
|
||||||
|
|
@ -327,35 +382,31 @@ export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount, TelegramProb
|
||||||
silent,
|
silent,
|
||||||
}) => {
|
}) => {
|
||||||
const send = deps?.sendTelegram ?? getTelegramRuntime().channel.telegram.sendMessageTelegram;
|
const send = deps?.sendTelegram ?? getTelegramRuntime().channel.telegram.sendMessageTelegram;
|
||||||
const replyToMessageId = parseTelegramReplyToMessageId(replyToId);
|
|
||||||
const messageThreadId = parseTelegramThreadId(threadId);
|
|
||||||
const result = await sendTelegramPayloadMessages({
|
const result = await sendTelegramPayloadMessages({
|
||||||
send,
|
send,
|
||||||
to,
|
to,
|
||||||
payload,
|
payload,
|
||||||
baseOpts: {
|
baseOpts: buildTelegramSendOptions({
|
||||||
verbose: false,
|
|
||||||
cfg,
|
cfg,
|
||||||
mediaLocalRoots,
|
mediaLocalRoots,
|
||||||
messageThreadId,
|
accountId,
|
||||||
replyToMessageId,
|
replyToId,
|
||||||
accountId: accountId ?? undefined,
|
threadId,
|
||||||
silent: silent ?? undefined,
|
silent,
|
||||||
},
|
}),
|
||||||
});
|
});
|
||||||
return { channel: "telegram", ...result };
|
return { channel: "telegram", ...result };
|
||||||
},
|
},
|
||||||
sendText: async ({ cfg, to, text, accountId, deps, replyToId, threadId, silent }) => {
|
sendText: async ({ cfg, to, text, accountId, deps, replyToId, threadId, silent }) => {
|
||||||
const send = deps?.sendTelegram ?? getTelegramRuntime().channel.telegram.sendMessageTelegram;
|
const result = await sendTelegramOutbound({
|
||||||
const replyToMessageId = parseTelegramReplyToMessageId(replyToId);
|
|
||||||
const messageThreadId = parseTelegramThreadId(threadId);
|
|
||||||
const result = await send(to, text, {
|
|
||||||
verbose: false,
|
|
||||||
cfg,
|
cfg,
|
||||||
messageThreadId,
|
to,
|
||||||
replyToMessageId,
|
text,
|
||||||
accountId: accountId ?? undefined,
|
accountId,
|
||||||
silent: silent ?? undefined,
|
deps,
|
||||||
|
replyToId,
|
||||||
|
threadId,
|
||||||
|
silent,
|
||||||
});
|
});
|
||||||
return { channel: "telegram", ...result };
|
return { channel: "telegram", ...result };
|
||||||
},
|
},
|
||||||
|
|
@ -371,18 +422,17 @@ export const telegramPlugin: ChannelPlugin<ResolvedTelegramAccount, TelegramProb
|
||||||
threadId,
|
threadId,
|
||||||
silent,
|
silent,
|
||||||
}) => {
|
}) => {
|
||||||
const send = deps?.sendTelegram ?? getTelegramRuntime().channel.telegram.sendMessageTelegram;
|
const result = await sendTelegramOutbound({
|
||||||
const replyToMessageId = parseTelegramReplyToMessageId(replyToId);
|
|
||||||
const messageThreadId = parseTelegramThreadId(threadId);
|
|
||||||
const result = await send(to, text, {
|
|
||||||
verbose: false,
|
|
||||||
cfg,
|
cfg,
|
||||||
|
to,
|
||||||
|
text,
|
||||||
mediaUrl,
|
mediaUrl,
|
||||||
mediaLocalRoots,
|
mediaLocalRoots,
|
||||||
messageThreadId,
|
accountId,
|
||||||
replyToMessageId,
|
deps,
|
||||||
accountId: accountId ?? undefined,
|
replyToId,
|
||||||
silent: silent ?? undefined,
|
threadId,
|
||||||
|
silent,
|
||||||
});
|
});
|
||||||
return { channel: "telegram", ...result };
|
return { channel: "telegram", ...result };
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue