@ours.network/tg-connector 0.4.0 → 0.5.0

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
@@ -169,6 +169,23 @@ answered in the chat. An emoji is validated against Telegram's fixed
169
169
  bot-reaction list **before** it is saved, with the valid set in the error —
170
170
  note that ✅ is *not* on that list, which is why the `read` default is 👌.
171
171
 
172
+ ### `/help` and the slash menu
173
+
174
+ **`/help`** is answered by the connector itself: it lists the commands the
175
+ connector owns (`/help`, `/id`, `/receipts`, `/emoji`) and the current receipt
176
+ settings for that connection. It works in a chat with no route yet too — you get
177
+ the command list and a pointer to `/id`.
178
+
179
+ The same list is registered with Telegram via `setMyCommands` at **default
180
+ scope** when a bot starts polling, so a client offers the commands in its slash
181
+ menu as you type. Registration is cosmetic and best-effort: if it fails it is
182
+ logged, and every command still works when typed by hand.
183
+
184
+ > **Only these four commands are intercepted.** Everything else you send is
185
+ > relayed to the agent exactly as typed, *including* text that starts with a
186
+ > slash — `/deploy prod` reaches the agent, and so does `/help me write the
187
+ > release note`, because only a **bare** `/help` is treated as the command.
188
+
172
189
  ### Message formatting
173
190
 
174
191
  Formatting is preserved **in both directions**.
package/dist/connector.js CHANGED
@@ -26182,6 +26182,30 @@ var TelegramClient = class {
26182
26182
  const parsed = await resp.json().catch(() => null);
26183
26183
  return parsed?.result?.message_id ?? 0;
26184
26184
  }
26185
+ // Publish the connector's own commands so a Telegram client offers them in its
26186
+ // slash menu while the user types. DEFAULT SCOPE on purpose (no `scope` field):
26187
+ // the list is a property of the bot, identical in every chat it serves, and a
26188
+ // token belongs to exactly one connector instance — so one call per bot start
26189
+ // covers every chat, including ones routed later, with no per-chat bookkeeping.
26190
+ //
26191
+ // Purely cosmetic: parseReceiptCommand, not this list, decides what is actually
26192
+ // intercepted. Throws for the caller to log — a bot whose menu did not publish
26193
+ // must still poll and still answer the commands typed by hand.
26194
+ async setMyCommands(commands) {
26195
+ const resp = await this.tgFetch(this.url("setMyCommands"), {
26196
+ method: "POST",
26197
+ headers: { "Content-Type": "application/json" },
26198
+ body: JSON.stringify({ commands })
26199
+ });
26200
+ if (!resp.ok) {
26201
+ const body = await resp.text();
26202
+ throw new Error(`setMyCommands failed (HTTP ${resp.status}): ${body}`);
26203
+ }
26204
+ const parsed = await resp.json().catch(() => null);
26205
+ if (parsed && parsed.ok === false) {
26206
+ throw new Error(`setMyCommands failed: ${parsed.description ?? "unknown error"}`);
26207
+ }
26208
+ }
26185
26209
  // Set (or, with a null emoji, clear) THE reaction on a message. A bot holds at
26186
26210
  // most one reaction per message, so this replaces whatever was there — which is
26187
26211
  // exactly why a read receipt overwrites the delivered one.
@@ -26664,7 +26688,16 @@ function emojiFor(state, s) {
26664
26688
  if (state === "read") return s.emojiRead;
26665
26689
  return null;
26666
26690
  }
