Skip to content

Commit b85d97f

Browse files
committed
refactor(telegram): inject shared bot deps
1 parent 243dabc commit b85d97f

5 files changed

Lines changed: 49 additions & 43 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { loadConfig, resolveStorePath } from "openclaw/plugin-sdk/config-runtime";
2+
import { readChannelAllowFromStore } from "openclaw/plugin-sdk/conversation-runtime";
3+
import { enqueueSystemEvent } from "openclaw/plugin-sdk/infra-runtime";
4+
import {
5+
dispatchReplyWithBufferedBlockDispatcher,
6+
listSkillCommandsForAgents,
7+
} from "openclaw/plugin-sdk/reply-runtime";
8+
import { wasSentByBot } from "./sent-message-cache.js";
9+
10+
export type TelegramBotDeps = {
11+
loadConfig: typeof loadConfig;
12+
resolveStorePath: typeof resolveStorePath;
13+
readChannelAllowFromStore: typeof readChannelAllowFromStore;
14+
enqueueSystemEvent: typeof enqueueSystemEvent;
15+
dispatchReplyWithBufferedBlockDispatcher: typeof dispatchReplyWithBufferedBlockDispatcher;
16+
listSkillCommandsForAgents: typeof listSkillCommandsForAgents;
17+
wasSentByBot: typeof wasSentByBot;
18+
};
19+
20+
export const defaultTelegramBotDeps: TelegramBotDeps = {
21+
loadConfig,
22+
resolveStorePath,
23+
readChannelAllowFromStore,
24+
enqueueSystemEvent,
25+
dispatchReplyWithBufferedBlockDispatcher,
26+
listSkillCommandsForAgents,
27+
wasSentByBot,
28+
};

