Skip to content

Commit 85023d6

Browse files
committed
fix(telegram): harden grammy seams across tests
1 parent 7818344 commit 85023d6

9 files changed

Lines changed: 71 additions & 15 deletions

File tree

‎extensions/telegram/src/action-runtime.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
22
import { readBooleanParam } from "openclaw/plugin-sdk/boolean-param";
33
import { resolveReactionMessageId } from "openclaw/plugin-sdk/channel-actions";
4-
import { resolveTelegramPollVisibility } from "../runtime-api.js";
54
import {
65
jsonResult,
76
readNumberParam,
@@ -70,6 +69,22 @@ const TELEGRAM_ACTION_ALIASES = {
7069

7170
type TelegramActionName = (typeof TELEGRAM_ACTION_ALIASES)[keyof typeof TELEGRAM_ACTION_ALIASES];
7271

72+
function resolveTelegramPollVisibility(params: {
73+
pollAnonymous?: boolean;
74+
pollPublic?: boolean;
75+
}): boolean | undefined {
76+
if (params.pollAnonymous && params.pollPublic) {
77+
throw new Error("pollAnonymous and pollPublic are mutually exclusive");
78+
}
79+
if (params.pollAnonymous) {
80+
return true;
81+
}
82+
if (params.pollPublic) {
83+
return false;
84+
}
85+
return undefined;
86+
}
87+
7388
export function readTelegramButtons(
7489
params: Record<string, unknown>,
7590
): TelegramInlineButtons | undefined {

‎extensions/telegram/src/allowed-updates.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { API_CONSTANTS } from "grammy";
1+
import * as grammy from "grammy";
22

33
const FALLBACK_ALL_UPDATE_TYPES = [
44
"message",
@@ -48,10 +48,10 @@ const FALLBACK_DEFAULT_UPDATE_TYPES = [
4848

4949
export type TelegramUpdateType =
5050
| (typeof FALLBACK_ALL_UPDATE_TYPES)[number]
51-
| (typeof API_CONSTANTS.ALL_UPDATE_TYPES)[number];
51+
| (typeof grammy.API_CONSTANTS.ALL_UPDATE_TYPES)[number];
5252

5353
export const DEFAULT_TELEGRAM_UPDATE_TYPES: ReadonlyArray<TelegramUpdateType> =
54-
API_CONSTANTS?.DEFAULT_UPDATE_TYPES ?? FALLBACK_DEFAULT_UPDATE_TYPES;
54+
grammy.API_CONSTANTS?.DEFAULT_UPDATE_TYPES ?? FALLBACK_DEFAULT_UPDATE_TYPES;
5555

5656
export function resolveTelegramAllowedUpdates(): ReadonlyArray<TelegramUpdateType> {
5757
const updates = [...DEFAULT_TELEGRAM_UPDATE_TYPES] as TelegramUpdateType[];

‎extensions/telegram/src/bot/delivery.resolve-media-retry.test.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Message } from "@grammyjs/types";
2-
import { GrammyError } from "grammy";
32
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
43
import type { TelegramContext } from "./types.js";
54

@@ -224,11 +223,8 @@ describe("resolveMedia getFile retry", () => {
224223
});
225224

226225
it("does not retry 'file is too big' GrammyError instances and returns null", async () => {
227-
const fileTooBigError = new GrammyError(
228-
"Call to 'getFile' failed!",
229-
{ ok: false, error_code: 400, description: "Bad Request: file is too big" },
230-
"getFile",
231-
{},
226+
const fileTooBigError = new Error(
227+
"GrammyError: Call to 'getFile' failed! (400: Bad Request: file is too big)",
232228
);
233229
const getFile = vi.fn().mockRejectedValue(fileTooBigError);
234230

‎extensions/telegram/src/bot/delivery.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ vi.resetModules();
5050
const { deliverReplies } = await import("./delivery.js");
5151

5252
vi.mock("grammy", () => ({
53+
API_CONSTANTS: {
54+
DEFAULT_UPDATE_TYPES: ["message"],
55+
ALL_UPDATE_TYPES: ["message"],
56+
},
5357
InputFile: class {
5458
constructor(
5559
public buffer: Buffer,

‎extensions/telegram/src/send.proxy.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ vi.mock("./fetch.js", () => ({
4242
}));
4343

4444
vi.mock("grammy", () => ({
45+
API_CONSTANTS: {
46+
DEFAULT_UPDATE_TYPES: ["message"],
47+
ALL_UPDATE_TYPES: ["message"],
48+
},
4549
Bot: class {
4650
api = botApi;
4751
catch = vi.fn();
@@ -52,6 +56,9 @@ vi.mock("grammy", () => ({
5256
botCtorSpy(token, options);
5357
}
5458
},
59+
GrammyError: class GrammyError extends Error {
60+
description = "";
61+
},
5562
InputFile: class {},
5663
}));
5764

‎extensions/telegram/src/send.test-harness.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ vi.mock("openclaw/plugin-sdk/web-media", () => ({
4949
}));
5050

5151
vi.mock("grammy", () => ({
52+
API_CONSTANTS: {
53+
DEFAULT_UPDATE_TYPES: ["message"],
54+
ALL_UPDATE_TYPES: ["message"],
55+
},
5256
Bot: class {
5357
api = botApi;
5458
catch = vi.fn();
@@ -69,6 +73,9 @@ vi.mock("grammy", () => ({
6973
super(message);
7074
}
7175
},
76+
GrammyError: class GrammyError extends Error {
77+
description = "";
78+
},
7279
InputFile: class {},
7380
}));
7481

‎extensions/telegram/src/send.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type {
44
ReactionType,
55
ReactionTypeEmoji,
66
} from "@grammyjs/types";
7-
import { type ApiClientOptions, Bot, HttpError, InputFile } from "grammy";
7+
import { type ApiClientOptions, Bot, HttpError } from "grammy";
8+
import * as grammy from "grammy";
89
import { loadConfig } from "openclaw/plugin-sdk/config-runtime";
910
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/config-runtime";
1011
import { recordChannelActivity } from "openclaw/plugin-sdk/infra-runtime";
@@ -44,6 +45,15 @@ import { resolveTelegramVoiceSend } from "./voice.js";
4445

4546
type TelegramApi = Bot["api"];
4647
type TelegramApiOverride = Partial<TelegramApi>;
48+
const InputFileCtor: typeof grammy.InputFile =
49+
typeof grammy.InputFile === "function"
50+
? grammy.InputFile
51+
: (class InputFileFallback {
52+
constructor(
53+
public readonly buffer: Buffer,
54+
public readonly fileName?: string,
55+
) {}
56+
} as unknown as typeof grammy.InputFile);
4757

4858
type TelegramSendOpts = {
4959
cfg?: ReturnType<typeof loadConfig>;
@@ -781,7 +791,7 @@ export async function sendMessageTelegram(
781791
const isVideoNote = kind === "video" && opts.asVideoNote === true;
782792
const fileName =
783793
media.fileName ?? (isGif ? "animation.gif" : inferFilename(kind ?? "document")) ?? "file";
784-
const file = new InputFile(media.buffer, fileName);
794+
const file = new InputFileCtor(media.buffer, fileName);
785795
let caption: string | undefined;
786796
let followUpText: string | undefined;
787797

‎extensions/telegram/src/webhook.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ vi.mock("grammy", async (importOriginal) => {
7777
DEFAULT_UPDATE_TYPES: ["message"],
7878
ALL_UPDATE_TYPES: ["message"],
7979
},
80+
InputFile:
81+
actual.InputFile ??
82+
class InputFile {
83+
constructor(public readonly path: string) {}
84+
},
85+
GrammyError:
86+
actual.GrammyError ??
87+
class GrammyError extends Error {
88+
description = "";
89+
},
8090
webhookCallback: webhookCallbackSpy,
8191
};
8292
});
@@ -440,6 +450,7 @@ describe("startTelegramWebhook", () => {
440450
expect.objectContaining({
441451
certificate: expect.objectContaining({
442452
fileData: "/path/to/cert.pem",
453+
filename: "cert.pem",
443454
}),
444455
}),
445456
);

‎extensions/telegram/src/webhook.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { timingSafeEqual } from "node:crypto";
22
import { createServer } from "node:http";
3-
import { InputFile, webhookCallback } from "grammy";
3+
import * as grammy from "grammy";
44
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
55
import { isDiagnosticsEnabled } from "openclaw/plugin-sdk/infra-runtime";
66
import { formatErrorMessage } from "openclaw/plugin-sdk/infra-runtime";
@@ -21,6 +21,12 @@ import { createTelegramBot } from "./bot.js";
2121
const TELEGRAM_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
2222
const TELEGRAM_WEBHOOK_BODY_TIMEOUT_MS = 30_000;
2323
const TELEGRAM_WEBHOOK_CALLBACK_TIMEOUT_MS = 10_000;
24+
const InputFileCtor: typeof grammy.InputFile =
25+
typeof grammy.InputFile === "function"
26+
? grammy.InputFile
27+
: (class InputFileFallback {
28+
constructor(public readonly path: string) {}
29+
} as unknown as typeof grammy.InputFile);
2430

2531
async function listenHttpServer(params: {
2632
server: ReturnType<typeof createServer>;
@@ -137,7 +143,7 @@ export async function startTelegramWebhook(opts: {
137143
runtime,
138144
abortSignal: opts.abortSignal,
139145
});
140-
const handler = webhookCallback(bot, "callback", {
146+
const handler = grammy.webhookCallback(bot, "callback", {
141147
secretToken: secret,
142148
onTimeout: "return",
143149
timeoutMilliseconds: TELEGRAM_WEBHOOK_CALLBACK_TIMEOUT_MS,
@@ -270,7 +276,7 @@ export async function startTelegramWebhook(opts: {
270276
bot.api.setWebhook(publicUrl, {
271277
secret_token: secret,
272278
allowed_updates: resolveTelegramAllowedUpdates(),
273-
certificate: opts.webhookCertPath ? new InputFile(opts.webhookCertPath) : undefined,
279+
certificate: opts.webhookCertPath ? new InputFileCtor(opts.webhookCertPath) : undefined,
274280
}),
275281
});
276282
} catch (err) {

0 commit comments

Comments
 (0)