26667
- var CMD_RE = /^\/(receipts|emoji)(?:@([A-Za-z0-9_]+))?$/i;
26691
+ var CMD_RE = /^\/(receipts|emoji|help)(?:@([A-Za-z0-9_]+))?$/i;
26692
+ var CONNECTOR_COMMANDS = [
26693
+ { command: "help", description: "What this connector handles here, and the current receipt settings" },
26694
+ { command: "id", description: "This chat's ids, for wiring up a new connection" },
26695
+ { command: "receipts", args: "on | off | status", description: "Delivery/read receipts, shown as reactions" },
26696
+ { command: "emoji", args: "delivered <emoji> | read <emoji> | reset", description: "Which emoji a receipt reacts with" }
26697
+ ];
26698
+ function telegramCommandList() {
26699
+ return CONNECTOR_COMMANDS.map((c) => ({ command: c.command, description: c.description }));
26700
+ }
26668
26701
  function invalidEmoji(input) {
26669
26702
  return {
26670
26703
  kind: "error",
@@ -26681,6 +26714,10 @@ function parseReceiptCommand(text, botUsername = "") {
26681
26714
  const [, verb, addressed] = m;
26682
26715
  if (addressed && botUsername && addressed.toLowerCase() !== botUsername.toLowerCase()) return null;
26683
26716
  const args = tokens.slice(1);
26717
+ if (verb.toLowerCase() === "help") {
26718
+ if (args.length) return null;
26719
+ return { kind: "help" };
26720
+ }
26684
26721
  if (verb.toLowerCase() === "receipts") {
26685
26722
  const a = (args[0] ?? "").toLowerCase();
26686
26723
  if (a === "on") return { kind: "receipts", on: true };
@@ -26699,7 +26736,7 @@ function parseReceiptCommand(text, botUsername = "") {
26699
26736
  if (!canon) return invalidEmoji(raw);
26700
26737
  return { kind: "emoji", slot, emoji: canon };
26701
26738
  }
26702
- function formatStatus(s, routeName, peerKnown) {
26739
+ function settingsLines(s, routeName, peerKnown) {
26703
26740
  const lines = [
26704
26741
  `Receipts for this connection ("${routeName}"):`,
26705
26742
  `\u2022 delivery/read reactions: ${s.receiptsEnabled ? "on" : "off"}`,
@@ -26707,9 +26744,23 @@ function formatStatus(s, routeName, peerKnown) {
26707
26744
  `\u2022 read: ${s.emojiRead}`
26708
26745
  ];
26709
26746
  if (!peerKnown) lines.push("\u2022 no agent connected yet \u2014 nothing to receive receipts from");
26747
+ return lines;
26748
+ }
26749
+ function formatStatus(s, routeName, peerKnown) {
26750
+ const lines = settingsLines(s, routeName, peerKnown);
26710
26751
  lines.push("", "Change with: /receipts on|off, /emoji delivered <emoji>, /emoji read <emoji>, /emoji reset");
26711
26752
  return lines.join("\n");
26712
26753
  }
26754
+ function formatHelp(settings, routeName, peerKnown) {
26755
+ const lines = ["Commands handled by this connector (answered here, never sent to the agent):"];
26756
+ for (const c of CONNECTOR_COMMANDS) {
26757
+ lines.push(`\u2022 /${c.command}${c.args ? ` ${c.args}` : ""} \u2014 ${c.description}`);
26758
+ }
26759
+ lines.push("", "Anything else you send is relayed to the agent exactly as typed \u2014 including text that starts with a slash.", "");
26760
+ if (settings) lines.push(...settingsLines(settings, routeName, peerKnown));
26761
+ else lines.push("This chat has no route yet, so there are no receipt settings to show. Run /id to get the ids needed to add one.");
26762
+ return lines.join("\n");
26763
+ }
26713
26764
 
26714
26765
  // src/msgmap.ts
26715
26766
  var RETENTION_DAYS = 30;
@@ -27318,13 +27369,25 @@ async function applyReceiptReactions(conn, senderCid, kind, wireIds) {
27318
27369
  }
27319
27370
  }
27320
27371
  }
27321
- async function handleReceiptCommand(conn, m) {
27322
- const cmd = parseReceiptCommand(m.text, conn.bot.username);
27372
+ async function handleConnectorCommand(bot, conn, m) {
27373
+ const cmd = parseReceiptCommand(m.text, bot.username);
27323
27374
  if (!cmd) return false;
27375
+ if (!conn) {
27376
+ if (cmd.kind !== "help") return false;
27377
+ try {
27378
+ await bot.tg.sendMessage(m.chat_id, formatHelp(null, "", false), m.thread_id);
27379
+ } catch (err) {
27380
+ log(`[bot ${botLabel(bot)}] failed to answer /help in unrouted chat ${m.chat_id}:`, String(err));
27381
+ }
27382
+ return true;
27383
+ }
27324
27384
  const { cfg } = conn;
27325
27385
  const cid = cfg.peerCid;
27326
27386
  let reply;
27327
27387
  switch (cmd.kind) {
27388
+ case "help":
27389
+ reply = formatHelp(conn.map.settingsFor(cid), cfg.name, !!cid);
27390
+ break;
27328
27391
  case "receipts": {
27329
27392
  const s = conn.map.updateSettings(cid, { receiptsEnabled: cmd.on });
27330
27393
  log(`[${cfg.name}] receipts turned ${cmd.on ? "on" : "off"} for this connection`);
@@ -27380,6 +27443,8 @@ async function onBotMessage(bot, m) {
27380
27443
  return;
27381
27444
  }
27382
27445
  const conn = resolveRoute(bot.exact, bot.catchAll, m.chat_id, m.thread_id);
27446
+ if (conn) conn.lastChat = { chatId: String(m.chat_id), threadId: m.thread_id ? String(m.thread_id) : "" };
27447
+ if (await handleConnectorCommand(bot, conn, m)) return;
27383
27448
  if (!conn) {
27384
27449
  const only = bot.exact.size === 1 && !bot.catchAll ? [...bot.exact.values()][0] : null;
27385
27450
  if (only && only.cfg.deniedMessage) {
@@ -27393,8 +27458,6 @@ async function onBotMessage(bot, m) {
27393
27458
  }
27394
27459
  return;
27395
27460
  }
27396
- conn.lastChat = { chatId: String(m.chat_id), threadId: m.thread_id ? String(m.thread_id) : "" };
27397
- if (await handleReceiptCommand(conn, m)) return;
27398
27461
  await forwardToNode(conn, m);
27399
27462
  }
27400
27463
  function activateRoute(conn) {
@@ -27469,6 +27532,15 @@ function activateRoute(conn) {
27469
27532
  function activateBot(bot) {
27470
27533
  if (bot.pollHandle) return;
27471
27534
  bot.pollHandle = bot.tg.poll((m) => onBotMessage(bot, m));
27535
+ void publishBotCommands(bot);
27536
+ }
27537
+ async function publishBotCommands(bot) {
27538
+ try {
27539
+ await bot.tg.setMyCommands(telegramCommandList());
27540
+ log(`[bot ${botLabel(bot)}] slash menu registered: ${telegramCommandList().map((c) => `/${c.command}`).join(" ")}`);
27541
+ } catch (err) {
27542
+ log(`[bot ${botLabel(bot)}] setMyCommands failed (commands still work when typed):`, String(err));
27543
+ }
27472
27544
  }
27473
27545
  var controlBusy = /* @__PURE__ */ new Set();
27474
27546
  async function processControlRequests(conn) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ours.network/tg-connector",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Bridge Telegram bots to ours proxy nodes — one ADAPT wrapper hosting N messenger packets, each connecting one Telegram bot to one ours node. Run `ours-tg-connector add_new_connection`.",
5
5
  "type": "module",
6
6
  "license": "FSL-1.1-Apache-2.0",