Skip to content

Commit 204ef7f

Browse files
committed
fix(telegram): report webhook registration status
1 parent 7108414 commit 204ef7f

11 files changed

Lines changed: 268 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Docs: https://docs.openclaw.ai
3939
- Channels/Telegram: continue polling when `deleteWebhook` hits a transient network failure but `getWebhookInfo` confirms no webhook is configured, so startup does not retry cleanup forever after the webhook was already removed. Refs #74086; carries forward #47384. Thanks @clovericbot.
4040
- Channels/Telegram: apply strict safe-send retry to inbound final replies when grammY wraps a pre-connect failure, while leaving ambiguous plain network envelopes single-shot to avoid duplicate visible messages. Fixes #74203. Thanks @nanli2000cn.
4141
- Channels/Telegram: surface polling liveness warnings in channel status and doctor when a running long-poller has not completed `getUpdates` after startup grace or its transport activity is stale, so silent polling failures no longer look clean. Refs #74299. Thanks @lolaopenclaw.
42+
- Channels/Telegram: publish webhook runtime state and warn when `setWebhook` has not completed after startup grace, so webhook-mode accounts no longer look healthy while registration is still failing or retrying. Refs #74299. Thanks @lolaopenclaw and @martingarramon.
4243
- Channels/Telegram: bound native command menu `deleteMyCommands` and `setMyCommands` Bot API calls and allow the same timeout-triggered transport fallback retry as other startup control calls, so Windows/WSL network stalls cannot leave command sync hanging behind an otherwise running provider. Refs #74086. Thanks @SymbolStar.
4344
- ACP/commands: accept forwarded ACP timeout config controls in the OpenClaw bridge, treat unsupported discard-close controls as recoverable cleanup, and restore native `/verbose full` plus no-arg status behavior, so Discord command menus and nested ACP turns no longer fail on supported session controls. Thanks @vincentkoc.
4445
- TUI/status: clear stale `streaming` footer state when a final event arrives after the active run was already cleared and no tracked runs remain, while preserving concurrent-run ownership and inactive local `/btw` terminal handling. Fixes #64825; carries forward #64842, #64843, #64847, and #64862. Thanks @briandevans and @Yanhu007.

