KeiSeiKit-1.0/_ts_packages/packages/telegram-adapter/src/client.ts
Parfii-bot c21943e40b feat(ts-packages): 6 TS packages — MCP server + 5 external-API adapters
Total 1465 LOC + 616 test LOC, 78/78 tests pass.
- @keisei/mcp-server (25 tests) — Rust-CLI bridge via execa, stdio+HTTP, HMAC auth, kei() meta-tool
- @keisei/telegram-adapter (16 tests) — grammy Bot, 7 tools
- @keisei/recall-adapter (8 tests) — Zoom via Recall.ai, 5 tools
- @keisei/grok-adapter (6 tests) — xAI OpenAI-compatible, 2 tools
- @keisei/gmail-adapter (11 tests) — googleapis OAuth2, 6 tools (new — LBM gap)
- @keisei/youtube-adapter (12 tests) — YouTube Data API v3, 5 tools (new — LBM gap)

RULE 0.2 exception #4 (TS for MCP/API layer documented in _ts_packages/README.md).
RULE 0.8 — env vars only (TELEGRAM_BOT_TOKEN, XAI_API_KEY, GMAIL_*, YOUTUBE_API_KEY).
Strict TypeScript: strict + exactOptionalPropertyTypes + noUncheckedIndexedAccess.
Genesis-scan clean (0 hits).
2026-04-22 12:45:19 +08:00

74 lines
2.7 KiB
TypeScript

// Thin wrapper over grammy's Bot class. One class = one responsibility:
// own the Bot instance, expose a narrow surface used by tool handlers.
import { Bot, InputFile } from "grammy";
import type { ContactRecord, GroupRecord } from "./types.js";
export interface TelegramClientConfig {
token: string;
}
export class TelegramClient {
private readonly bot: Bot;
private readonly contactsCache: Map<number, ContactRecord> = new Map();
private readonly groupsCache: Map<number, GroupRecord> = new Map();
constructor(cfg: TelegramClientConfig) {
if (!cfg.token) throw new Error("TELEGRAM_BOT_TOKEN is required");
this.bot = new Bot(cfg.token);
}
async status(): Promise<{ username: string; id: number }> {
const me = await this.bot.api.getMe();
return { username: me.username, id: me.id };
}
async chatInfo(chat: string | number): Promise<{ id: number; title: string; type: string }> {
const info = await this.bot.api.getChat(chat);
const title = "title" in info && info.title ? info.title :
"first_name" in info && info.first_name ? info.first_name : String(info.id);
return { id: info.id, title, type: info.type };
}
async sendText(chat: string | number, text: string): Promise<number> {
const msg = await this.bot.api.sendMessage(chat, text);
return msg.message_id;
}
async sendDocument(chat: string | number, filePath: string, caption?: string): Promise<number> {
const msg = await this.bot.api.sendDocument(chat, new InputFile(filePath), caption !== undefined ? { caption } : {});
return msg.message_id;
}
async sendPhoto(chat: string | number, filePath: string, caption?: string): Promise<number> {
const msg = await this.bot.api.sendPhoto(chat, new InputFile(filePath), caption !== undefined ? { caption } : {});
return msg.message_id;
}
async sendVideo(chat: string | number, filePath: string, caption?: string): Promise<number> {
const msg = await this.bot.api.sendVideo(chat, new InputFile(filePath), caption !== undefined ? { caption } : {});
return msg.message_id;
}
async sendVoice(chat: string | number, filePath: string, caption?: string): Promise<number> {
const msg = await this.bot.api.sendVoice(chat, new InputFile(filePath), caption !== undefined ? { caption } : {});
return msg.message_id;
}
listGroups(): GroupRecord[] {
return Array.from(this.groupsCache.values());
}
listContacts(): ContactRecord[] {
return Array.from(this.contactsCache.values());
}
// Test helpers to seed cache; kept internal via underscore prefix.
_seedContact(c: ContactRecord): void {
this.contactsCache.set(c.userId, c);
}
_seedGroup(g: GroupRecord): void {
this.groupsCache.set(g.chatId, g);
}
}