- New client: src/clients/telegram.ts
- Tools: telegram_get_me, telegram_send_message, telegram_send_photo, telegram_get_updates, telegram_get_chat
- REST endpoints: GET /api/telegram/me, POST /api/telegram/message, POST /api/telegram/photo, GET /api/telegram/updates, GET /api/telegram/chat
- Multi-account env var pattern: TELEGRAM_{ACCOUNT}_BOT_TOKEN
- Uses Telegram Bot API (https://api.telegram.org)
Total tools: 24 (email 6, obsidian 5, whatsapp 3, linkedin 4, telegram 5)
130 lines
3.3 KiB
TypeScript
130 lines
3.3 KiB
TypeScript
const TELEGRAM_API_BASE = 'https://api.telegram.org';
|
|
|
|
function getToken(account: string): string {
|
|
const envKey = `TELEGRAM_${account.toUpperCase()}_BOT_TOKEN`;
|
|
return process.env[envKey] ?? '';
|
|
}
|
|
|
|
async function telegramRequest(
|
|
token: string,
|
|
method: string,
|
|
params?: Record<string, unknown>
|
|
) {
|
|
const url = `${TELEGRAM_API_BASE}/bot${token}/${method}`;
|
|
const res = await fetch(url, {
|
|
method: params ? 'POST' : 'GET',
|
|
headers: params ? { 'Content-Type': 'application/json' } : undefined,
|
|
body: params ? JSON.stringify(params) : undefined,
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (!data.ok) {
|
|
throw new Error(`Telegram API error: ${data.description ?? 'Unknown error'}`);
|
|
}
|
|
return data.result;
|
|
}
|
|
|
|
export async function getMe(args: { account?: string }): Promise<{
|
|
id: number;
|
|
first_name: string;
|
|
username: string;
|
|
is_bot: boolean;
|
|
}> {
|
|
const token = getToken(args.account ?? 'default');
|
|
if (!token) {
|
|
throw new Error('Missing Telegram credentials. Set TELEGRAM_{ACCOUNT}_BOT_TOKEN');
|
|
}
|
|
return telegramRequest(token, 'getMe');
|
|
}
|
|
|
|
export async function sendMessage(args: {
|
|
chat_id: string | number;
|
|
text: string;
|
|
parse_mode?: 'HTML' | 'Markdown' | 'MarkdownV2';
|
|
account?: string;
|
|
}): Promise<{ message_id: number; chat_id: string | number }> {
|
|
const token = getToken(args.account ?? 'default');
|
|
if (!token) {
|
|
throw new Error('Missing Telegram credentials. Set TELEGRAM_{ACCOUNT}_BOT_TOKEN');
|
|
}
|
|
|
|
const result = await telegramRequest(token, 'sendMessage', {
|
|
chat_id: args.chat_id,
|
|
text: args.text,
|
|
parse_mode: args.parse_mode,
|
|
});
|
|
|
|
return {
|
|
message_id: result.message_id,
|
|
chat_id: result.chat.id,
|
|
};
|
|
}
|
|
|
|
export async function sendPhoto(args: {
|
|
chat_id: string | number;
|
|
photo: string;
|
|
caption?: string;
|
|
account?: string;
|
|
}): Promise<{ message_id: number; chat_id: string | number }> {
|
|
const token = getToken(args.account ?? 'default');
|
|
if (!token) {
|
|
throw new Error('Missing Telegram credentials. Set TELEGRAM_{ACCOUNT}_BOT_TOKEN');
|
|
}
|
|
|
|
const result = await telegramRequest(token, 'sendPhoto', {
|
|
chat_id: args.chat_id,
|
|
photo: args.photo,
|
|
caption: args.caption,
|
|
});
|
|
|
|
return {
|
|
message_id: result.message_id,
|
|
chat_id: result.chat.id,
|
|
};
|
|
}
|
|
|
|
export async function getUpdates(args: {
|
|
limit?: number;
|
|
account?: string;
|
|
}): Promise<Array<{
|
|
update_id: number;
|
|
message?: {
|
|
message_id: number;
|
|
from?: { id: number; first_name: string; username?: string };
|
|
chat: { id: number; type: string; title?: string };
|
|
date: number;
|
|
text?: string;
|
|
};
|
|
}>> {
|
|
const token = getToken(args.account ?? 'default');
|
|
if (!token) {
|
|
throw new Error('Missing Telegram credentials. Set TELEGRAM_{ACCOUNT}_BOT_TOKEN');
|
|
}
|
|
|
|
return telegramRequest(token, 'getUpdates', {
|
|
limit: args.limit ?? 10,
|
|
});
|
|
}
|
|
|
|
export async function getChat(args: {
|
|
chat_id: string | number;
|
|
account?: string;
|
|
}): Promise<{
|
|
id: number;
|
|
type: string;
|
|
title?: string;
|
|
username?: string;
|
|
description?: string;
|
|
member_count?: number;
|
|
}> {
|
|
const token = getToken(args.account ?? 'default');
|
|
if (!token) {
|
|
throw new Error('Missing Telegram credentials. Set TELEGRAM_{ACCOUNT}_BOT_TOKEN');
|
|
}
|
|
|
|
return telegramRequest(token, 'getChat', {
|
|
chat_id: args.chat_id,
|
|
});
|
|
}
|