‎docs/channels/telegram.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ Per-account, per-group, and per-topic overrides are supported (same inheritance
867867
- Some hosts resolve `api.telegram.org` to IPv6 first; broken IPv6 egress can cause intermittent Telegram API failures.
868868
- If logs include `TypeError: fetch failed` or `Network request for 'getUpdates' failed!`, OpenClaw now retries these as recoverable network errors.
869869
- If logs include `Polling stall detected`, OpenClaw restarts polling and rebuilds the Telegram transport after 120 seconds without completed long-poll liveness by default.
870-
- `openclaw channels status --probe` and `openclaw doctor` warn when a running polling account has not completed `getUpdates` after startup grace, or when its last successful polling transport activity is stale.
870+
- `openclaw channels status --probe` and `openclaw doctor` warn when a running polling account has not completed `getUpdates` after startup grace, when a running webhook account has not completed `setWebhook` after startup grace, or when the last successful polling transport activity is stale.
871871
- Increase `channels.telegram.pollingStallThresholdMs` only when long-running `getUpdates` calls are healthy but your host still reports false polling-stall restarts. Persistent stalls usually point to proxy, DNS, IPv6, or TLS egress issues between the host and `api.telegram.org`.
872872
- Telegram also honors process proxy env for Bot API transport, including `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, and their lowercase variants. `NO_PROXY` / `no_proxy` can still bypass `api.telegram.org`.
873873
- If the OpenClaw managed proxy is configured through `OPENCLAW_PROXY_URL` for a service environment and no standard proxy env is present, Telegram uses that URL for Bot API transport too.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,13 @@ describe("monitorTelegramProvider (grammY)", () => {
774774
});
775775

776776
it("passes configured webhookHost to webhook listener", async () => {
777+
const setStatus = vi.fn();
777778
await monitorTelegramProvider({
778779
token: "tok",
779780
useWebhook: true,
780781
webhookUrl: "https://example.test/telegram",
781782
webhookSecret: "secret",
783+
setStatus,
782784
config: {
783785
agents: { defaults: { maxConcurrent: 2 } },
784786
channels: {
@@ -792,6 +794,7 @@ describe("monitorTelegramProvider (grammY)", () => {
792794
expect(startTelegramWebhookSpy).toHaveBeenCalledWith(
793795
expect.objectContaining({
794796
host: "0.0.0.0",
797+
setStatus,
795798
}),
796799
);
797800
expect(runSpy).not.toHaveBeenCalled();

‎extensions/telegram/src/monitor.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
154154
abortSignal: opts.abortSignal,
155155
publicUrl: opts.webhookUrl,
156156
webhookCertPath: opts.webhookCertPath,
157+
setStatus: opts.setStatus,
157158
});
158159
await waitForAbortSignal(opts.abortSignal);
159160
return;

‎extensions/telegram/src/status-issues.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212

1313
const TELEGRAM_POLLING_CONNECT_GRACE_MS = 120_000;
1414
const TELEGRAM_POLLING_STALE_TRANSPORT_MS = 30 * 60_000;
15+
const TELEGRAM_WEBHOOK_CONNECT_GRACE_MS = 120_000;
1516

1617
type TelegramAccountStatus = {
1718
accountId?: unknown;
@@ -124,6 +125,40 @@ function collectTelegramPollingRuntimeIssues(params: {
124125
}
125126
}
126127

128+
function collectTelegramWebhookRuntimeIssues(params: {
129+
account: TelegramAccountStatus;
130+
accountId: string;
131+
issues: ChannelStatusIssue[];
132+
now: number;
133+
}) {
134+
const { account, accountId, issues, now } = params;
135+
if (account.running !== true || asString(account.mode) !== "webhook") {
136+
return;
137+
}
138+
139+
if (account.connected !== false) {
140+
return;
141+
}
142+
143+
const lastStartAt = asFiniteNumber(account.lastStartAt);
144+
const withinStartupGrace =
145+
lastStartAt != null && now - lastStartAt < TELEGRAM_WEBHOOK_CONNECT_GRACE_MS;
146+
if (withinStartupGrace) {
147+
return;
148+
}
149+
150+
issues.push({
151+
channel: "telegram",
152+
accountId,
153+
kind: "runtime",
154+
message: appendTelegramRuntimeError(
155+
"Telegram webhook listener is running but setWebhook has not completed since startup",
156+
account.lastError,
157+
),
158+
fix: `Run: ${formatCliCommand("openclaw channels status --probe")} (or restart the gateway). Check the webhook URL, secret, TLS/proxy reachability, and Telegram setWebhook logs if it persists.`,
159+
});
160+
}
161+
127162
function readTelegramGroupMembershipAuditSummary(
128163
value: unknown,
129164
): TelegramGroupMembershipAuditSummary {
@@ -174,12 +209,19 @@ export function collectTelegramStatusIssues(
174209
if (!accountId) {
175210
continue;
176211
}
212+
const now = Date.now();
177213

178214
collectTelegramPollingRuntimeIssues({
179215
account,
180216
accountId,
181217
issues,
182-
now: Date.now(),
218+
now,
219+
});
220+
collectTelegramWebhookRuntimeIssues({
221+
account,
222+
accountId,
223+
issues,
224+
now,
183225
});
184226

185227
if (account.allowUnmentionedGroups === true) {

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe("collectTelegramStatusIssues", () => {
156156
expect(issues).toEqual([]);
157157
});
158158

159-
it("does not report webhook accounts with polling-only runtime diagnostics", () => {
159+
it("reports webhook runtime state that never completed setWebhook after startup grace", () => {
160160
const issues = collectTelegramStatusIssues([
161161
{
162162
accountId: "main",
@@ -166,6 +166,48 @@ describe("collectTelegramStatusIssues", () => {
166166
mode: "webhook",
167167
connected: false,
168168
lastStartAt: Date.now() - 10 * 60_000,
169+
lastError: "fetch failed",
170+
} as ChannelAccountSnapshot,
171+
]);
172+
173+
expect(issues).toEqual([
174+
expect.objectContaining({
175+
channel: "telegram",
176+
accountId: "main",
177+
kind: "runtime",
178+
message: expect.stringContaining("setWebhook has not completed"),
179+
}),
180+
]);
181+
expect(issues[0]?.message).toContain("fetch failed");
182+
expect(issues[0]?.fix).toContain("webhook URL");
183+
});
184+
185+
it("does not report webhook startup before the connect grace expires", () => {
186+
const issues = collectTelegramStatusIssues([
187+
{
188+
accountId: "main",
189+
enabled: true,
190+
configured: true,
191+
running: true,
192+
mode: "webhook",
193+
connected: false,
194+
lastStartAt: Date.now() - 60_000,
195+
} as ChannelAccountSnapshot,
196+
]);
197+
198+
expect(issues).toEqual([]);
199+
});
200+
201+
it("does not report an advertised webhook just because no user updates arrived", () => {
202+
const issues = collectTelegramStatusIssues([
203+
{
204+
accountId: "main",
205+
enabled: true,
206+
configured: true,
207+
running: true,
208+
mode: "webhook",
209+
connected: true,
210+
lastStartAt: Date.now() - 60 * 60_000,
169211
} as ChannelAccountSnapshot,
170212
]);
171213

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { createTelegramWebhookStatusPublisher } from "./webhook-status.js";
3+
4+
describe("createTelegramWebhookStatusPublisher", () => {
5+
it("publishes start, advertised, update, failure, and stop status patches", () => {
6+
const setStatus = vi.fn();
7+
const status = createTelegramWebhookStatusPublisher(setStatus);
8+
9+
status.noteWebhookStart();
10+
status.noteWebhookAdvertised(1234);
11+
status.noteWebhookUpdateReceived(2345);
12+
status.noteWebhookRegistrationFailure("fetch failed");
13+
status.noteWebhookStop();
14+
15+
expect(setStatus).toHaveBeenNthCalledWith(1, {
16+
mode: "webhook",
17+
connected: false,
18+
lastConnectedAt: null,
19+
lastEventAt: null,
20+
lastTransportActivityAt: null,
21+
});
22+
expect(setStatus).toHaveBeenNthCalledWith(2, {
23+
mode: "webhook",
24+
connected: true,
25+
lastConnectedAt: 1234,
26+
lastEventAt: 1234,
27+
lastError: null,
28+
});
29+
expect(setStatus).toHaveBeenNthCalledWith(3, {
30+
mode: "webhook",
31+
connected: true,
32+
lastConnectedAt: 2345,
33+
lastEventAt: 2345,
34+
lastError: null,
35+
});
36+
expect(setStatus).toHaveBeenNthCalledWith(4, {
37+
mode: "webhook",
38+
connected: false,
39+
lastError: "fetch failed",
40+
});
41+
expect(setStatus).toHaveBeenNthCalledWith(5, {
42+
mode: "webhook",
43+
connected: false,
44+
});
45+
});
46+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { ChannelAccountSnapshot } from "openclaw/plugin-sdk/channel-contract";
2+
import { createConnectedChannelStatusPatch } from "openclaw/plugin-sdk/gateway-runtime";
3+
4+
type TelegramWebhookStatusSink = (patch: Omit<ChannelAccountSnapshot, "accountId">) => void;
5+
6+
export function createTelegramWebhookStatusPublisher(setStatus?: TelegramWebhookStatusSink) {
7+
return {
8+
noteWebhookStart() {
9+
setStatus?.({
10+
mode: "webhook",
11+
connected: false,
12+
lastConnectedAt: null,
13+
lastEventAt: null,
14+
lastTransportActivityAt: null,
15+
});
16+
},
17+
noteWebhookAdvertised(at = Date.now()) {
18+
setStatus?.({
19+
...createConnectedChannelStatusPatch(at),
20+
mode: "webhook",
21+
lastError: null,
22+
});
23+
},
24+
noteWebhookUpdateReceived(at = Date.now()) {
25+
setStatus?.({
26+
...createConnectedChannelStatusPatch(at),
27+
mode: "webhook",
28+
lastError: null,
29+
});
30+
},
31+
noteWebhookRegistrationFailure(error: string) {
32+
setStatus?.({
33+
mode: "webhook",
34+
connected: false,
35+
lastError: error,
36+
});
37+
},
38+
noteWebhookStop() {
39+
setStatus?.({
40+
mode: "webhook",
41+
connected: false,
42+
});
43+
},
44+
};
45+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,15 @@ describe("startTelegramWebhook", () => {
412412
initSpy.mockClear();
413413
createTelegramBotSpy.mockClear();
414414
const runtimeLog = vi.fn();
415+
const setStatus = vi.fn();
415416
const cfg = { bindings: [] };
416417
await withStartedWebhook(
417418
{
418419
secret: TELEGRAM_SECRET,
419420
accountId: "opie",
420421
config: cfg,
421422
runtime: { log: runtimeLog, error: vi.fn(), exit: vi.fn() },
423+
setStatus,
422424
},
423425
async ({ port }) => {
424426
expect(createTelegramBotSpy).toHaveBeenCalledWith(
@@ -438,20 +440,36 @@ describe("startTelegramWebhook", () => {
438440
expect(runtimeLog).toHaveBeenCalledWith(
439441
expect.stringContaining("webhook advertised to telegram on http://"),
440442
);
443+
expect(setStatus).toHaveBeenNthCalledWith(1, {
444+
mode: "webhook",
445+
connected: false,
446+
lastConnectedAt: null,
447+
lastEventAt: null,
448+
lastTransportActivityAt: null,
449+
});
450+
expect(setStatus).toHaveBeenNthCalledWith(2, {
451+
mode: "webhook",
452+
connected: true,
453+
lastConnectedAt: expect.any(Number),
454+
lastEventAt: expect.any(Number),
455+
lastError: null,
456+
});
441457
},
442458
);
443459
});
444460

445461
it("keeps local listener alive and retries when setWebhook has a recoverable startup failure", async () => {
446462
const runtimeLog = vi.fn();
447463
const runtimeError = vi.fn();
464+
const setStatus = vi.fn();
448465
setWebhookSpy.mockRejectedValueOnce(new TypeError("fetch failed")).mockResolvedValueOnce(true);
449466

450467
await withStartedWebhook(
451468
{
452469
secret: TELEGRAM_SECRET,
453470
path: TELEGRAM_WEBHOOK_PATH,
454471
runtime: { log: runtimeLog, error: runtimeError, exit: vi.fn() },
472+
setStatus,
455473
webhookRegistrationRetryPolicy: {
456474
initialMs: 0,
457475
maxMs: 0,
@@ -471,6 +489,18 @@ describe("startTelegramWebhook", () => {
471489
expect(runtimeLog).toHaveBeenCalledWith(
472490
expect.stringContaining("webhook advertised to telegram on http://"),
473491
);
492+
expect(setStatus).toHaveBeenCalledWith({
493+
mode: "webhook",
494+
connected: false,
495+
lastError: "fetch failed",
496+
});
497+
expect(setStatus).toHaveBeenCalledWith(
498+
expect.objectContaining({
499+
mode: "webhook",
500+
connected: true,
501+
lastError: null,
502+
}),
503+
);
474504
},
475505
);
476506
});
@@ -532,13 +562,15 @@ describe("startTelegramWebhook", () => {
532562
it("invokes webhook handler on matching path", async () => {
533563
handleUpdateSpy.mockClear();
534564
createTelegramBotSpy.mockClear();
565+
const setStatus = vi.fn();
535566
const cfg = { bindings: [] };
536567
await withStartedWebhook(
537568
{
538569
secret: TELEGRAM_SECRET,
539570
accountId: "opie",
540571
config: cfg,
541572
path: TELEGRAM_WEBHOOK_PATH,
573+
setStatus,
542574
},
543575
async ({ port }) => {
544576
expect(createTelegramBotSpy).toHaveBeenCalledWith(
@@ -555,6 +587,13 @@ describe("startTelegramWebhook", () => {
555587
});
556588
expect(response.status).toBe(200);
557589
await vi.waitFor(() => expect(handleUpdateSpy).toHaveBeenCalledWith(JSON.parse(payload)));
590+
expect(setStatus).toHaveBeenCalledWith(
591+
expect.objectContaining({
592+
mode: "webhook",
593+
connected: true,
594+
lastError: null,
595+
}),
596+
);
558597
},
559598
);
560599
});

0 commit comments

Comments
 (0)