feat: Improve Telegram bot message handling and formatting
This commit is contained in:
1 parent
4b672efa41
commit
81a5c5ea25
2 files changed
+3
-13
No files matched your search
+3
-12
@@ -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<void> {
|
||||
// Cleanup resources
|
||||
await this.pipelineExecutor.mcpToolsService.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user