Closed BLazzeD21 closed 11 months ago
что значит работает не асинхронно? напиши какое поведение получаешь и какое хочешь
что такое sessions
и LEXICON_EN
, menuKeyboard
The simple answer is just use webhook mode. In polling mode, the bot fetches up to 100 pending updates and waits for all of them to finish processing before fetching the next batch. In webhook mode, the bot receives one update per chat until you finish processing, regardless of how many chats there are.
LEXICON - это просто словарь со всеми репликами бота.
sessions - сonst sessions = {};
из index.js
menuKeyboard - клавиатура с изображения
Обработчик с командой start приведен для примера того, как обрабатывается команда в боте. Проблема заключается в том, что при использовании команд бота, которые обращаются к openai API, он не может обрабатывать другие команды ни от одного пользователя. Бот буквально зависает на одном пользователи и одном запросе.
LEXICON - это просто словарь со всеми репликами бота. sessions -
сonst sessions = {};
из index.js menuKeyboard - клавиатура с изображенияОбработчик с командой start приведен для примера того, как обрабатывается команда в боте. Проблема заключается в том, что при использовании команд бота, которые обращаются к openai API, он не может обрабатывать другие команды ни от одного пользователя. Бот буквально зависает на одном пользователи и одном запросе.
сделай замыкающую функцию внутри, которая будет не блочить твой поток (почитай что такое замыкания в Js)
либо есть еще 1 вариант, через rxjs
сделать подписку на получение сообщение, и пока он обрабатывает твое сообщение, у тебя будет 1 текст, а когда придет ответ ты сможешь изменить сообщение на сообщение с ответом, либо отослать новое
If you need it to respond to the same chat even while processing, you just need to unhandle the promise that takes time. For example:
getLLMAnswer().then(async response => {
await ctx.sendMessage(response);
}).catch(errorHandler);
You should not wait this. This method will cause three things you should be careful about:
.then
callback.If you need it to respond to the same chat even while processing, you just need to unhandle the promise that takes time. For example:
getLLMAnswer().then(async response => { await ctx.sendMessage(response); }).catch(errorHandler);
You should not wait this. This method will cause three things you should be careful about:
- Errors will no longer be caught by Telegraf because it's an unhandled promise. You should catch them yourself as shown above.
- session will close while your unhandled promise is processing. You should not try to access or write to ctx.session in the
.then
callback.- Telegraf will no longer limit how many requests you're handling in these unhandled promises. You should use a ratelimit plugin if needed.
I'm a little confused on how to apply this to my main handlers...
https://github.com/BLazzeD21/Node.js-ChatGPT-Bot/blob/master/src/handlers/openaiHandlers.js
For example:
openai.getImage(...).then(async image => {
if (imageUrl == '400') {
// ...test of the logic for this handler
}).catch(errorHandler);
Notice I'm not awaiting openai.getImage
.
Thank you all very much for your help! The last solution worked!
The bot does not work async, how can you either implement threads for different users or make it async?
The bot implements routers, but it's workaround:
index.js:
handlers/userHandlers.js: