@ours.network/tg-connector 2.1.0 → 2.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 +3 -1
- package/dist/connector.js +61 -10
- package/docs/telegram-widgets.md +12 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -219,7 +219,9 @@ that agent as a `telegram_widget_query` JSON message (untrusted data). Discover
|
|
|
219
219
|
the commands with `list_contact_commands`. Ordinary agent text is never turned
|
|
220
220
|
into a widget. Typed command rows are never posted to Telegram; the drain
|
|
221
221
|
consumes them and the SDK returns each command's result. Production Telegram
|
|
222
|
-
supports widgets in private chats only.
|
|
222
|
+
supports widgets in private chats only. Widget commands are accepted only from
|
|
223
|
+
the route's recorded agent, which the route takes from its contact list when it
|
|
224
|
+
has exactly one contact. See
|
|
223
225
|
[docs/telegram-widgets.md](docs/telegram-widgets.md) and
|
|
224
226
|
[examples/widget-choice.html](examples/widget-choice.html).
|
|
225
227
|
|
package/dist/connector.js
CHANGED
|
@@ -27977,6 +27977,10 @@ var WidgetStore = class {
|
|
|
27977
27977
|
return "claimed";
|
|
27978
27978
|
}
|
|
27979
27979
|
};
|
|
27980
|
+
async function routeForCommand(deps) {
|
|
27981
|
+
if (!deps.route().peerCid && deps.ensureBound) await deps.ensureBound("command");
|
|
27982
|
+
return deps.route();
|
|
27983
|
+
}
|
|
27980
27984
|
var stateSchema = { type: "string", maxLength: WIDGET_STATE_MAX_BYTES };
|
|
27981
27985
|
var SEND_SCHEMA = {
|
|
27982
27986
|
type: "object",
|
|
@@ -28036,7 +28040,7 @@ function recordResult(rec, extra = {}) {
|
|
|
28036
28040
|
};
|
|
28037
28041
|
}
|
|
28038
28042
|
async function sendWidgetCommand(deps, args, ctx) {
|
|
28039
|
-
const route = deps
|
|
28043
|
+
const route = await routeForCommand(deps);
|
|
28040
28044
|
const denied = authorize(route, ctx);
|
|
28041
28045
|
if (denied) return denied;
|
|
28042
28046
|
const o = asObject(args);
|
|
@@ -28119,7 +28123,7 @@ async function sendWidgetCommand(deps, args, ctx) {
|
|
|
28119
28123
|
return recordResult(rec);
|
|
28120
28124
|
}
|
|
28121
28125
|
async function updateWidgetCommand(deps, args, ctx) {
|
|
28122
|
-
const route = deps
|
|
28126
|
+
const route = await routeForCommand(deps);
|
|
28123
28127
|
const denied = authorize(route, ctx);
|
|
28124
28128
|
if (denied) return denied;
|
|
28125
28129
|
const o = asObject(args);
|
|
@@ -28906,6 +28910,47 @@ function singleFlight(task) {
|
|
|
28906
28910
|
return Object.assign(run, { idle: () => active ?? Promise.resolve() });
|
|
28907
28911
|
}
|
|
28908
28912
|
|
|
28913
|
+
// src/proxy-binding.ts
|
|
28914
|
+
var CONTAINER_ID = /^[0-9A-F]{64}$/;
|
|
28915
|
+
function makeProxyBinder(deps) {
|
|
28916
|
+
let chain = Promise.resolve();
|
|
28917
|
+
const evaluate = async (reason) => {
|
|
28918
|
+
if (deps.current()) return "kept";
|
|
28919
|
+
let contacts;
|
|
28920
|
+
try {
|
|
28921
|
+
contacts = await deps.listContacts();
|
|
28922
|
+
} catch (err) {
|
|
28923
|
+
deps.log(`proxy agent not bound (${reason}): listing contacts failed: ${String(err)}`);
|
|
28924
|
+
return "error";
|
|
28925
|
+
}
|
|
28926
|
+
if (deps.current()) return "kept";
|
|
28927
|
+
if (contacts.length === 0) return "none";
|
|
28928
|
+
if (contacts.length > 1) {
|
|
28929
|
+
deps.log(`proxy agent not bound (${reason}): the route has ${contacts.length} contacts; widgets stay unavailable until exactly one remains`);
|
|
28930
|
+
return "ambiguous";
|
|
28931
|
+
}
|
|
28932
|
+
const [only] = contacts;
|
|
28933
|
+
if (typeof only.container_id !== "string" || !CONTAINER_ID.test(only.container_id)) {
|
|
28934
|
+
deps.log(`proxy agent not bound (${reason}): the only contact has a malformed container id`);
|
|
28935
|
+
return "invalid";
|
|
28936
|
+
}
|
|
28937
|
+
try {
|
|
28938
|
+
deps.persist(only.container_id);
|
|
28939
|
+
} catch (err) {
|
|
28940
|
+
deps.log(`proxy agent not bound (${reason}): saving the binding failed: ${String(err)}`);
|
|
28941
|
+
return "error";
|
|
28942
|
+
}
|
|
28943
|
+
deps.commit(only.container_id);
|
|
28944
|
+
deps.log(`proxy agent bound (${reason}): "${only.name}" (${only.container_id})`);
|
|
28945
|
+
return "bound";
|
|
28946
|
+
};
|
|
28947
|
+
return (reason) => {
|
|
28948
|
+
const run = chain.then(() => evaluate(reason));
|
|
28949
|
+
chain = run.catch(() => void 0);
|
|
28950
|
+
return run;
|
|
28951
|
+
};
|
|
28952
|
+
}
|
|
28953
|
+
|
|
28909
28954
|
// src/connector.ts
|
|
28910
28955
|
var CONFIG = loadConfig();
|
|
28911
28956
|
var STATE_DIR = CONFIG.stateDir;
|
|
@@ -28929,11 +28974,21 @@ function makeConnection(client, dir, cfg, bot) {
|
|
|
28929
28974
|
drainMessages: singleFlight(() => forwardToTelegram(conn)),
|
|
28930
28975
|
drainFiles: singleFlight(() => forwardFilesToTelegram(conn)),
|
|
28931
28976
|
syncReceipts: singleFlight(() => syncReceiptHistory(conn)),
|
|
28977
|
+
bindProxy: makeProxyBinder({
|
|
28978
|
+
current: () => cfg.peerCid,
|
|
28979
|
+
listContacts: async () => (await client.listContacts()).contacts,
|
|
28980
|
+
persist: (peerCid) => writeMeta(dir, { ...cfg, peerCid }),
|
|
28981
|
+
commit: (peerCid) => {
|
|
28982
|
+
cfg.peerCid = peerCid;
|
|
28983
|
+
},
|
|
28984
|
+
log: (m2) => log(`[${cfg.name}] ${m2}`)
|
|
28985
|
+
}),
|
|
28932
28986
|
widgets: {
|
|
28933
28987
|
route: () => ({ name: cfg.name, chatId: cfg.chatId, threadId: cfg.threadId, peerCid: cfg.peerCid }),
|
|
28934
28988
|
tg: bot.tg,
|
|
28935
28989
|
store: new WidgetStore(dir, (m2) => log(`[${cfg.name}] ${m2}`)),
|
|
28936
|
-
log: (m2) => log(m2)
|
|
28990
|
+
log: (m2) => log(m2),
|
|
28991
|
+
ensureBound: (reason) => conn.bindProxy(reason)
|
|
28937
28992
|
},
|
|
28938
28993
|
watch: null,
|
|
28939
28994
|
watchHandle: null,
|
|
@@ -29172,13 +29227,8 @@ function watchRoute(conn) {
|
|
|
29172
29227
|
} else if (event === "contact_accepted" || event === "sibling_contact_added" || event === "local_contact_added") {
|
|
29173
29228
|
const name = String(ev.from ?? ev.name ?? "");
|
|
29174
29229
|
const cid = String(ev.container_id ?? ev.cid ?? "");
|
|
29175
|
-
|
|
29176
|
-
|
|
29177
|
-
writeMeta(conn.dir, cfg);
|
|
29178
|
-
log(`[${cfg.name}] proxy agent connected: "${name}" (${cid})`);
|
|
29179
|
-
} else {
|
|
29180
|
-
log(`[${cfg.name}] contact added: "${name}" (${cid})`);
|
|
29181
|
-
}
|
|
29230
|
+
log(`[${cfg.name}] contact added: "${name}" (${cid})`);
|
|
29231
|
+
await conn.bindProxy(event);
|
|
29182
29232
|
} else if (event === "contact_restored") {
|
|
29183
29233
|
log(`[${cfg.name}] contact "${String(ev.from ?? "")}" restored (re-keyed)`);
|
|
29184
29234
|
}
|
|
@@ -29599,6 +29649,7 @@ async function restoreConnection(name) {
|
|
|
29599
29649
|
connections.set(name, conn);
|
|
29600
29650
|
const conflict = registerRoute(conn);
|
|
29601
29651
|
if (conflict) throw new Error(conflict);
|
|
29652
|
+
await conn.bindProxy("restore");
|
|
29602
29653
|
await registerWidgetCommands(conn);
|
|
29603
29654
|
watchRoute(conn);
|
|
29604
29655
|
await Promise.all([conn.drainMessages(), conn.drainFiles(), conn.syncReceipts()]);
|
package/docs/telegram-widgets.md
CHANGED
|
@@ -50,9 +50,18 @@ A route offers widgets only when it is pinned to **one private chat**: a positiv
|
|
|
50
50
|
chat id with no topic. Catch-all, group and topic routes advertise no widget
|
|
51
51
|
commands, so `send_command` reports `unsupported_peer`.
|
|
52
52
|
|
|
53
|
-
Only the route's connected agent can use them. That is the
|
|
54
|
-
|
|
55
|
-
argument.
|
|
53
|
+
Only the route's connected agent can use them. That is the route's recorded
|
|
54
|
+
proxy agent (`peerCid`), and the command's sender must be that agent as
|
|
55
|
+
authenticated by ours, never by a name or an argument.
|
|
56
|
+
|
|
57
|
+
A route records its agent once, from the daemon's authenticated contact list, and
|
|
58
|
+
only when the route has **exactly one** contact. It checks when the route starts,
|
|
59
|
+
when a contact is added, and when a widget command arrives while no agent is
|
|
60
|
+
recorded. This also covers routes created before the binding was recorded. A
|
|
61
|
+
recorded agent is never replaced by later contacts. If the route has no contact or
|
|
62
|
+
several, or the contact list cannot be read, or the binding cannot be saved, no
|
|
63
|
+
agent is recorded and widget commands answer `not_authorized`; ordinary messages
|
|
64
|
+
are unaffected. The connector log names the reason (`proxy agent not bound ...`).
|
|
56
65
|
|
|
57
66
|
## Agent usage
|
|
58
67
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ours.network/tg-connector",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Bridge Telegram bots to ours.network agents \u2014 attaches to an already-running ours daemon and bridges one Telegram chat/topic to one agent per route. Run `ours-tg-connector add_new_connection`.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "FSL-1.1-Apache-2.0",
|
|
@@ -47,6 +47,6 @@
|
|
|
47
47
|
"esbuild": "^0.24.2",
|
|
48
48
|
"tsx": "^4.19.0",
|
|
49
49
|
"typescript": "^5.5.4",
|
|
50
|
-
"@ours.network/daemon": "5.0
|
|
50
|
+
"@ours.network/daemon": "5.2.0"
|
|
51
51
|
}
|
|
52
52
|
}
|