refactor: centralize telegram send param parsing
This commit is contained in:
@@ -10,6 +10,29 @@ import type { ChannelMessageActionAdapter, ChannelMessageActionName } from "../t
|
|||||||
|
|
||||||
const providerId = "telegram";
|
const providerId = "telegram";
|
||||||
|
|
||||||
|
function readTelegramSendParams(params: Record<string, unknown>) {
|
||||||
|
const to = readStringParam(params, "to", { required: true });
|
||||||
|
const mediaUrl = readStringParam(params, "media", { trim: false });
|
||||||
|
const content =
|
||||||
|
readStringParam(params, "message", {
|
||||||
|
required: !mediaUrl,
|
||||||
|
allowEmpty: true,
|
||||||
|
}) ?? "";
|
||||||
|
const replyTo = readStringParam(params, "replyTo");
|
||||||
|
const threadId = readStringParam(params, "threadId");
|
||||||
|
const buttons = params.buttons;
|
||||||
|
const asVoice = typeof params.asVoice === "boolean" ? params.asVoice : undefined;
|
||||||
|
return {
|
||||||
|
to,
|
||||||
|
content,
|
||||||
|
mediaUrl: mediaUrl ?? undefined,
|
||||||
|
replyToMessageId: replyTo ?? undefined,
|
||||||
|
messageThreadId: threadId ?? undefined,
|
||||||
|
buttons,
|
||||||
|
asVoice,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export const telegramMessageActions: ChannelMessageActionAdapter = {
|
export const telegramMessageActions: ChannelMessageActionAdapter = {
|
||||||
listActions: ({ cfg }) => {
|
listActions: ({ cfg }) => {
|
||||||
const accounts = listEnabledTelegramAccounts(cfg).filter(
|
const accounts = listEnabledTelegramAccounts(cfg).filter(
|
||||||
@@ -41,28 +64,12 @@ export const telegramMessageActions: ChannelMessageActionAdapter = {
|
|||||||
},
|
},
|
||||||
handleAction: async ({ action, params, cfg, accountId }) => {
|
handleAction: async ({ action, params, cfg, accountId }) => {
|
||||||
if (action === "send") {
|
if (action === "send") {
|
||||||
const to = readStringParam(params, "to", { required: true });
|
const sendParams = readTelegramSendParams(params);
|
||||||
const mediaUrl = readStringParam(params, "media", { trim: false });
|
|
||||||
const content =
|
|
||||||
readStringParam(params, "message", {
|
|
||||||
required: !mediaUrl,
|
|
||||||
allowEmpty: true,
|
|
||||||
}) ?? "";
|
|
||||||
const replyTo = readStringParam(params, "replyTo");
|
|
||||||
const threadId = readStringParam(params, "threadId");
|
|
||||||
const buttons = params.buttons;
|
|
||||||
const asVoice = typeof params.asVoice === "boolean" ? params.asVoice : undefined;
|
|
||||||
return await handleTelegramAction(
|
return await handleTelegramAction(
|
||||||
{
|
{
|
||||||
action: "sendMessage",
|
action: "sendMessage",
|
||||||
to,
|
...sendParams,
|
||||||
content,
|
|
||||||
mediaUrl: mediaUrl ?? undefined,
|
|
||||||
replyToMessageId: replyTo ?? undefined,
|
|
||||||
messageThreadId: threadId ?? undefined,
|
|
||||||
accountId: accountId ?? undefined,
|
accountId: accountId ?? undefined,
|
||||||
buttons,
|
|
||||||
asVoice,
|
|
||||||
},
|
},
|
||||||
cfg,
|
cfg,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user