@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.21
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/dist/api.js +103 -0
- package/dist/cli.js +134 -565
- package/dist/control.js +102 -0
- package/dist/prompt.js +10 -0
- package/dist/runtime.js +94 -0
- package/dist/store.js +70 -0
- package/dist/update.js +33 -0
- package/dist/version.js +97 -0
- package/package.json +34 -22
- package/runtime/node_modules/@metro-labs/discord/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord/src/accounts.ts +119 -0
- package/runtime/node_modules/@metro-labs/discord/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord/src/format.ts +210 -0
- package/runtime/node_modules/@metro-labs/discord/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/package.json +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/inbound.ts +367 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +335 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-serve.ts +124 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +278 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +322 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +20 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +202 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +338 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +333 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/events.ts +117 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +164 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +208 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +357 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +79 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +162 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +120 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +58 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +134 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +214 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +224 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +125 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +41 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor.ts +323 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +169 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +295 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +71 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +37 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +147 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +19 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +364 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +300 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +131 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +145 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +203 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/index.ts +192 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +73 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session.ts +211 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-dispatch.ts +212 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +397 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +240 -0
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +80 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-resolve.ts +200 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach.ts +182 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +172 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +48 -0
- package/runtime/node_modules/@metro-labs/telegram/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/telegram-user/package.json +28 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +81 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +29 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +67 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +318 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +132 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +141 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +113 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +333 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +78 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +39 -0
- package/runtime/runtime.json +3 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/dist/tail.js +0 -161
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { accountFor, accounts, tg, targetOf } from './accounts.js';
|
|
2
|
+
import { respond } from './wire.js';
|
|
3
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
4
|
+
import { normalizeTelegram } from '@metro-labs/mcp/stations/messaging-normalize';
|
|
5
|
+
import {
|
|
6
|
+
adminMemberList,
|
|
7
|
+
inaccessibleMemberList,
|
|
8
|
+
type TgChatMember,
|
|
9
|
+
} from './members.js';
|
|
10
|
+
import {
|
|
11
|
+
makeStation,
|
|
12
|
+
type CallMsg,
|
|
13
|
+
type StationHandler,
|
|
14
|
+
} from '@metro-labs/mcp/stations/station-runtime';
|
|
15
|
+
import { mediaKindOf } from './attachments.js';
|
|
16
|
+
import { assertContentLength } from '@metro-labs/mcp/stations/attachments';
|
|
17
|
+
import {
|
|
18
|
+
emitOutbound,
|
|
19
|
+
finishSend,
|
|
20
|
+
media,
|
|
21
|
+
MEDIA_METHOD_FIELD,
|
|
22
|
+
sendDice,
|
|
23
|
+
sendLocation,
|
|
24
|
+
sendMedia,
|
|
25
|
+
} from './media-actions.js';
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export type { CallMsg };
|
|
31
|
+
|
|
32
|
+
const meCache = new Map<string, { id: number; username: string | null }>();
|
|
33
|
+
async function getMe(
|
|
34
|
+
accountId: string,
|
|
35
|
+
): Promise<{ id: number; username: string | null } | null> {
|
|
36
|
+
const cached = meCache.get(accountId);
|
|
37
|
+
if (cached) return cached;
|
|
38
|
+
try {
|
|
39
|
+
const me = await tg<{ id: number; username?: string }>(
|
|
40
|
+
accountId,
|
|
41
|
+
'getMe',
|
|
42
|
+
{},
|
|
43
|
+
10_000,
|
|
44
|
+
);
|
|
45
|
+
const v = { id: me.id, username: me.username ?? null };
|
|
46
|
+
meCache.set(accountId, v);
|
|
47
|
+
return v;
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function listAccounts(id: string): Promise<void> {
|
|
54
|
+
const list = await Promise.all(
|
|
55
|
+
[...accounts.values()].map(async (a) => {
|
|
56
|
+
const me = await getMe(a.cfg.id);
|
|
57
|
+
const username = me?.username ?? null;
|
|
58
|
+
return {
|
|
59
|
+
id: a.cfg.id,
|
|
60
|
+
handle: username === null ? null : `@${username}`,
|
|
61
|
+
url: username === null ? null : `https://t.me/${username}`,
|
|
62
|
+
owner: a.cfg.owner ?? null,
|
|
63
|
+
botId: me?.id ?? null,
|
|
64
|
+
username,
|
|
65
|
+
};
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
respond(id, { result: { accounts: list } });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface SendArgs {
|
|
72
|
+
line: string;
|
|
73
|
+
text: string;
|
|
74
|
+
replyTo?: string;
|
|
75
|
+
parseMode?: string;
|
|
76
|
+
buttons?: { text: string; url: string }[][];
|
|
77
|
+
account?: string;
|
|
78
|
+
attachments?: {
|
|
79
|
+
kind?: string;
|
|
80
|
+
path?: string;
|
|
81
|
+
url?: string;
|
|
82
|
+
mime?: string;
|
|
83
|
+
name?: string;
|
|
84
|
+
}[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
type WireAttachment = NonNullable<SendArgs['attachments']>[number];
|
|
89
|
+
|
|
90
|
+
const attachmentSrc = (att: WireAttachment): string | undefined =>
|
|
91
|
+
att.path ?? att.url;
|
|
92
|
+
|
|
93
|
+
const attachmentKind = (att: WireAttachment): string =>
|
|
94
|
+
mediaKindOf(att.kind, att.mime, attachmentSrc(att) ?? att.name);
|
|
95
|
+
|
|
96
|
+
async function sendOneAttachment(
|
|
97
|
+
a: SendArgs,
|
|
98
|
+
att: WireAttachment,
|
|
99
|
+
caption: string | undefined,
|
|
100
|
+
): Promise<{ accountId: string; message_id: number; kind: string }> {
|
|
101
|
+
const kind = attachmentKind(att);
|
|
102
|
+
const mf = MEDIA_METHOD_FIELD[kind];
|
|
103
|
+
if (!mf) throw new Error(`telegram cannot send a '${kind}' attachment`);
|
|
104
|
+
const sent = await sendMedia(mf.method, mf.field, {
|
|
105
|
+
line: a.line,
|
|
106
|
+
path: attachmentSrc(att),
|
|
107
|
+
caption,
|
|
108
|
+
replyTo: a.replyTo,
|
|
109
|
+
parseMode: a.parseMode,
|
|
110
|
+
account: a.account,
|
|
111
|
+
name: att.name,
|
|
112
|
+
});
|
|
113
|
+
return { ...sent, kind };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const captionLabel = (caption: string | undefined, kind: string): string =>
|
|
117
|
+
caption !== undefined && caption !== '' ? caption : `[${kind}]`;
|
|
118
|
+
|
|
119
|
+
async function sendAttachments(id: string, a: SendArgs): Promise<void> {
|
|
120
|
+
const { line, text, replyTo, attachments = [] } = a;
|
|
121
|
+
let last: { accountId: string; message_id: number } | undefined;
|
|
122
|
+
const delivered: string[] = [];
|
|
123
|
+
for (let i = 0; i < attachments.length; i++) {
|
|
124
|
+
const att = attachments[i];
|
|
125
|
+
if (!att) continue;
|
|
126
|
+
const caption = i === 0 ? text : undefined;
|
|
127
|
+
const sent = await sendOneAttachment(a, att, caption);
|
|
128
|
+
last = sent;
|
|
129
|
+
delivered.push(sent.kind);
|
|
130
|
+
emitOutbound(
|
|
131
|
+
sent.accountId,
|
|
132
|
+
line,
|
|
133
|
+
String(sent.message_id),
|
|
134
|
+
captionLabel(caption, sent.kind),
|
|
135
|
+
replyTo,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (!last) throw new Error('no attachments were sent');
|
|
139
|
+
respond(id, {
|
|
140
|
+
result: {
|
|
141
|
+
messageId: String(last.message_id),
|
|
142
|
+
account: last.accountId,
|
|
143
|
+
attachments: delivered,
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function send(id: string, args: Record<string, unknown>): Promise<void> {
|
|
149
|
+
const a = args as unknown as SendArgs;
|
|
150
|
+
if (a.attachments?.length) {
|
|
151
|
+
await sendAttachments(id, a);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const { line, text, replyTo, parseMode, buttons, account } = a;
|
|
155
|
+
const { accountId, chatId, topicId } = targetOf(line, account);
|
|
156
|
+
const body: Record<string, unknown> = { chat_id: chatId, text };
|
|
157
|
+
if (topicId !== undefined) body.message_thread_id = topicId;
|
|
158
|
+
if (replyTo) body.reply_parameters = { message_id: Number(replyTo) };
|
|
159
|
+
if (parseMode) body.parse_mode = parseMode;
|
|
160
|
+
if (buttons) body.reply_markup = { inline_keyboard: buttons };
|
|
161
|
+
const sent = await tg<{ message_id: number }>(accountId, 'sendMessage', body);
|
|
162
|
+
finishSend(id, accountId, line, String(sent.message_id), text, replyTo);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function react(id: string, args: Record<string, unknown>): Promise<void> {
|
|
166
|
+
const { line, messageId, emoji, account } = args as {
|
|
167
|
+
line: string;
|
|
168
|
+
messageId: string;
|
|
169
|
+
emoji: string;
|
|
170
|
+
account?: string;
|
|
171
|
+
};
|
|
172
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
173
|
+
await tg(accountId, 'setMessageReaction', {
|
|
174
|
+
chat_id: chatId,
|
|
175
|
+
message_id: Number(messageId),
|
|
176
|
+
reaction: emoji ? [{ type: 'emoji', emoji }] : [],
|
|
177
|
+
});
|
|
178
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function edit(id: string, args: Record<string, unknown>): Promise<void> {
|
|
182
|
+
const { line, messageId, text, parseMode, account } = args as {
|
|
183
|
+
line: string;
|
|
184
|
+
messageId: string;
|
|
185
|
+
text: string;
|
|
186
|
+
parseMode?: string;
|
|
187
|
+
account?: string;
|
|
188
|
+
};
|
|
189
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
190
|
+
await tg(accountId, 'editMessageText', {
|
|
191
|
+
chat_id: chatId,
|
|
192
|
+
message_id: Number(messageId),
|
|
193
|
+
text,
|
|
194
|
+
parse_mode: parseMode,
|
|
195
|
+
});
|
|
196
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function remove(id: string, args: Record<string, unknown>): Promise<void> {
|
|
200
|
+
const { line, messageId, account } = args as {
|
|
201
|
+
line: string;
|
|
202
|
+
messageId: string;
|
|
203
|
+
account?: string;
|
|
204
|
+
};
|
|
205
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
206
|
+
await tg(accountId, 'deleteMessage', {
|
|
207
|
+
chat_id: chatId,
|
|
208
|
+
message_id: Number(messageId),
|
|
209
|
+
});
|
|
210
|
+
respond(id, { result: { ok: true, account: accountId } });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
async function download(id: string, args: Record<string, unknown>): Promise<void> {
|
|
216
|
+
const {
|
|
217
|
+
fileId,
|
|
218
|
+
outDir = '/tmp',
|
|
219
|
+
account,
|
|
220
|
+
} = args as { fileId: string; outDir?: string; account?: string };
|
|
221
|
+
const accountId = accountFor({ account });
|
|
222
|
+
const acct = accounts.get(accountId);
|
|
223
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
224
|
+
const meta = await tg<{ file_path: string }>(accountId, 'getFile', {
|
|
225
|
+
file_id: fileId,
|
|
226
|
+
});
|
|
227
|
+
const res = await fetch(`${acct.fileApi}/${meta.file_path}`, {
|
|
228
|
+
signal: AbortSignal.timeout(60_000),
|
|
229
|
+
});
|
|
230
|
+
if (!res.ok) throw new Error(`telegram download ${res.status}`);
|
|
231
|
+
assertContentLength(res.headers.get('content-length'));
|
|
232
|
+
const data = await res.arrayBuffer();
|
|
233
|
+
const filename = meta.file_path.split('/').pop() ?? `${fileId}.bin`;
|
|
234
|
+
const path = `${outDir}/${Date.now()}-${filename}`;
|
|
235
|
+
await Bun.write(path, data);
|
|
236
|
+
respond(id, {
|
|
237
|
+
result: { path, fileSize: data.byteLength, account: accountId },
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function listMembers(
|
|
242
|
+
id: string,
|
|
243
|
+
args: Record<string, unknown>,
|
|
244
|
+
): Promise<void> {
|
|
245
|
+
const { line, account } = args as { line: string; account?: string };
|
|
246
|
+
const { accountId, chatId } = targetOf(line, account);
|
|
247
|
+
let admins: TgChatMember[];
|
|
248
|
+
try {
|
|
249
|
+
admins = await tg<TgChatMember[]>(accountId, 'getChatAdministrators', {
|
|
250
|
+
chat_id: chatId,
|
|
251
|
+
});
|
|
252
|
+
} catch (e) {
|
|
253
|
+
respond(id, { result: inaccessibleMemberList(errMsg(e)) });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
let total: number | null = null;
|
|
257
|
+
try {
|
|
258
|
+
total = await tg<number>(accountId, 'getChatMemberCount', {
|
|
259
|
+
chat_id: chatId,
|
|
260
|
+
});
|
|
261
|
+
} catch {
|
|
262
|
+
total = null;
|
|
263
|
+
}
|
|
264
|
+
respond(id, { result: adminMemberList(admins, total) });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const HANDLERS: Record<string, StationHandler> = {
|
|
268
|
+
accounts: (id) => listAccounts(id),
|
|
269
|
+
send,
|
|
270
|
+
react,
|
|
271
|
+
edit,
|
|
272
|
+
delete: remove,
|
|
273
|
+
listMembers,
|
|
274
|
+
send_photo: (id, args) =>
|
|
275
|
+
media(id, 'sendPhoto', 'photo', ((args.caption as string) ?? '') + ' [image]', args),
|
|
276
|
+
send_document: (id, args) =>
|
|
277
|
+
media(id, 'sendDocument', 'document', ((args.caption as string) ?? '') + ' [file]', args),
|
|
278
|
+
send_voice: (id, args) => media(id, 'sendVoice', 'voice', '[voice]', args),
|
|
279
|
+
send_sticker: (id, args) => media(id, 'sendSticker', 'sticker', '[sticker]', args),
|
|
280
|
+
send_dice: sendDice,
|
|
281
|
+
send_location: sendLocation,
|
|
282
|
+
download,
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
export const handleCall = makeStation({
|
|
286
|
+
handlers: HANDLERS,
|
|
287
|
+
normalize: normalizeTelegram,
|
|
288
|
+
preDispatch: (id, action) => {
|
|
289
|
+
if (action === 'read') {
|
|
290
|
+
respond(id, { error: "unsupported verb 'read' on telegram" });
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
return false;
|
|
294
|
+
},
|
|
295
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import {
|
|
2
|
+
saveBufferToCache,
|
|
3
|
+
assertContentLength,
|
|
4
|
+
} from '@metro-labs/mcp/stations/attachments';
|
|
5
|
+
import type { SavedAttachment } from '@metro-labs/mcp/stations/attachments';
|
|
6
|
+
import { tg, accounts } from './accounts.js';
|
|
7
|
+
import type { TgMsg } from './types.js';
|
|
8
|
+
|
|
9
|
+
export type { SavedAttachment };
|
|
10
|
+
|
|
11
|
+
function isOggRef(mime: string | undefined, ref: string | undefined): boolean {
|
|
12
|
+
const ext = ref?.split('?')[0]?.split('.').pop()?.toLowerCase();
|
|
13
|
+
return mime === 'audio/ogg' || ext === 'ogg' || ext === 'oga';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isVoiceRef(
|
|
17
|
+
kind: string | undefined,
|
|
18
|
+
mime: string | undefined,
|
|
19
|
+
ref: string | undefined,
|
|
20
|
+
): boolean {
|
|
21
|
+
const isOgg = isOggRef(mime, ref);
|
|
22
|
+
if (kind === 'voice') return true;
|
|
23
|
+
if (kind === 'audio') return isOgg;
|
|
24
|
+
return !kind && isOgg;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function mediaKindOf(
|
|
28
|
+
kind: string | undefined,
|
|
29
|
+
mime: string | undefined,
|
|
30
|
+
ref: string | undefined,
|
|
31
|
+
): 'image' | 'voice' | 'audio' | 'video' | 'document' {
|
|
32
|
+
const m = mime ?? '';
|
|
33
|
+
if (kind === 'image' || m.startsWith('image/')) return 'image';
|
|
34
|
+
if (isVoiceRef(kind, mime, ref)) return 'voice';
|
|
35
|
+
if (kind === 'video' || m.startsWith('video/')) return 'video';
|
|
36
|
+
if (kind === 'audio' || m.startsWith('audio/')) return 'audio';
|
|
37
|
+
return 'document';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface TgMediaRef {
|
|
41
|
+
fileId: string;
|
|
42
|
+
name?: string;
|
|
43
|
+
mime?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const MEDIA_EXTRACTORS: ((m: TgMsg) => TgMediaRef | null)[] = [
|
|
47
|
+
(m) => {
|
|
48
|
+
const largest = m.photo?.[m.photo.length - 1];
|
|
49
|
+
return largest ? { fileId: largest.file_id, mime: 'image/jpeg' } : null;
|
|
50
|
+
},
|
|
51
|
+
(m) =>
|
|
52
|
+
m.document?.file_id
|
|
53
|
+
? {
|
|
54
|
+
fileId: m.document.file_id,
|
|
55
|
+
name: m.document.file_name,
|
|
56
|
+
mime: m.document.mime_type,
|
|
57
|
+
}
|
|
58
|
+
: null,
|
|
59
|
+
(m) =>
|
|
60
|
+
m.video?.file_id
|
|
61
|
+
? {
|
|
62
|
+
fileId: m.video.file_id,
|
|
63
|
+
name: m.video.file_name,
|
|
64
|
+
mime: m.video.mime_type ?? 'video/mp4',
|
|
65
|
+
}
|
|
66
|
+
: null,
|
|
67
|
+
(m) =>
|
|
68
|
+
m.animation?.file_id
|
|
69
|
+
? {
|
|
70
|
+
fileId: m.animation.file_id,
|
|
71
|
+
name: m.animation.file_name,
|
|
72
|
+
mime: m.animation.mime_type,
|
|
73
|
+
}
|
|
74
|
+
: null,
|
|
75
|
+
(m) =>
|
|
76
|
+
m.audio?.file_id
|
|
77
|
+
? {
|
|
78
|
+
fileId: m.audio.file_id,
|
|
79
|
+
name: m.audio.file_name,
|
|
80
|
+
mime: m.audio.mime_type,
|
|
81
|
+
}
|
|
82
|
+
: null,
|
|
83
|
+
(m) =>
|
|
84
|
+
m.voice?.file_id
|
|
85
|
+
? { fileId: m.voice.file_id, mime: m.voice.mime_type ?? 'audio/ogg' }
|
|
86
|
+
: null,
|
|
87
|
+
(m) =>
|
|
88
|
+
m.sticker?.file_id
|
|
89
|
+
? { fileId: m.sticker.file_id, mime: 'image/webp' }
|
|
90
|
+
: null,
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
export function mediaRefOf(m: TgMsg): TgMediaRef | null {
|
|
94
|
+
for (const extract of MEDIA_EXTRACTORS) {
|
|
95
|
+
const ref = extract(m);
|
|
96
|
+
if (ref) return ref;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function saveTelegramMedia(
|
|
102
|
+
accountId: string,
|
|
103
|
+
ref: TgMediaRef,
|
|
104
|
+
messageId: string,
|
|
105
|
+
index = 0,
|
|
106
|
+
): Promise<SavedAttachment> {
|
|
107
|
+
const file = await tg<{ file_path?: string }>(accountId, 'getFile', {
|
|
108
|
+
file_id: ref.fileId,
|
|
109
|
+
});
|
|
110
|
+
if (!file.file_path)
|
|
111
|
+
throw new Error(`telegram getFile returned no file_path for ${ref.fileId}`);
|
|
112
|
+
const acct = accounts.get(accountId);
|
|
113
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
114
|
+
const res = await fetch(`${acct.fileApi}/${file.file_path}`, {
|
|
115
|
+
signal: AbortSignal.timeout(60_000),
|
|
116
|
+
});
|
|
117
|
+
if (!res.ok)
|
|
118
|
+
throw new Error(
|
|
119
|
+
`telegram file download ${res.status} for ${file.file_path}`,
|
|
120
|
+
);
|
|
121
|
+
assertContentLength(res.headers.get('content-length'));
|
|
122
|
+
const data = new Uint8Array(await res.arrayBuffer());
|
|
123
|
+
const saved = await saveBufferToCache(data, messageId, index, {
|
|
124
|
+
mime: ref.mime,
|
|
125
|
+
name: ref.name ?? file.file_path,
|
|
126
|
+
});
|
|
127
|
+
return { ...saved, name: ref.name };
|
|
128
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
2
|
+
import { accounts, lineOf } from './accounts.js';
|
|
3
|
+
import { mintId, SELF_URI } from './wire.js';
|
|
4
|
+
import { mediaRefOf, saveTelegramMedia } from './attachments.js';
|
|
5
|
+
import type { TgMsg, TgReaction, TgReactionCount } from './types.js';
|
|
6
|
+
|
|
7
|
+
export type { TgMsg, TgReaction, TgReactionCount };
|
|
8
|
+
|
|
9
|
+
function lineForMsg(
|
|
10
|
+
accountId: string,
|
|
11
|
+
m: TgMsg,
|
|
12
|
+
): { line: string; topicId?: number } {
|
|
13
|
+
const topicId = m.is_topic_message ? m.message_thread_id : undefined;
|
|
14
|
+
return { line: lineOf(accountId, m.chat.id, topicId), topicId };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function stickerTag(s: NonNullable<TgMsg['sticker']>): string {
|
|
18
|
+
const set = s.set_name ? ` · ${s.set_name}` : '';
|
|
19
|
+
return `[sticker${s.emoji ? ` ${s.emoji}` : ''}${set}]`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const TAG_EXTRACTORS: ((m: TgMsg) => string | null)[] = [
|
|
23
|
+
(m) => (m.photo?.length ? '[image]' : null),
|
|
24
|
+
(m) => (m.voice ? '[voice]' : null),
|
|
25
|
+
(m) => (m.audio ? `[audio: ${m.audio.file_name ?? 'audio'}]` : null),
|
|
26
|
+
(m) => (m.video ? `[video: ${m.video.file_name ?? 'video'}]` : null),
|
|
27
|
+
(m) => (m.animation ? `[gif: ${m.animation.file_name ?? 'gif'}]` : null),
|
|
28
|
+
(m) => (m.sticker ? stickerTag(m.sticker) : null),
|
|
29
|
+
(m) =>
|
|
30
|
+
m.document && !m.animation
|
|
31
|
+
? `[file: ${m.document.file_name ?? 'doc'}]`
|
|
32
|
+
: null,
|
|
33
|
+
(m) =>
|
|
34
|
+
m.location
|
|
35
|
+
? `[location: ${m.location.latitude}, ${m.location.longitude}]`
|
|
36
|
+
: null,
|
|
37
|
+
(m) => (m.dice ? `[dice ${m.dice.emoji} = ${m.dice.value}]` : null),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function projectText(m: TgMsg): string {
|
|
41
|
+
const tags = TAG_EXTRACTORS.map((f) => f(m)).filter(
|
|
42
|
+
(t): t is string => t !== null,
|
|
43
|
+
);
|
|
44
|
+
return [m.text ?? m.caption, ...tags].filter(Boolean).join(' ');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function envelope(accountId: string, m: TgMsg): Record<string, unknown> {
|
|
48
|
+
const { line } = lineForMsg(accountId, m);
|
|
49
|
+
return {
|
|
50
|
+
kind: 'inbound',
|
|
51
|
+
id: mintId(),
|
|
52
|
+
ts: new Date(m.date * 1000).toISOString(),
|
|
53
|
+
station: 'telegram',
|
|
54
|
+
line,
|
|
55
|
+
line_name: m.chat.title ?? m.chat.first_name ?? undefined,
|
|
56
|
+
from: `metro://telegram/${accountId}/user/${m.from?.id ?? 'unknown'}`,
|
|
57
|
+
from_name: m.from?.username ? `@${m.from.username}` : m.from?.first_name,
|
|
58
|
+
from_display_name: m.from?.first_name,
|
|
59
|
+
message_id: String(m.message_id),
|
|
60
|
+
text: projectText(m),
|
|
61
|
+
payload: m,
|
|
62
|
+
is_private: m.chat.type === 'private',
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function reactionEnvelope(
|
|
67
|
+
accountId: string,
|
|
68
|
+
r: TgReaction,
|
|
69
|
+
): Record<string, unknown> | null {
|
|
70
|
+
if (r.user?.is_bot) return null;
|
|
71
|
+
const newEmojis = r.new_reaction
|
|
72
|
+
.filter((x) => x.type === 'emoji')
|
|
73
|
+
.map((x) => x.emoji ?? '');
|
|
74
|
+
const oldEmojis = r.old_reaction
|
|
75
|
+
.filter((x) => x.type === 'emoji')
|
|
76
|
+
.map((x) => x.emoji ?? '');
|
|
77
|
+
const added = newEmojis.filter((e) => !oldEmojis.includes(e));
|
|
78
|
+
if (!added.length) return null;
|
|
79
|
+
return {
|
|
80
|
+
kind: 'react',
|
|
81
|
+
id: mintId(),
|
|
82
|
+
ts: new Date(r.date * 1000).toISOString(),
|
|
83
|
+
station: 'telegram',
|
|
84
|
+
line: lineOf(accountId, r.chat.id),
|
|
85
|
+
from: `metro://telegram/${accountId}/user/${r.user?.id ?? 'unknown'}`,
|
|
86
|
+
from_name: r.user?.username ? `@${r.user.username}` : r.user?.first_name,
|
|
87
|
+
from_display_name: r.user?.first_name,
|
|
88
|
+
message_id: String(r.message_id),
|
|
89
|
+
emoji: added[0],
|
|
90
|
+
event: { type: 'react', emoji: added[0], targetId: String(r.message_id) },
|
|
91
|
+
is_private: r.chat.type === 'private',
|
|
92
|
+
payload: r,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function reactionCountEnvelope(
|
|
97
|
+
accountId: string,
|
|
98
|
+
rc: TgReactionCount,
|
|
99
|
+
): Record<string, unknown> | null {
|
|
100
|
+
const emojiReactions = rc.reactions.filter(
|
|
101
|
+
(r) => r.type.type === 'emoji' && r.type.emoji,
|
|
102
|
+
);
|
|
103
|
+
if (!emojiReactions.length) return null;
|
|
104
|
+
const top = emojiReactions.reduce((a, b) =>
|
|
105
|
+
b.total_count > a.total_count ? b : a,
|
|
106
|
+
);
|
|
107
|
+
const emoji = top.type.emoji ?? '';
|
|
108
|
+
if (!emoji) return null;
|
|
109
|
+
return {
|
|
110
|
+
kind: 'react',
|
|
111
|
+
id: mintId(),
|
|
112
|
+
ts: new Date(rc.date * 1000).toISOString(),
|
|
113
|
+
station: 'telegram',
|
|
114
|
+
line: lineOf(accountId, rc.chat.id),
|
|
115
|
+
from: `metro://telegram/${accountId}/user/unknown`,
|
|
116
|
+
message_id: String(rc.message_id),
|
|
117
|
+
emoji,
|
|
118
|
+
event: { type: 'react', emoji, targetId: String(rc.message_id) },
|
|
119
|
+
is_private: rc.chat.type === 'private',
|
|
120
|
+
payload: rc,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function emitInbound(
|
|
125
|
+
emit: (e: unknown) => void,
|
|
126
|
+
accountId: string,
|
|
127
|
+
e: Record<string, unknown>,
|
|
128
|
+
): void {
|
|
129
|
+
const owner = accounts.get(accountId)?.cfg.owner;
|
|
130
|
+
const payload = {
|
|
131
|
+
...(e.payload as Record<string, unknown> | undefined),
|
|
132
|
+
account: accountId,
|
|
133
|
+
};
|
|
134
|
+
emit({ ...e, ...(owner ? { to: owner } : {}), account: accountId, payload });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function saveMediaAndEmit(
|
|
138
|
+
emit: (e: unknown) => void,
|
|
139
|
+
accountId: string,
|
|
140
|
+
m: TgMsg,
|
|
141
|
+
sourceEnvId: string,
|
|
142
|
+
): void {
|
|
143
|
+
const ref = mediaRefOf(m);
|
|
144
|
+
if (!ref) return;
|
|
145
|
+
const line = lineForMsg(accountId, m).line;
|
|
146
|
+
void saveTelegramMedia(accountId, ref, String(m.message_id), 0)
|
|
147
|
+
.then((saved) => {
|
|
148
|
+
emitInbound(emit, accountId, {
|
|
149
|
+
kind: 'inbound',
|
|
150
|
+
id: mintId(),
|
|
151
|
+
ts: new Date().toISOString(),
|
|
152
|
+
station: 'telegram',
|
|
153
|
+
line,
|
|
154
|
+
from: SELF_URI || `metro://telegram/${accountId}/self`,
|
|
155
|
+
text: `📎 saved: ${saved.path}`,
|
|
156
|
+
payload: {
|
|
157
|
+
contentType: 'attachmentSaved',
|
|
158
|
+
attachmentFor: sourceEnvId,
|
|
159
|
+
index: 0,
|
|
160
|
+
attachmentPath: saved.path,
|
|
161
|
+
localPath: saved.path,
|
|
162
|
+
mime: saved.mime,
|
|
163
|
+
name: saved.name,
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
})
|
|
167
|
+
.catch((err: unknown) =>
|
|
168
|
+
process.stderr.write(
|
|
169
|
+
`telegram media save failed: ${errMsg(err)}\n`,
|
|
170
|
+
),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
2
|
+
import { accounts, loadAccounts, tg, type Account } from './accounts.js';
|
|
3
|
+
import { emit } from './wire.js';
|
|
4
|
+
import {
|
|
5
|
+
emitInbound,
|
|
6
|
+
envelope,
|
|
7
|
+
reactionCountEnvelope,
|
|
8
|
+
reactionEnvelope,
|
|
9
|
+
saveMediaAndEmit,
|
|
10
|
+
type TgMsg,
|
|
11
|
+
type TgReaction,
|
|
12
|
+
type TgReactionCount,
|
|
13
|
+
} from './format.js';
|
|
14
|
+
import { readCalls } from '@metro-labs/mcp/trains/protocol';
|
|
15
|
+
import { handleCall } from './actions.js';
|
|
16
|
+
|
|
17
|
+
readCalls('telegram', handleCall);
|
|
18
|
+
|
|
19
|
+
interface Update {
|
|
20
|
+
update_id: number;
|
|
21
|
+
message?: TgMsg;
|
|
22
|
+
message_reaction?: TgReaction;
|
|
23
|
+
message_reaction_count?: TgReactionCount;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function handleUpdate(id: string, u: Update): void {
|
|
27
|
+
if (u.message && !u.message.from?.is_bot) {
|
|
28
|
+
const env = envelope(id, u.message);
|
|
29
|
+
emitInbound(emit, id, env);
|
|
30
|
+
saveMediaAndEmit(emit, id, u.message, env.id as string);
|
|
31
|
+
}
|
|
32
|
+
if (u.message_reaction) {
|
|
33
|
+
const env = reactionEnvelope(id, u.message_reaction);
|
|
34
|
+
if (env) emitInbound(emit, id, env);
|
|
35
|
+
}
|
|
36
|
+
if (u.message_reaction_count) {
|
|
37
|
+
const env = reactionCountEnvelope(id, u.message_reaction_count);
|
|
38
|
+
if (env) emitInbound(emit, id, env);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function runAccount(acct: Account): Promise<void> {
|
|
43
|
+
const { id } = acct.cfg;
|
|
44
|
+
try {
|
|
45
|
+
await tg(id, 'deleteWebhook', { drop_pending_updates: false });
|
|
46
|
+
} catch (err) {
|
|
47
|
+
process.stderr.write(
|
|
48
|
+
`telegram[${id}] deleteWebhook: ${errMsg(err)}\n`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (;;) {
|
|
53
|
+
try {
|
|
54
|
+
const updates = await tg<Update[]>(
|
|
55
|
+
id,
|
|
56
|
+
'getUpdates',
|
|
57
|
+
{
|
|
58
|
+
offset: acct.offset,
|
|
59
|
+
timeout: 25,
|
|
60
|
+
allowed_updates: [
|
|
61
|
+
'message',
|
|
62
|
+
'message_reaction',
|
|
63
|
+
'message_reaction_count',
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
60_000,
|
|
67
|
+
);
|
|
68
|
+
for (const u of updates) {
|
|
69
|
+
acct.offset = u.update_id + 1;
|
|
70
|
+
handleUpdate(id, u);
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
process.stderr.write(
|
|
74
|
+
`telegram[${id}] poll error: ${errMsg(err)}\n`,
|
|
75
|
+
);
|
|
76
|
+
await new Promise((r) => setTimeout(r, 2_000));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const cfgs = loadAccounts();
|
|
82
|
+
for (const cfg of cfgs) {
|
|
83
|
+
accounts.set(cfg.id, {
|
|
84
|
+
cfg,
|
|
85
|
+
api: `https://api.telegram.org/bot${cfg.token}`,
|
|
86
|
+
fileApi: `https://api.telegram.org/file/bot${cfg.token}`,
|
|
87
|
+
offset: 0,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (accounts.size === 0) {
|
|
91
|
+
process.stderr.write('telegram: no accounts booted, exiting\n');
|
|
92
|
+
process.exit(2);
|
|
93
|
+
}
|
|
94
|
+
process.stderr.write(
|
|
95
|
+
`telegram train ready (multi) — ${accounts.size} account(s): ${[...accounts.keys()].join(', ')}\n`,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
for (const acct of accounts.values())
|
|
99
|
+
runAccount(acct).catch((err: unknown) => {
|
|
100
|
+
process.stderr.write(
|
|
101
|
+
`telegram[${acct.cfg.id}] account loop failed: ${errMsg(err)}\n`,
|
|
102
|
+
);
|
|
103
|
+
});
|