openclaw/src/channels/registry.ts
spiceoogway 84c1ab4d55 feat(telegram-gramjs): Phase 1 - User account adapter with tests and docs
Implements Telegram user account support via GramJS/MTProto (#937).

## What's New
- Complete GramJS channel adapter for user accounts (not bots)
- Interactive auth flow (phone → SMS → 2FA)
- Session persistence via StringSession
- DM and group message support
- Security policies (allowFrom, dmPolicy, groupPolicy)
- Multi-account configuration

## Files Added

### Core Implementation (src/telegram-gramjs/)
- auth.ts - Interactive authentication flow
- auth.test.ts - Auth flow tests (mocked)
- client.ts - GramJS TelegramClient wrapper
- config.ts - Config adapter for multi-account
- gateway.ts - Gateway adapter (poll/send)
- handlers.ts - Message conversion (GramJS → openclaw)
- handlers.test.ts - Message conversion tests
- setup.ts - CLI setup wizard
- types.ts - TypeScript type definitions
- index.ts - Module exports

### Configuration
- src/config/types.telegram-gramjs.ts - Config schema

### Plugin Extension
- extensions/telegram-gramjs/index.ts - Plugin registration
- extensions/telegram-gramjs/src/channel.ts - Channel plugin implementation
- extensions/telegram-gramjs/openclaw.plugin.json - Plugin manifest
- extensions/telegram-gramjs/package.json - Dependencies

### Documentation
- docs/channels/telegram-gramjs.md - Complete setup guide (14KB)
- GRAMJS-PHASE1-SUMMARY.md - Implementation summary

### Registry
- src/channels/registry.ts - Added telegram-gramjs to CHAT_CHANNEL_ORDER

## Test Coverage
-  Auth flow with phone/SMS/2FA (mocked)
-  Phone number validation
-  Session verification
-  Message conversion (DM, group, reply)
-  Session key routing
-  Command extraction
-  Edge cases (empty messages, special chars)

## Features Implemented (Phase 1)
-  User account authentication via MTProto
-  DM message send/receive
-  Group message send/receive
-  Reply context preservation
-  Security policies (pairing, allowlist)
-  Multi-account support
-  Session persistence
-  Command detection

## Next Steps (Phase 2)
- Media support (photos, videos, files)
- Voice messages and stickers
- Message editing and deletion
- Reactions
- Channel messages

## Documentation Highlights
- Getting API credentials from my.telegram.org
- Interactive setup wizard walkthrough
- DM and group policies configuration
- Multi-account examples
- Rate limits and troubleshooting
- Security best practices
- Migration guide from Bot API

Closes #937 (Phase 1)
2026-01-30 03:03:15 -05:00

191 lines
6.1 KiB
TypeScript

import type { ChannelMeta } from "./plugins/types.js";
import type { ChannelId } from "./plugins/types.js";
import { requireActivePluginRegistry } from "../plugins/runtime.js";
// Channel docking: add new core channels here (order + meta + aliases), then
// register the plugin in its extension entrypoint and keep protocol IDs in sync.
export const CHAT_CHANNEL_ORDER = [
"telegram",
"telegram-gramjs",
"whatsapp",
"discord",
"googlechat",
"slack",
"signal",
"imessage",
] as const;
export type ChatChannelId = (typeof CHAT_CHANNEL_ORDER)[number];
export const CHANNEL_IDS = [...CHAT_CHANNEL_ORDER] as const;
export const DEFAULT_CHAT_CHANNEL: ChatChannelId = "whatsapp";
export type ChatChannelMeta = ChannelMeta;
const WEBSITE_URL = "https://openclaw.ai";
const CHAT_CHANNEL_META: Record<ChatChannelId, ChannelMeta> = {
telegram: {
id: "telegram",
label: "Telegram",
selectionLabel: "Telegram (Bot API)",
detailLabel: "Telegram Bot",
docsPath: "/channels/telegram",
docsLabel: "telegram",
blurb: "simplest way to get started — register a bot with @BotFather and get going.",
systemImage: "paperplane",
selectionDocsPrefix: "",
selectionDocsOmitLabel: true,
selectionExtras: [WEBSITE_URL],
},
"telegram-gramjs": {
id: "telegram-gramjs",
label: "Telegram (User Account)",
selectionLabel: "Telegram (GramJS User Account)",
detailLabel: "Telegram User",
docsPath: "/channels/telegram-gramjs",
docsLabel: "telegram-gramjs",
blurb:
"user account via MTProto; access all chats including private groups (requires phone auth).",
systemImage: "paperplane.fill",
},
whatsapp: {
id: "whatsapp",
label: "WhatsApp",
selectionLabel: "WhatsApp (QR link)",
detailLabel: "WhatsApp Web",
docsPath: "/channels/whatsapp",
docsLabel: "whatsapp",
blurb: "works with your own number; recommend a separate phone + eSIM.",
systemImage: "message",
},
discord: {
id: "discord",
label: "Discord",
selectionLabel: "Discord (Bot API)",
detailLabel: "Discord Bot",
docsPath: "/channels/discord",
docsLabel: "discord",
blurb: "very well supported right now.",
systemImage: "bubble.left.and.bubble.right",
},
googlechat: {
id: "googlechat",
label: "Google Chat",
selectionLabel: "Google Chat (Chat API)",
detailLabel: "Google Chat",
docsPath: "/channels/googlechat",
docsLabel: "googlechat",
blurb: "Google Workspace Chat app with HTTP webhook.",
systemImage: "message.badge",
},
slack: {
id: "slack",
label: "Slack",
selectionLabel: "Slack (Socket Mode)",
detailLabel: "Slack Bot",
docsPath: "/channels/slack",
docsLabel: "slack",
blurb: "supported (Socket Mode).",
systemImage: "number",
},
signal: {
id: "signal",
label: "Signal",
selectionLabel: "Signal (signal-cli)",
detailLabel: "Signal REST",
docsPath: "/channels/signal",
docsLabel: "signal",
blurb: 'signal-cli linked device; more setup (David Reagans: "Hop on Discord.").',
systemImage: "antenna.radiowaves.left.and.right",
},
imessage: {
id: "imessage",
label: "iMessage",
selectionLabel: "iMessage (imsg)",
detailLabel: "iMessage",
docsPath: "/channels/imessage",
docsLabel: "imessage",
blurb: "this is still a work in progress.",
systemImage: "message.fill",
},
};
export const CHAT_CHANNEL_ALIASES: Record<string, ChatChannelId> = {
imsg: "imessage",
"google-chat": "googlechat",
gchat: "googlechat",
gramjs: "telegram-gramjs",
"telegram-user": "telegram-gramjs",
"telegram-mtproto": "telegram-gramjs",
};
const normalizeChannelKey = (raw?: string | null): string | undefined => {
const normalized = raw?.trim().toLowerCase();
return normalized || undefined;
};
export function listChatChannels(): ChatChannelMeta[] {
return CHAT_CHANNEL_ORDER.map((id) => CHAT_CHANNEL_META[id]);
}
export function listChatChannelAliases(): string[] {
return Object.keys(CHAT_CHANNEL_ALIASES);
}
export function getChatChannelMeta(id: ChatChannelId): ChatChannelMeta {
return CHAT_CHANNEL_META[id];
}
export function normalizeChatChannelId(raw?: string | null): ChatChannelId | null {
const normalized = normalizeChannelKey(raw);
if (!normalized) return null;
const resolved = CHAT_CHANNEL_ALIASES[normalized] ?? normalized;
return CHAT_CHANNEL_ORDER.includes(resolved as ChatChannelId)
? (resolved as ChatChannelId)
: null;
}
// Channel docking: prefer this helper in shared code. Importing from
// `src/channels/plugins/*` can eagerly load channel implementations.
export function normalizeChannelId(raw?: string | null): ChatChannelId | null {
return normalizeChatChannelId(raw);
}
// Normalizes registered channel plugins (bundled or external).
//
// Keep this light: we do not import channel plugins here (those are "heavy" and can pull in
// monitors, web login, etc). The plugin registry must be initialized first.
export function normalizeAnyChannelId(raw?: string | null): ChannelId | null {
const key = normalizeChannelKey(raw);
if (!key) return null;
const registry = requireActivePluginRegistry();
const hit = registry.channels.find((entry) => {
const id = String(entry.plugin.id ?? "")
.trim()
.toLowerCase();
if (id && id === key) return true;
return (entry.plugin.meta.aliases ?? []).some((alias) => alias.trim().toLowerCase() === key);
});
return (hit?.plugin.id as ChannelId | undefined) ?? null;
}
export function formatChannelPrimerLine(meta: ChatChannelMeta): string {
return `${meta.label}: ${meta.blurb}`;
}
export function formatChannelSelectionLine(
meta: ChatChannelMeta,
docsLink: (path: string, label?: string) => string,
): string {
const docsPrefix = meta.selectionDocsPrefix ?? "Docs:";
const docsLabel = meta.docsLabel ?? meta.id;
const docs = meta.selectionDocsOmitLabel
? docsLink(meta.docsPath)
: docsLink(meta.docsPath, docsLabel);
const extras = (meta.selectionExtras ?? []).filter(Boolean).join(" ");
return `${meta.label}${meta.blurb} ${docsPrefix ? `${docsPrefix} ` : ""}${docs}${extras ? ` ${extras}` : ""}`;
}