@bytesbrains/pi-telegram-bridge 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytesbrains/pi-telegram-bridge",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Telegram bot bridge for pi agents — send messages, ask questions, and listen for human replies via Telegram.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -577,4 +577,86 @@ describe("session lifecycle", () => {
577
577
  await new Promise((r) => setTimeout(r, 100));
578
578
  expect(mockPollUpdates).toHaveBeenCalledTimes(1);
579
579
  });
580
+
581
+ it("does not crash when TELEGRAM_BOT_TOKEN is not set (graceful degradation)", async () => {
582
+ vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
583
+ vi.stubEnv("TELEGRAM_CHAT_ID", "");
584
+ const consoleWarn = vi
585
+ .spyOn(console, "warn")
586
+ .mockImplementation(() => {});
587
+
588
+ const pi = createMockPI();
589
+ telegramBridge(pi as any);
590
+
591
+ const ctx = {
592
+ sessionManager: { getEntries: () => [] },
593
+ };
594
+
595
+ // Should NOT throw
596
+ await expect(
597
+ pi._listeners["session_start"][0]({}, ctx),
598
+ ).resolves.toBeUndefined();
599
+
600
+ // Should have warned about missing config
601
+ expect(consoleWarn).toHaveBeenCalledWith(
602
+ expect.stringContaining("TELEGRAM_BOT_TOKEN"),
603
+ );
604
+
605
+ // Listener should NOT have been started
606
+ expect(mockPollUpdates).not.toHaveBeenCalled();
607
+
608
+ consoleWarn.mockRestore();
609
+ });
610
+
611
+ it("does not crash when only TELEGRAM_BOT_TOKEN is missing", async () => {
612
+ vi.stubEnv("TELEGRAM_BOT_TOKEN", "");
613
+ // CHAT_ID is set
614
+ const consoleWarn = vi
615
+ .spyOn(console, "warn")
616
+ .mockImplementation(() => {});
617
+
618
+ const pi = createMockPI();
619
+ telegramBridge(pi as any);
620
+
621
+ const ctx = {
622
+ sessionManager: { getEntries: () => [] },
623
+ };
624
+
625
+ await expect(
626
+ pi._listeners["session_start"][0]({}, ctx),
627
+ ).resolves.toBeUndefined();
628
+
629
+ expect(consoleWarn).toHaveBeenCalledWith(
630
+ expect.stringContaining("TELEGRAM_BOT_TOKEN"),
631
+ );
632
+ expect(mockPollUpdates).not.toHaveBeenCalled();
633
+
634
+ consoleWarn.mockRestore();
635
+ });
636
+
637
+ it("does not crash when only TELEGRAM_CHAT_ID is missing", async () => {
638
+ vi.stubEnv("TELEGRAM_CHAT_ID", "");
639
+ // BOT_TOKEN is set
640
+ const consoleWarn = vi
641
+ .spyOn(console, "warn")
642
+ .mockImplementation(() => {});
643
+
644
+ const pi = createMockPI();
645
+ telegramBridge(pi as any);
646
+
647
+ const ctx = {
648
+ sessionManager: { getEntries: () => [] },
649
+ };
650
+
651
+ await expect(
652
+ pi._listeners["session_start"][0]({}, ctx),
653
+ ).resolves.toBeUndefined();
654
+
655
+ expect(consoleWarn).toHaveBeenCalledWith(
656
+ expect.stringContaining("TELEGRAM_BOT_TOKEN"),
657
+ );
658
+ expect(mockPollUpdates).not.toHaveBeenCalled();
659
+
660
+ consoleWarn.mockRestore();
661
+ });
580
662
  });
package/src/index.ts CHANGED
@@ -189,9 +189,23 @@ export default function telegramBridge(pi: ExtensionAPI) {
189
189
  lastUpdateId = (entry.data as { update_id: number }).update_id;
190
190
  }
191
191
  }
192
+ // Skip listener if Telegram is not configured
193
+ if (!process.env.TELEGRAM_BOT_TOKEN || !process.env.TELEGRAM_CHAT_ID) {
194
+ console.warn(
195
+ "pi-telegram-bridge: TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID not set. Listener disabled.",
196
+ );
197
+ return;
198
+ }
192
199
  // Start background listener
193
- mentionTrigger = await resolveMentionTrigger();
194
- startListener();
200
+ try {
201
+ mentionTrigger = await resolveMentionTrigger();
202
+ startListener();
203
+ } catch (e: unknown) {
204
+ console.warn(
205
+ "pi-telegram-bridge: Failed to start listener:",
206
+ e instanceof Error ? e.message : e,
207
+ );
208
+ }
195
209
  });
196
210
 
197
211
  pi.on("session_shutdown", () => {