Skip to content

Commit 38250ac

Browse files
committed
refactor(telegram): consolidate outbound contracts and targets
1 parent 8f2850d commit 38250ac

7 files changed

Lines changed: 66 additions & 193 deletions

File tree

‎extensions/telegram/src/bot/body-helpers.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ type TelegramTextMessage = Pick<
101101
"text" | "caption" | "entities" | "caption_entities" | "poll"
102102
> & { rich_message?: unknown };
103103

104-
function hasTelegramRichMessage(value: unknown): boolean {
105-
return typeof value === "object" && value !== null && !Array.isArray(value);
106-
}
107-
108104
function compactRichText(value: string): string {
109105
return value
110106
.split("\n")
@@ -187,11 +183,11 @@ function renderRichBlocks(value: unknown): string {
187183
export function resolveTelegramRichMessagePlaceholder(
188184
msg: TelegramTextMessage,
189185
): string | undefined {
190-
return hasTelegramRichMessage(msg.rich_message) ? TELEGRAM_RICH_MESSAGE_PLACEHOLDER : undefined;
186+
return isRecord(msg.rich_message) ? TELEGRAM_RICH_MESSAGE_PLACEHOLDER : undefined;
191187
}
192188

193189
export function resolveTelegramRichMessageText(msg: TelegramTextMessage): string | undefined {
194-
if (!hasTelegramRichMessage(msg.rich_message)) {
190+
if (!isRecord(msg.rich_message)) {
195191
return undefined;
196192
}
197193
return compactRichText(renderRichBlocks(msg.rich_message)) || undefined;

‎extensions/telegram/src/send-actions.ts‎

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { ReactionType, ReactionTypeEmoji } from "grammy/types";
2-
import type { RetryConfig } from "openclaw/plugin-sdk/retry-runtime";
32
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
43
import { formatErrorMessage } from "openclaw/plugin-sdk/ssrf-runtime";
54
import { buildTypingThreadParams } from "./bot/helpers.js";
@@ -12,32 +11,21 @@ import {
1211
withTelegramApiContextLease,
1312
type TelegramApi,
1413
type TelegramApiContext,
15-
type TelegramApiOverride,
1614
} from "./send-context.js";
15+
import type {
16+
TelegramApiCallOpts,
17+
TelegramMessageActionOpts,
18+
TelegramSendOpts,
19+
} from "./send-message-types.js";
1720
import { prepareTelegramOutbound } from "./send-outbound.js";
18-
import type { OpenClawConfig } from "./send.runtime.js";
1921
import { parseTelegramTarget } from "./targets.js";
2022

21-
type TelegramReactionOpts = {
22-
cfg: OpenClawConfig;
23-
token?: string;
24-
accountId?: string;
25-
api?: TelegramApiOverride;
23+
type TelegramReactionOpts = TelegramApiCallOpts & {
2624
remove?: boolean;
27-
verbose?: boolean;
28-
retry?: RetryConfig;
29-
gatewayClientScopes?: readonly string[];
3025
};
3126

32-
type TelegramTypingOpts = {
33-
cfg: OpenClawConfig;
34-
token?: string;
35-
accountId?: string;
36-
verbose?: boolean;
37-
api?: TelegramApiOverride;
38-
retry?: RetryConfig;
39-
messageThreadId?: number;
40-
};
27+
type TelegramTypingOpts = Omit<TelegramApiCallOpts, "gatewayClientScopes"> &
28+
Pick<TelegramSendOpts, "messageThreadId">;
4129

4230
export async function sendTypingTelegram(
4331
to: string,
@@ -135,21 +123,10 @@ async function reactMessageTelegramWithContext(
135123
return { ok: true };
136124
}
137125

138-
type TelegramDeleteOpts = {
139-
cfg: OpenClawConfig;
140-
token?: string;
141-
accountId?: string;
142-
notify?: boolean;
143-
verbose?: boolean;
144-
api?: TelegramApiOverride;
145-
retry?: RetryConfig;
146-
gatewayClientScopes?: readonly string[];
147-
};
148-
149126
export async function deleteMessageTelegram(
150127
chatIdInput: string | number,
151128
messageIdInput: string | number,
152-
opts: TelegramDeleteOpts,
129+
opts: TelegramMessageActionOpts,
153130
): Promise<{ ok: true } | { ok: false; warning: string }> {
154131
const context = resolveTelegramApiContext(opts);
155132
return withTelegramApiContextLease(
@@ -161,7 +138,7 @@ export async function deleteMessageTelegram(
161138
async function deleteMessageTelegramWithContext(
162139
chatIdInput: string | number,
163140
messageIdInput: string | number,
164-
opts: TelegramDeleteOpts,
141+
opts: TelegramMessageActionOpts,
165142
context: TelegramApiContext,
166143
): Promise<{ ok: true } | { ok: false; warning: string }> {
167144
const { api } = context;
@@ -197,7 +174,7 @@ async function deleteMessageTelegramWithContext(
197174
export async function pinMessageTelegram(
198175
chatIdInput: string | number,
199176
messageIdInput: string | number,
200-
opts: TelegramDeleteOpts,
177+
opts: TelegramMessageActionOpts,
201178
): Promise<{ ok: true; messageId: string; chatId: string }> {
202179
const context = resolveTelegramApiContext(opts);
203180
return withTelegramApiContextLease(
@@ -209,7 +186,7 @@ export async function pinMessageTelegram(
209186
async function pinMessageTelegramWithContext(
210187
chatIdInput: string | number,
211188
messageIdInput: string | number,
212-
opts: TelegramDeleteOpts,
189+
opts: TelegramMessageActionOpts,
213190
context: TelegramApiContext,
214191
): Promise<{ ok: true; messageId: string; chatId: string }> {
215192
const { api } = context;
@@ -234,7 +211,7 @@ async function pinMessageTelegramWithContext(
234211
export async function unpinMessageTelegram(
235212
chatIdInput: string | number,
236213
messageIdInput: string | number | undefined,
237-
opts: TelegramDeleteOpts,
214+
opts: TelegramMessageActionOpts,
238215
): Promise<{ ok: true; chatId: string; messageId?: string }> {
239216
const context = resolveTelegramApiContext(opts);
240217
return withTelegramApiContextLease(
@@ -246,7 +223,7 @@ export async function unpinMessageTelegram(
246223
async function unpinMessageTelegramWithContext(
247224
chatIdInput: string | number,
248225
messageIdInput: string | number | undefined,
249-
opts: TelegramDeleteOpts,
226+
opts: TelegramMessageActionOpts,
250227
context: TelegramApiContext,
251228
): Promise<{ ok: true; chatId: string; messageId?: string }> {
252229
const { api } = context;

‎extensions/telegram/src/send-edit.ts‎

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { RetryConfig } from "openclaw/plugin-sdk/retry-runtime";
21
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
32
import type { TelegramInlineButtons } from "./button-types.js";
43
import { renderTelegramHtmlText, telegramHtmlToPlainTextFallback } from "./format.js";
@@ -25,10 +24,9 @@ import {
2524
withTelegramApiContextLease,
2625
withTelegramHtmlParseFallback,
2726
type TelegramApiContext,
28-
type TelegramApiOverride,
2927
} from "./send-context.js";
28+
import type { TelegramApiCallOpts, TelegramSendOpts } from "./send-message-types.js";
3029
import { prepareTelegramOutbound } from "./send-outbound.js";
31-
import type { OpenClawConfig } from "./send.runtime.js";
3230
import { resolveMarkdownTableMode } from "./send.runtime.js";
3331
import { resolveTelegramBotUserIdFromToken } from "./token.js";
3432

@@ -37,36 +35,15 @@ type TelegramEditMessageCaptionParams = Parameters<
3735
TelegramApiContext["api"]["editMessageCaption"]
3836
>[2];
3937

40-
type TelegramEditOpts = {
41-
token?: string;
42-
accountId?: string;
43-
verbose?: boolean;
44-
api?: TelegramApiOverride;
45-
retry?: RetryConfig;
46-
gatewayClientScopes?: readonly string[];
47-
textMode?: "markdown" | "html";
48-
/** Controls whether link previews are shown in the edited message. */
49-
linkPreview?: boolean;
50-
/** Inline keyboard buttons (reply markup). Pass empty array to remove buttons. */
51-
buttons?: TelegramInlineButtons;
52-
/** Use Telegram's media-caption edit endpoint, or fall back to it when text edits target media. */
53-
editMode?: "text" | "caption" | "auto";
54-
/** Resolved runtime config from the command or gateway boundary. */
55-
cfg: OpenClawConfig;
56-
};
38+
type TelegramEditReplyMarkupOpts = TelegramApiCallOpts & Pick<TelegramSendOpts, "buttons">;
5739

58-
type TelegramEditReplyMarkupOpts = {
59-
token?: string;
60-
accountId?: string;
61-
verbose?: boolean;
62-
api?: TelegramApiOverride;
63-
retry?: RetryConfig;
64-
gatewayClientScopes?: readonly string[];
65-
/** Inline keyboard buttons (reply markup). Pass empty array to remove buttons. */
66-
buttons?: TelegramInlineButtons;
67-
/** Resolved runtime config from the command or gateway boundary. */
68-
cfg: OpenClawConfig;
69-
};
40+
type TelegramEditOpts = TelegramEditReplyMarkupOpts &
41+
Pick<TelegramSendOpts, "textMode"> & {
42+
/** Controls whether link previews are shown in the edited message. */
43+
linkPreview?: boolean;
44+
/** Use Telegram's media-caption edit endpoint, or fall back to it when text edits target media. */
45+
editMode?: "text" | "caption" | "auto";
46+
};
7047

7148
export async function editMessageReplyMarkupTelegram(
7249
chatIdInput: string | number,

‎extensions/telegram/src/send-forum-topics.ts‎

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { recordChannelActivity } from "openclaw/plugin-sdk/channel-activity-runtime";
2-
import type { RetryConfig } from "openclaw/plugin-sdk/retry-runtime";
32
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
43
import {
54
createTelegramNonIdempotentRequestWithDiag,
@@ -9,26 +8,15 @@ import {
98
resolveTelegramApiContext,
109
withTelegramApiContextLease,
1110
type TelegramApiContext,
12-
type TelegramApiOverride,
1311
} from "./send-context.js";
14-
import type { OpenClawConfig } from "./send.runtime.js";
12+
import type { TelegramApiCallOpts, TelegramMessageActionOpts } from "./send-message-types.js";
1513
import { parseTelegramTarget } from "./targets.js";
1614

17-
type TelegramDeleteOpts = {
18-
cfg: OpenClawConfig;
19-
token?: string;
20-
accountId?: string;
21-
notify?: boolean;
22-
verbose?: boolean;
23-
api?: TelegramApiOverride;
24-
retry?: RetryConfig;
25-
gatewayClientScopes?: readonly string[];
26-
};
2715
type TelegramCreateForumTopicParams = NonNullable<
2816
Parameters<TelegramApiContext["api"]["createForumTopic"]>[2]
2917
>;
3018

31-
type TelegramEditForumTopicOpts = TelegramDeleteOpts & {
19+
type TelegramEditForumTopicOpts = TelegramMessageActionOpts & {
3220
name?: string;
3321
iconCustomEmojiId?: string;
3422
};
@@ -122,7 +110,7 @@ export async function renameForumTopicTelegram(
122110
chatIdInput: string | number,
123111
messageThreadIdInput: string | number,
124112
name: string,
125-
opts: TelegramDeleteOpts,
113+
opts: TelegramMessageActionOpts,
126114
): Promise<{ ok: true; chatId: string; messageThreadId: number; name: string }> {
127115
const result = await editForumTopicTelegram(chatIdInput, messageThreadIdInput, {
128116
...opts,
@@ -140,14 +128,7 @@ export async function renameForumTopicTelegram(
140128
// Forum topic creation
141129
// ---------------------------------------------------------------------------
142130

143-
type TelegramCreateForumTopicOpts = {
144-
cfg: OpenClawConfig;
145-
token?: string;
146-
accountId?: string;
147-
api?: TelegramApiOverride;
148-
verbose?: boolean;
149-
retry?: RetryConfig;
150-
gatewayClientScopes?: readonly string[];
131+
type TelegramCreateForumTopicOpts = TelegramApiCallOpts & {
151132
/** Icon color for the topic (must be one of 0x6FB9F0, 0xFFD67E, 0xCB86DB, 0x8EEE98, 0xFF93B2, 0xFB6F5F). */
152133
iconColor?: TelegramCreateForumTopicParams["icon_color"];
153134
/** Custom emoji ID for the topic icon. */

‎extensions/telegram/src/send-message-types.ts‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ export type TelegramSendOpts = {
5151
onDeliveryResult?: (result: TelegramSendResult) => Promise<void> | void;
5252
};
5353

54+
export type TelegramApiCallOpts = Pick<
55+
TelegramSendOpts,
56+
"cfg" | "token" | "accountId" | "verbose" | "api" | "retry" | "gatewayClientScopes"
57+
>;
58+
59+
export type TelegramThreadedSendOpts = TelegramApiCallOpts &
60+
Pick<TelegramSendOpts, "replyToMessageId" | "messageThreadId">;
61+
62+
export type TelegramMessageActionOpts = TelegramApiCallOpts & { notify?: boolean };
63+
5464
export type TelegramSendMessageParams = Parameters<TelegramApi["sendMessage"]>[2];
5565

5666
export type TelegramSendResult = {
@@ -63,20 +73,8 @@ export type TelegramSendResult = {
6373
};
6474
};
6575

66-
export type TelegramLocationSendOpts = Pick<
67-
TelegramSendOpts,
68-
| "cfg"
69-
| "token"
70-
| "accountId"
71-
| "verbose"
72-
| "api"
73-
| "retry"
74-
| "gatewayClientScopes"
75-
| "replyToMessageId"
76-
| "messageThreadId"
77-
| "buttons"
78-
| "quoteText"
79-
| "promptContextProjectionPlan"
80-
| "silent"
81-
| "onDeliveryResult"
82-
>;
76+
export type TelegramLocationSendOpts = TelegramThreadedSendOpts &
77+
Pick<
78+
TelegramSendOpts,
79+
"buttons" | "quoteText" | "promptContextProjectionPlan" | "silent" | "onDeliveryResult"
80+
>;

‎extensions/telegram/src/send-special.ts‎

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
1-
import type { RetryConfig } from "openclaw/plugin-sdk/retry-runtime";
21
import {
32
resolveTelegramApiContext,
43
withTelegramApiContextLease,
54
type TelegramApiContext,
6-
type TelegramApiOverride,
75
} from "./send-context.js";
8-
import type { TelegramSendResult } from "./send-message-types.js";
6+
import type {
7+
TelegramSendOpts,
8+
TelegramSendResult,
9+
TelegramThreadedSendOpts,
10+
} from "./send-message-types.js";
911
import { finalizeTelegramOutbound, prepareTelegramOutbound } from "./send-outbound.js";
10-
import { normalizePollInput, type OpenClawConfig, type PollInput } from "./send.runtime.js";
12+
import { normalizePollInput, type PollInput } from "./send.runtime.js";
1113

1214
type TelegramSendPollParams = Parameters<TelegramApiContext["api"]["sendPoll"]>[3];
1315

14-
type TelegramStickerOpts = {
15-
cfg: OpenClawConfig;
16-
token?: string;
17-
accountId?: string;
18-
verbose?: boolean;
19-
api?: TelegramApiOverride;
20-
retry?: RetryConfig;
21-
gatewayClientScopes?: readonly string[];
22-
/** Message ID to reply to (for threading) */
23-
replyToMessageId?: number;
24-
/** Forum topic thread ID (for forum supergroups) */
25-
messageThreadId?: number;
26-
};
27-
2816
/**
2917
* Send a sticker to a Telegram chat by file_id.
3018
* @param to - Chat ID or username (e.g., "123456789" or "@username")
@@ -34,7 +22,7 @@ type TelegramStickerOpts = {
3422
export async function sendStickerTelegram(
3523
to: string,
3624
fileId: string,
37-
opts: TelegramStickerOpts,
25+
opts: TelegramThreadedSendOpts,
3826
): Promise<TelegramSendResult> {
3927
if (!fileId?.trim()) {
4028
throw new Error("Telegram sticker file_id is required");
@@ -50,7 +38,7 @@ export async function sendStickerTelegram(
5038
async function sendStickerTelegramWithContext(
5139
to: string,
5240
fileId: string,
53-
opts: TelegramStickerOpts,
41+
opts: TelegramThreadedSendOpts,
5442
context: TelegramApiContext,
5543
): Promise<TelegramSendResult> {
5644
const { api } = context;
@@ -79,23 +67,11 @@ async function sendStickerTelegramWithContext(
7967
});
8068
}
8169

82-
type TelegramPollOpts = {
83-
cfg: OpenClawConfig;
84-
token?: string;
85-
accountId?: string;
86-
verbose?: boolean;
87-
api?: TelegramApiOverride;
88-
retry?: RetryConfig;
89-
gatewayClientScopes?: readonly string[];
90-
/** Message ID to reply to (for threading) */
91-
replyToMessageId?: number;
92-
/** Forum topic thread ID (for forum supergroups) */
93-
messageThreadId?: number;
94-
/** Send message silently (no notification). Defaults to false. */
95-
silent?: boolean;
96-
/** Whether votes are anonymous. Defaults to true (Telegram default). */
97-
isAnonymous?: boolean;
98-
};
70+
type TelegramPollOpts = TelegramThreadedSendOpts &
71+
Pick<TelegramSendOpts, "silent"> & {
72+
/** Whether votes are anonymous. Defaults to true (Telegram default). */
73+
isAnonymous?: boolean;
74+
};
9975

10076
/**
10177
* Send a poll to a Telegram chat.

0 commit comments

Comments
 (0)