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 {
|
private setupHandlers(): void {
|
||||||
// Start command
|
|
||||||
this.bot.start((ctx) => {
|
this.bot.start((ctx) => {
|
||||||
ctx.reply(
|
ctx.reply(
|
||||||
'Привет! Я бот для работы с AI пайплайнами. Просто отправь мне сообщение, и я обработаю его через пайплайн.',
|
'Привет! Я бот для работы с AI пайплайнами. Просто отправь мне сообщение, и я обработаю его через пайплайн.',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle text messages
|
|
||||||
this.bot.on('text', async (ctx) => {
|
this.bot.on('text', async (ctx) => {
|
||||||
const chatId = ctx.chat.id;
|
const chatId = ctx.chat.id;
|
||||||
const userMessage = ctx.message.text;
|
const userMessage = ctx.message.text;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create a simple pipeline with the user's message as the first step
|
|
||||||
const userPipeline: Pipeline = {
|
const userPipeline: Pipeline = {
|
||||||
description: 'User message processing pipeline',
|
description: 'User message processing pipeline',
|
||||||
systemPrompt:
|
systemPrompt: `Ты полезный ассистент, который всегда отвечает на русском языке.
|
||||||
'Ты полезный ассистент, который всегда отвечает на русском языке. Анализируй сообщения пользователя и давай развернутые ответы.',
|
Анализируй сообщения пользователя и давай развернутые ответы.
|
||||||
|
Твой ответ всегда должен быть отформатирован для telegram.`,
|
||||||
steps: [
|
steps: [
|
||||||
{
|
{
|
||||||
name: 'Start',
|
name: 'Start',
|
||||||
@@ -44,13 +42,10 @@ export class TelegramBot {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Store the pipeline for this chat
|
|
||||||
this.activePipelines.set(chatId, userPipeline);
|
this.activePipelines.set(chatId, userPipeline);
|
||||||
|
|
||||||
// Show typing action
|
|
||||||
await ctx.sendChatAction('typing');
|
await ctx.sendChatAction('typing');
|
||||||
|
|
||||||
// Execute the pipeline
|
|
||||||
const results = await this.pipelineExecutor.executeChain({
|
const results = await this.pipelineExecutor.executeChain({
|
||||||
pipeline: userPipeline,
|
pipeline: userPipeline,
|
||||||
variables: {},
|
variables: {},
|
||||||
@@ -58,7 +53,6 @@ export class TelegramBot {
|
|||||||
openAIApiKey: process.env.OPENAI_API_KEY || '',
|
openAIApiKey: process.env.OPENAI_API_KEY || '',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send the result back to the user
|
|
||||||
const lastResult = results[results.length - 1];
|
const lastResult = results[results.length - 1];
|
||||||
if (lastResult) {
|
if (lastResult) {
|
||||||
await ctx.reply(lastResult);
|
await ctx.reply(lastResult);
|
||||||
@@ -73,7 +67,6 @@ export class TelegramBot {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Error handling
|
|
||||||
this.bot.catch((error: unknown) => {
|
this.bot.catch((error: unknown) => {
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
error instanceof Error ? error.message : 'Unknown error';
|
error instanceof Error ? error.message : 'Unknown error';
|
||||||
@@ -85,7 +78,6 @@ export class TelegramBot {
|
|||||||
this.bot.launch();
|
this.bot.launch();
|
||||||
console.log('Telegram bot started');
|
console.log('Telegram bot started');
|
||||||
|
|
||||||
// Enable graceful stop
|
|
||||||
process.once('SIGINT', () => {
|
process.once('SIGINT', () => {
|
||||||
this.bot.stop('SIGINT');
|
this.bot.stop('SIGINT');
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
@@ -97,7 +89,6 @@ export class TelegramBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async cleanup(): Promise<void> {
|
private async cleanup(): Promise<void> {
|
||||||
// Cleanup resources
|
|
||||||
await this.pipelineExecutor.mcpToolsService.disconnect();
|
await this.pipelineExecutor.mcpToolsService.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,6 @@ async function main() {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize and start the Telegram bot
|
|
||||||
const bot = new TelegramBot(telegramToken, mcpServerUrl);
|
const bot = new TelegramBot(telegramToken, mcpServerUrl);
|
||||||
bot.launch();
|
bot.launch();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user