import { tool } from '@strands-agents/sdk' import { z } from 'zod' /** Telegram Bot API — requires TELEGRAM_BOT_TOKEN from settings/env */ function getToken(): string | null { try { const raw = localStorage.getItem('careless-v2-settings') const s = raw ? JSON.parse(raw) : {} return s.telegramBotToken || null } catch { return null } } export const telegramSendTool = tool({ name: 'telegram_send', description: 'Send a Telegram message (requires TELEGRAM_BOT_TOKEN in settings)', inputSchema: z.object({ chatId: z.string().describe('Chat ID or @username'), text: z.string(), parseMode: z.enum(['HTML', 'Markdown', 'MarkdownV2']).optional(), }), callback: async (input) => { try { const token = getToken() if (!token) return JSON.stringify({ status: 'error', error: 'Set telegramBotToken in settings' }) const url = `https://api.telegram.org/bot${token}/sendMessage` const res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chat_id: input.chatId, text: input.text, parse_mode: input.parseMode }), }) const data = await res.json() return JSON.stringify({ status: res.ok ? 'success' : 'error', data }) } catch (err: unknown) { return JSON.stringify({ status: 'error', error: (err as Error).message }) } }, }) export const telegramGetUpdatesTool = tool({ name: 'telegram_get_updates', description: 'Poll Telegram updates (long poll). Returns recent messages to the bot.', inputSchema: z.object({ offset: z.number().optional(), limit: z.number().optional(), timeout: z.number().optional(), }), callback: async (input) => { try { const token = getToken() if (!token) return JSON.stringify({ status: 'error', error: 'Set telegramBotToken in settings' }) const url = `https://api.telegram.org/bot${token}/getUpdates?offset=${input.offset || 0}&limit=${input.limit || 100}&timeout=${input.timeout || 0}` const res = await fetch(url) const data = await res.json() return JSON.stringify({ status: res.ok ? 'success' : 'error', data }) } catch (err: unknown) { return JSON.stringify({ status: 'error', error: (err as Error).message }) } }, }) export const TELEGRAM_TOOLS = [telegramSendTool, telegramGetUpdatesTool]