extensions/telegram/src/bot-message-dispatch.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import type {
2424
import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
2525
import { resolveChunkMode } from "openclaw/plugin-sdk/reply-runtime";
2626
import { clearHistoryEntriesIfEnabled } from "openclaw/plugin-sdk/reply-runtime";
27-
import { dispatchReplyWithBufferedBlockDispatcher } from "openclaw/plugin-sdk/reply-runtime";
2827
import type { ReplyPayload } from "openclaw/plugin-sdk/reply-runtime";
2928
import { danger, logVerbose } from "openclaw/plugin-sdk/runtime-env";
3029
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
30+
import { defaultTelegramBotDeps, type TelegramBotDeps } from "./bot-deps.js";
3131
import type { TelegramMessageContext } from "./bot-message-context.js";
3232
import type { TelegramBotOptions } from "./bot.js";
3333
import { deliverReplies } from "./bot/delivery.js";
@@ -52,18 +52,6 @@ import { editMessageTelegram } from "./send.js";
5252
import { cacheSticker, describeStickerImage } from "./sticker-cache.js";
5353

5454
const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
55-
const DEFAULT_BOT_MESSAGE_DISPATCH_RUNTIME = {
56-
dispatchReplyWithBufferedBlockDispatcher,
57-
};
58-
let botMessageDispatchRuntimeForTest:
59-
| Partial<typeof DEFAULT_BOT_MESSAGE_DISPATCH_RUNTIME>
60-
| undefined;
61-
62-
export function setBotMessageDispatchRuntimeForTest(
63-
runtime?: Partial<typeof DEFAULT_BOT_MESSAGE_DISPATCH_RUNTIME>,
64-
): void {
65-
botMessageDispatchRuntimeForTest = runtime;
66-
}
6755

6856
/** Minimum chars before sending first streaming message (improves push notification UX) */
6957
const DRAFT_MIN_INITIAL_CHARS = 30;
@@ -122,6 +110,7 @@ type DispatchTelegramMessageParams = {
122110
streamMode: TelegramStreamMode;
123111
textLimit: number;
124112
telegramCfg: TelegramAccountConfig;
113+
telegramDeps?: TelegramBotDeps;
125114
opts: Pick<TelegramBotOptions, "token">;
126115
};
127116

@@ -159,12 +148,9 @@ export const dispatchTelegramMessage = async ({
159148
streamMode,
160149
textLimit,
161150
telegramCfg,
151+
telegramDeps = defaultTelegramBotDeps,
162152
opts,
163153
}: DispatchTelegramMessageParams) => {
164-
const botMessageDispatchRuntime = {
165-
...DEFAULT_BOT_MESSAGE_DISPATCH_RUNTIME,
166-
...botMessageDispatchRuntimeForTest,
167-
};
168154
const {
169155
ctxPayload,
170156
msg,
@@ -551,7 +537,7 @@ export const dispatchTelegramMessage = async ({
551537

552538
let dispatchError: unknown;
553539
try {
554-
({ queuedFinal } = await botMessageDispatchRuntime.dispatchReplyWithBufferedBlockDispatcher({
540+
({ queuedFinal } = await telegramDeps.dispatchReplyWithBufferedBlockDispatcher({
555541
ctx: ctxPayload,
556542
cfg,
557543
dispatcherOptions: {

extensions/telegram/src/bot-message.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ReplyToMode } from "openclaw/plugin-sdk/config-runtime";
22
import type { TelegramAccountConfig } from "openclaw/plugin-sdk/config-runtime";
33
import { danger } from "openclaw/plugin-sdk/runtime-env";
44
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
5+
import type { TelegramBotDeps } from "./bot-deps.js";
56
import {
67
buildTelegramMessageContext,
78
type BuildTelegramMessageContextParams,
@@ -21,6 +22,7 @@ type TelegramMessageProcessorDeps = Omit<
2122
replyToMode: ReplyToMode;
2223
streamMode: TelegramStreamMode;
2324
textLimit: number;
25+
telegramDeps: TelegramBotDeps;
2426
opts: Pick<TelegramBotOptions, "token">;
2527
};
2628

@@ -45,6 +47,7 @@ export const createTelegramMessageProcessor = (deps: TelegramMessageProcessorDep
4547
replyToMode,
4648
streamMode,
4749
textLimit,
50+
telegramDeps,
4851
opts,
4952
} = deps;
5053

@@ -89,6 +92,7 @@ export const createTelegramMessageProcessor = (deps: TelegramMessageProcessorDep
8992
streamMode,
9093
textLimit,
9194
telegramCfg,
95+
telegramDeps,
9296
opts,
9397
});
9498
} catch (err) {

extensions/telegram/src/bot-native-commands.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ import {
3737
resolveCommandArgMenu,
3838
} from "openclaw/plugin-sdk/reply-runtime";
3939
import { finalizeInboundContext } from "openclaw/plugin-sdk/reply-runtime";
40-
import { dispatchReplyWithBufferedBlockDispatcher } from "openclaw/plugin-sdk/reply-runtime";
41-
import { listSkillCommandsForAgents } from "openclaw/plugin-sdk/reply-runtime";
4240
import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
4341
import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing";
4442
import { danger, logVerbose } from "openclaw/plugin-sdk/runtime-env";
4543
import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
4644
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
4745
import { withTelegramApiErrorLogging } from "./api-logging.js";
4846
import { isSenderAllowed, normalizeDmAllowFromWithStore } from "./bot-access.js";
47+
import { defaultTelegramBotDeps, type TelegramBotDeps } from "./bot-deps.js";
4948
import type { TelegramMediaRef } from "./bot-message-context.js";
5049
import {
5150
buildCappedTelegramMenuCommands,
@@ -77,19 +76,6 @@ import { resolveTelegramGroupPromptSettings } from "./group-config-helpers.js";
7776
import { buildInlineKeyboard } from "./send.js";
7877

7978
const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again.";
80-
const DEFAULT_BOT_NATIVE_COMMANDS_RUNTIME = {
81-
dispatchReplyWithBufferedBlockDispatcher,
82-
listSkillCommandsForAgents,
83-
};
84-
let botNativeCommandsRuntimeForTest:
85-
| Partial<typeof DEFAULT_BOT_NATIVE_COMMANDS_RUNTIME>
86-
| undefined;
87-
88-
export function setBotNativeCommandsRuntimeForTest(
89-
runtime?: Partial<typeof DEFAULT_BOT_NATIVE_COMMANDS_RUNTIME>,
90-
): void {
91-
botNativeCommandsRuntimeForTest = runtime;
92-
}
9379

9480
type TelegramNativeCommandContext = Context & { match?: string };
9581

@@ -114,6 +100,7 @@ export type RegisterTelegramHandlerParams = {
114100
telegramTransport?: TelegramTransport;
115101
runtime: RuntimeEnv;
116102
telegramCfg: TelegramAccountConfig;
103+
telegramDeps?: TelegramBotDeps;
117104
allowFrom?: Array<string | number>;
118105
groupAllowFrom?: Array<string | number>;
119106
resolveGroupPolicy: (chatId: string | number) => ChannelGroupPolicy;
@@ -155,6 +142,7 @@ export type RegisterTelegramNativeCommandsParams = {
155142
messageThreadId?: number,
156143
) => { groupConfig?: TelegramGroupConfig; topicConfig?: TelegramTopicConfig };
157144
shouldSkipUpdate: (ctx: TelegramUpdateKeyContext) => boolean;
145+
telegramDeps?: TelegramBotDeps;
158146
opts: { token: string };
159147
};
160148

@@ -377,12 +365,9 @@ export const registerTelegramNativeCommands = ({
377365
resolveGroupPolicy,
378366
resolveTelegramGroupConfig,
379367
shouldSkipUpdate,
368+
telegramDeps = defaultTelegramBotDeps,
380369
opts,
381370
}: RegisterTelegramNativeCommandsParams) => {
382-
const botNativeCommandsRuntime = {
383-
...DEFAULT_BOT_NATIVE_COMMANDS_RUNTIME,
384-
...botNativeCommandsRuntimeForTest,
385-
};
386371
const silentErrorReplies = telegramCfg.silentErrorReplies === true;
387372
const boundRoute =
388373
nativeEnabled && nativeSkillsEnabled
@@ -395,7 +380,7 @@ export const registerTelegramNativeCommands = ({
395380
}
396381
const skillCommands =
397382
nativeEnabled && nativeSkillsEnabled && boundRoute
398-
? botNativeCommandsRuntime.listSkillCommandsForAgents({
383+
? telegramDeps.listSkillCommandsForAgents({
399384
cfg,
400385
agentIds: [boundRoute.agentId],
401386
})
@@ -776,7 +761,7 @@ export const registerTelegramNativeCommands = ({
776761
accountId: route.accountId,
777762
});
778763

779-
await botNativeCommandsRuntime.dispatchReplyWithBufferedBlockDispatcher({
764+
await telegramDeps.dispatchReplyWithBufferedBlockDispatcher({
780765
ctx: ctxPayload,
781766
cfg,
782767
dispatcherOptions: {

extensions/telegram/src/bot.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
resolveNativeSkillsEnabled,
1111
} from "openclaw/plugin-sdk/config-runtime";
1212
import type { OpenClawConfig, ReplyToMode } from "openclaw/plugin-sdk/config-runtime";
13-
import { loadConfig } from "openclaw/plugin-sdk/config-runtime";
1413
import {
1514
resolveChannelGroupPolicy,
1615
resolveChannelGroupRequireMention,
@@ -24,6 +23,7 @@ import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
2423
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
2524
import { createNonExitingRuntime, type RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
2625
import { resolveTelegramAccount } from "./accounts.js";
26+
import { defaultTelegramBotDeps, type TelegramBotDeps } from "./bot-deps.js";
2727
import { registerTelegramHandlers } from "./bot-handlers.js";
2828
import { createTelegramMessageProcessor } from "./bot-message.js";
2929
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
@@ -64,6 +64,7 @@ export type TelegramBotOptions = {
6464
};
6565
/** Pre-resolved Telegram transport to reuse across bot instances. If not provided, creates a new one. */
6666
telegramTransport?: TelegramTransport;
67+
telegramDeps?: TelegramBotDeps;
6768
};
6869

6970
export { getTelegramSequentialKey };
@@ -72,14 +73,12 @@ type TelegramBotRuntime = {
7273
Bot: typeof Bot;
7374
sequentialize: typeof sequentialize;
7475
apiThrottler: typeof apiThrottler;
75-
loadConfig: typeof loadConfig;
7676
};
7777

7878
const DEFAULT_TELEGRAM_BOT_RUNTIME: TelegramBotRuntime = {
7979
Bot,
8080
sequentialize,
8181
apiThrottler,
82-
loadConfig,
8382
};
8483

8584
let telegramBotRuntimeForTest: TelegramBotRuntime | undefined;
@@ -124,7 +123,8 @@ function extractTelegramApiMethod(input: TelegramFetchInput): string | null {
124123
export function createTelegramBot(opts: TelegramBotOptions) {
125124
const botRuntime = telegramBotRuntimeForTest ?? DEFAULT_TELEGRAM_BOT_RUNTIME;
126125
const runtime: RuntimeEnv = opts.runtime ?? createNonExitingRuntime();
127-
const cfg = opts.config ?? botRuntime.loadConfig();
126+
const telegramDeps = opts.telegramDeps ?? defaultTelegramBotDeps;
127+
const cfg = opts.config ?? telegramDeps.loadConfig();
128128
const account = resolveTelegramAccount({
129129
cfg,
130130
accountId: opts.accountId,
@@ -490,6 +490,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
490490
streamMode,
491491
textLimit,
492492
opts,
493+
telegramDeps,
493494
});
494495

495496
registerTelegramNativeCommands({
@@ -510,6 +511,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
510511
resolveTelegramGroupConfig,
511512
shouldSkipUpdate,
512513
opts,
514+
telegramDeps,
513515
});
514516

515517
registerTelegramHandlers({
@@ -528,6 +530,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
528530
shouldSkipUpdate,
529531
processMessage,
530532
logger,
533+
telegramDeps,
531534
});
532535

533536
const originalStop = bot.stop.bind(bot);

0 commit comments

Comments
 (0)