From 81a5c5ea253041eb0e5f3580241db9a5899e7018 Mon Sep 17 00:00:00 2001 From: uxname Date: Sat, 24 May 2025 17:24:12 +0300 Subject: [PATCH] feat: Improve Telegram bot message handling and formatting --- src/bot/telegram-bot.ts | 15 +++------------ src/main.ts | 1 - 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/bot/telegram-bot.ts b/src/bot/telegram-bot.ts index 694bd00..2fbdf6c 100644 --- a/src/bot/telegram-bot.ts +++ b/src/bot/telegram-bot.ts @@ -14,24 +14,22 @@ export class TelegramBot { } private setupHandlers(): void { - // Start command this.bot.start((ctx) => { ctx.reply( 'Привет! Я бот для работы с AI пайплайнами. Просто отправь мне сообщение, и я обработаю его через пайплайн.', ); }); - // Handle text messages this.bot.on('text', async (ctx) => { const chatId = ctx.chat.id; const userMessage = ctx.message.text; try { - // Create a simple pipeline with the user's message as the first step const userPipeline: Pipeline = { description: 'User message processing pipeline', - systemPrompt: - 'Ты полезный ассистент, который всегда отвечает на русском языке. Анализируй сообщения пользователя и давай развернутые ответы.', + systemPrompt: `Ты полезный ассистент, который всегда отвечает на русском языке. + Анализируй сообщения пользователя и давай развернутые ответы. + Твой ответ всегда должен быть отформатирован для telegram.`, steps: [ { name: 'Start', @@ -44,13 +42,10 @@ export class TelegramBot { ], }; - // Store the pipeline for this chat this.activePipelines.set(chatId, userPipeline); - // Show typing action await ctx.sendChatAction('typing'); - // Execute the pipeline const results = await this.pipelineExecutor.executeChain({ pipeline: userPipeline, variables: {}, @@ -58,7 +53,6 @@ export class TelegramBot { openAIApiKey: process.env.OPENAI_API_KEY || '', }); - // Send the result back to the user const lastResult = results[results.length - 1]; if (lastResult) { await ctx.reply(lastResult); @@ -73,7 +67,6 @@ export class TelegramBot { } }); - // Error handling this.bot.catch((error: unknown) => { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; @@ -85,7 +78,6 @@ export class TelegramBot { this.bot.launch(); console.log('Telegram bot started'); - // Enable graceful stop process.once('SIGINT', () => { this.bot.stop('SIGINT'); this.cleanup(); @@ -97,7 +89,6 @@ export class TelegramBot { } private async cleanup(): Promise { - // Cleanup resources await this.pipelineExecutor.mcpToolsService.disconnect(); } } diff --git a/src/main.ts b/src/main.ts index 4b71fad..f155528 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,7 +29,6 @@ async function main() { process.exit(1); } - // Initialize and start the Telegram bot const bot = new TelegramBot(telegramToken, mcpServerUrl); bot.launch();