Skip to content

Commit f94d212

Browse files
committed
improved commands & added direct communication with AI
1 parent d56ccfe commit f94d212

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

lib/chatbot.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ class ChatBot {
3535
const prediction = await this.api.ai.predict({
3636
form: ctx.session.form,
3737
language: this.lang.name, // Use the ChatBot's prop for language
38+
token: ctx.from.id,
3839
});
3940
await ctx.reply(prediction.answer);
40-
return ctx.scene.leave();
41+
return await ctx.reply(ctx.session.lang.furtherQuestion);
4142
}
4243
const question = ctx.session.actualField;
4344
if (question.type === 'text' || question.type === 'input') {
@@ -56,17 +57,16 @@ class ChatBot {
5657
}
5758

5859
async handleField(ctx) {
59-
// think about how to remove it from here -> cohesion
60-
if (ctx.message.text === '/stop') return ctx.scene.leave();
61-
if (ctx.message.text === '/language') {
62-
await ctx.scene.leave();
63-
// cohesion
64-
const languagesMarkups = Object.keys(languages).map((name) =>
65-
Markup.button.callback(name, name),
66-
);
67-
ctx.reply('Choose', Markup.inlineKeyboard(languagesMarkups).oneTime());
68-
return true;
60+
if (ctx.session.test.length === 0) {
61+
const prediction = await this.api.ai.ask({
62+
question: ctx.message.text,
63+
language: this.lang.name, // Use the ChatBot's prop for language
64+
token: ctx.from.id,
65+
});
66+
await ctx.reply(prediction.answer);
67+
return;
6968
}
69+
7070
const answer = ctx.message.text;
7171
const field = ctx.session.actualField;
7272
const question = ctx.session.test[0];
@@ -76,20 +76,23 @@ class ChatBot {
7676
ctx.session.test.shift();
7777
ctx.session.actualField = ctx.session.test[0];
7878
await this.showQuestion(ctx);
79-
return true;
79+
return;
8080
}
8181
// only 2 types of questions
8282
if (field.type === 'choice') {
8383
const choice = Object.values(field.choices).find(
8484
(c) => c.name === answer,
8585
);
86-
if (!choice) return await ctx.reply(this.lang.choose_options);
86+
if (!choice) {
87+
await ctx.reply(this.lang.choose_options);
88+
return;
89+
}
8790
if (choice.type === 'text') {
8891
ctx.session.form += question.name + ': ' + choice.name + '\n';
8992
ctx.session.test.shift();
9093
ctx.session.actualField = ctx.session.test[0];
9194
await this.showQuestion(ctx);
92-
return true;
95+
return;
9396
}
9497
// for input and choice types
9598
await ctx.reply(choice.description);
@@ -101,9 +104,9 @@ class ChatBot {
101104
ctx.session.test.shift();
102105
ctx.session.actualField = ctx.session.test[0];
103106
await this.showQuestion(ctx);
104-
return true;
107+
return;
105108
}
106-
return false;
109+
return;
107110
}
108111
}
109112
module.exports = ChatBot;

main.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const API_URL = process.env.API_URL;
1010
const endpoints = {
1111
ai: {
1212
predict: 'post',
13+
ask: 'post',
14+
leave: 'post',
1315
},
1416
};
1517

@@ -30,15 +32,25 @@ const endpoints = {
3032
// eslint-disable-next-line camelcase
3133
const options = { reply_markup: { remove_keyboard: true } };
3234

35+
const stage = new Scenes.Stage([formScene]);
3336
formScene.enter(async (ctx) => await chatBot.start(ctx));
3437
const listener = async (ctx) => await chatBot.handleField(ctx);
3538
formScene.on('text', listener);
3639
formScene.on('message', listener);
3740
formScene.leave(async (ctx) => {
41+
ctx.session.test = [];
42+
ctx.session.form = '';
43+
await api.ai.leave({ token: ctx.from.id });
3844
await ctx.reply(ctx.session.lang.leave, options);
3945
await ctx.reply(ctx.session.lang.help);
4046
});
41-
const stage = new Scenes.Stage([formScene]);
47+
stage.command('stop', async (ctx) => await ctx.scene.leave());
48+
stage.command('language', async (ctx) => {
49+
const languagesMarkups = Object.keys(languages).map((name) =>
50+
Markup.button.callback(name, name),
51+
);
52+
ctx.reply('Choose', Markup.inlineKeyboard(languagesMarkups).oneTime());
53+
});
4254

4355
const logStatics = () => {
4456
console.clear();
@@ -95,6 +107,7 @@ const endpoints = {
95107
bot.action(lang, async (ctx) => {
96108
ctx.session.lang = languages[lang];
97109
await ctx.reply(ctx.session.lang.done);
110+
if (ctx.session.test && ctx.session.test.length) await chatBot.start(ctx);
98111
});
99112
});
100113

static/languages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"english": {
3+
"furtherQuestion": "Ask me whatever you want if you would like to know more about your future or have some tips! 😉 \n Or you can leave this dialog using /stop",
34
"name": "english",
45
"publicity": "Your amazing ad could be here! 🌟",
56
"welcome": "Hi there! We're thrilled to have you with us! 😊",
@@ -14,6 +15,7 @@
1415
"getHelp": "Here is how we can help you: \n\n1. /start - Start the conversation \n2. /stop - Exit the conversation \n3. /language - Change the language \n4. /help - Get help"
1516
},
1617
"russian": {
18+
"furtherQuestion": "Ты так же можешь задать мне любой вопрос о твоем будущем, а так получить советы или мотивацию! 😉 \n Или ты можешь просто покинуть этот диалог /stop",
1719
"name": "russian",
1820
"publicity": "Ваша реклама могла бы быть здесь! 🌟",
1921
"welcome": "Привет! Мы рады, что вы с нами! 😊",
@@ -28,6 +30,7 @@
2830
"getHelp": "Здесь все, что может тебе помочь: \n\n1. /start - Начать разговор \n2. /stop - Завершить разговор \n3. /language - Изменить язык \n4. /help - Получить помощь"
2931
},
3032
"ukrainian": {
33+
"furtherQuestion": "Ти так само можеш поставити мені будь-яке питання про твоє майбутнє, а так отримати поради чи мотивацію! 😉 \n Або ти можеш просто покинути цей діалог /stop",
3134
"name": "ukrainian",
3235
"publicity": "Ваша реклама може бути тут! 🌟",
3336
"welcome": "Вітаємо! Ми раді бачити вас тут! 😊",

0 commit comments

Comments
 (0)