update proj
This commit is contained in:
67
bot.js
67
bot.js
@@ -1,16 +1,18 @@
|
||||
const TelegramBot = require('node-telegram-bot-api');
|
||||
require('dotenv').config()
|
||||
import TelegramBot from "node-telegram-bot-api";
|
||||
import dotenv from "dotenv";
|
||||
import axios from "axios";
|
||||
|
||||
const axios = require('axios')
|
||||
import { UserRepository } from "./user.repository.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const tokenTg = process.env.TOKEN_TG;
|
||||
const tokenAi = process.env.TOKEN_AI;
|
||||
const accessId = process.env.SECRET_ID;
|
||||
|
||||
|
||||
const bot = new TelegramBot(tokenTg, { polling: true });
|
||||
|
||||
console.log('Бот запущен...');
|
||||
console.log("Бот запущен...");
|
||||
|
||||
|
||||
bot.onText(/\/info/, (msg)=>{
|
||||
@@ -23,14 +25,24 @@ bot.onText(/\/info/, (msg)=>{
|
||||
})
|
||||
|
||||
|
||||
bot.onText(/\/start/, (msg) => {
|
||||
bot.onText(/\/start/, async(msg) => {
|
||||
const chatId = msg.chat.id;
|
||||
const chatType = msg.chat.type
|
||||
const user = msg.from
|
||||
try{
|
||||
const createOrUpdateUser = await UserRepository.createOrUpdateUser({
|
||||
telegramId: user.id,
|
||||
username: user.username,
|
||||
chatId: chatId
|
||||
});
|
||||
}catch(err){
|
||||
console.error('Ошибка при создании или обновлении пользователя:', err);
|
||||
}
|
||||
bot.sendMessage(chatId, `Чат перезапущен, тип чата ${chatType}`);
|
||||
});
|
||||
|
||||
|
||||
bot.on('message', (msg) => {
|
||||
bot.on('message', async(msg) => {
|
||||
const chatId = msg.chat.id;
|
||||
|
||||
const user = msg.from
|
||||
@@ -50,7 +62,46 @@ bot.on('message', (msg) => {
|
||||
}
|
||||
|
||||
if (msg.text) {
|
||||
bot.sendMessage(chatId, `Ты написал: ${msg.text}`);
|
||||
//bot.sendMessage(chatId, `Ты написал: ${msg.text}`);
|
||||
try{
|
||||
const last_msg_context = await UserRepository.getContext(user.id) || ''
|
||||
console.log('last_msg_context:', last_msg_context);
|
||||
if(last_msg_context === ''){
|
||||
console.log('Новый пользователь, начинаем диалог с чистого листа');
|
||||
const response = await getAIResponse(msg.text, null);
|
||||
bot.sendMessage(chatId, response.message);
|
||||
await UserRepository.updateContext(user.id, response.id);
|
||||
}else{
|
||||
console.log('Продолжаем диалог, контекст найден');
|
||||
const response = await getAIResponse(msg.text, last_msg_context);
|
||||
bot.sendMessage(chatId, response.message);
|
||||
await UserRepository.updateContext(user.id, response.id);
|
||||
}
|
||||
}catch(err){
|
||||
console.error('Ошибка при обработке сообщения:', err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
async function getAIResponse(message, context) {
|
||||
try{
|
||||
const options = {
|
||||
method:'POST',
|
||||
url:`https://agent.timeweb.cloud/api/v1/cloud-ai/agents/${accessId}/call`,
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokenAi}`,
|
||||
'x-proxy-source': '',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data:{
|
||||
message: message,
|
||||
parent_message_id: context
|
||||
}
|
||||
}
|
||||
const response = await axios.request(options);
|
||||
return response.data;
|
||||
}catch(err){
|
||||
console.error('Ошибка при получении ответа от AI:', err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user