@qusic/pi-telegram 0.1.0 → 0.1.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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![pi](https://img.shields.io/npm/dependency-version/%40qusic%2Fpi-telegram/dev/%40earendil-works%2Fpi-coding-agent?label=pi)](https://github.com/earendil-works/pi)
6
6
  [![License](https://img.shields.io/npm/l/%40qusic%2Fpi-telegram)](LICENSE)
7
7
 
8
- A Telegram DM bridge for [pi](https://pi.dev) — chat with your pi coding agent from anywhere through a Telegram bot. Based on [badlogic/pi-telegram](https://github.com/badlogic/pi-telegram), rewritten with AI (Claude `opus-4.7`) to better fit my personal preferences and needs.
8
+ A Telegram DM bridge for [pi](https://pi.dev) — chat with your pi coding agent from anywhere through a Telegram bot. Based on [badlogic/pi-telegram](https://github.com/badlogic/pi-telegram), rewritten with AI to better fit my personal preferences and needs.
9
9
 
10
10
  ## Features
11
11
 
@@ -39,7 +39,7 @@ Resuming a session echoes that session's last reply back to the chat, so it refl
39
39
  | Area | Upstream | This fork |
40
40
  | --------------------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
41
41
  | Setup & lifecycle | `/telegram-setup` / `/telegram-connect` / `/telegram-disconnect` commands; explicit per-session connect | No pi-side commands; you write `~/.pi/agent/telegram.json` yourself, polling auto-starts per session |
42
- | Authorization | First DM user is auto-paired | First DM user id is reported via `notify`; you add it to the config and restart |
42
+ | Authorization | First DM user is auto-paired | First DM sender id is shown as a warning in pi; unauthorized senders receive no response |
43
43
  | Commands & bot menu | Telegram-side commands limited to `/status`, `/compact`, `stop`, `/help`, `/start`; no menu published | Adds `/new`, `/resume`, `/skills` and publishes the menu via `setMyCommands`; session switching from Telegram |
44
44
  | Concurrency | Extra messages while busy are queued and dispatched after the current turn ends | Extra messages are steered into the running turn |
45
45
  | Aborted-turn replay | After `stop`, queued messages are re-injected as a synthetic history block | No replay — `stop` just aborts |
@@ -76,7 +76,7 @@ pi -e git:github.com/Qusic/pi-telegram
76
76
  ```
77
77
 
78
78
  3. Start pi — polling starts automatically.
79
- 4. DM your bot once. pi will notify you with your Telegram user id; add `"allowedUserId": <id>` to the config and restart.
79
+ 4. DM your bot once. pi shows a warning with your Telegram user id; add `"allowedUserId": <id>` to the config and restart. The bot stays silent until authorized.
80
80
 
81
81
  ## Usage
82
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qusic/pi-telegram",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Telegram bridge extension for Pi agent",
5
5
  "keywords": [
6
6
  "pi-package",
package/src/config.ts CHANGED
@@ -32,7 +32,7 @@ function validateConfig(config: TelegramConfig): void {
32
32
  throw new Error(
33
33
  `Telegram bridge: ${CONFIG_PATH} missing "botToken". ` +
34
34
  `Create the file with {"botToken": "<bot-token-from-BotFather>"} and restart. ` +
35
- `Optionally set "allowedUserId" too — if omitted, the bot will report your user id when you first message it.`,
35
+ `Optionally set "allowedUserId" too — if omitted, pi will show the sender's user id as a warning when the bot first receives a DM.`,
36
36
  );
37
37
  }
38
38
  }
package/src/polling.ts CHANGED
@@ -20,6 +20,25 @@ interface TelegramMediaGroupState {
20
20
  flushTimer?: ReturnType<typeof setTimeout>;
21
21
  }
22
22
 
23
+ interface IncomingPrivateMessage {
24
+ message: TelegramMessage;
25
+ userId: number;
26
+ }
27
+
28
+ function getIncomingPrivateMessage(update: TelegramUpdate): IncomingPrivateMessage | undefined {
29
+ const message = update.message ?? update.edited_message;
30
+ if (message?.chat.type !== "private" || !message.from || message.from.is_bot) return undefined;
31
+ return { message, userId: message.from.id };
32
+ }
33
+
34
+ function notifyAuthorizationRequired(ctx: ExtensionContext, userId: number): void {
35
+ ctx.ui.notify(
36
+ `Telegram user id ${userId} needs authorization. ` +
37
+ `Add "allowedUserId": ${userId} to ~/.pi/agent/telegram.json, then restart pi.`,
38
+ "warning",
39
+ );
40
+ }
41
+
23
42
  interface PollingDeps {
24
43
  pi: ExtensionAPI;
25
44
  api: ReturnType<typeof createApi>;
@@ -62,7 +81,15 @@ export function createPolling(deps: PollingDeps): void {
62
81
  { signal },
63
82
  );
64
83
  const last = updates.at(-1);
65
- if (last) await config.update({ lastUpdateId: last.update_id });
84
+ if (last) {
85
+ await config.update({ lastUpdateId: last.update_id });
86
+ // Skip stale work on startup, but still report the authorization id
87
+ // from the latest private message when authorization is unconfigured.
88
+ if (config.get().allowedUserId === undefined) {
89
+ const incoming = getIncomingPrivateMessage(last);
90
+ if (incoming) notifyAuthorizationRequired(ctx, incoming.userId);
91
+ }
92
+ }
66
93
  } catch {
67
94
  // ignore
68
95
  }
@@ -98,25 +125,19 @@ export function createPolling(deps: PollingDeps): void {
98
125
  }
99
126
 
100
127
  async function handleUpdate(update: TelegramUpdate, ctx: ExtensionContext): Promise<void> {
101
- const message = update.message || update.edited_message;
102
- if (message?.chat.type !== "private" || !message.from || message.from.is_bot) return;
128
+ const incoming = getIncomingPrivateMessage(update);
129
+ if (!incoming) return;
130
+ const { message, userId } = incoming;
103
131
 
104
132
  const cfg = config.get();
105
133
  if (cfg.allowedUserId === undefined) {
106
- // Bootstrap: report the user id so the operator can authorize it.
107
- ctx.ui.notify(
108
- `Telegram bridge: received message from user id ${message.from.id}. ` +
109
- `Add "allowedUserId": ${message.from.id} to ~/.pi/agent/telegram.json to authorize, then restart.`,
110
- "warning",
111
- );
112
- await api.sendText(message.chat.id, "Bot not configured: ask the operator to authorize your user id.");
134
+ notifyAuthorizationRequired(ctx, userId);
113
135
  return;
114
136
  }
115
137
 
116
- if (message.from.id !== cfg.allowedUserId) {
117
- await api.sendText(message.chat.id, "This bot is not authorized for your account.");
118
- return;
119
- }
138
+ // Stay silent for unauthorized senders so the bot does not disclose that
139
+ // it is active or invite strangers to keep probing it.
140
+ if (userId !== cfg.allowedUserId) return;
120
141
 
121
142
  // Albums arrive as separate updates with the same media_group_id; debounce.
122
143
  if (message.media_group_id) {
@@ -139,8 +160,8 @@ export function createPolling(deps: PollingDeps): void {
139
160
  // ---------- pi handler registration ----------
140
161
 
141
162
  pi.on("session_start", (_event, ctx) => {
142
- // botToken validity guaranteed by createConfig; allowedUserId may still
143
- // be missing — handleUpdate bootstraps it on first incoming message.
163
+ // botToken validity is guaranteed by createConfig; polling reports the
164
+ // sender id locally while allowedUserId is missing.
144
165
  start(ctx);
145
166
  });
146
167