A
A
arti_djeims2016-12-27 02:02:56
JavaScript
arti_djeims, 2016-12-27 02:02:56

How to make a sequential JS bot?

There is a code that works with certain entered words:

var message = 'хочу заказать пиццу';
  
   if(message.indexOf('пиццу') + 1) { //если в предложении есть слово "пиццу"
      alert('с чем пиццу хотите?');
   }

In this example, when a person enters any sentence that has the word "pizza" in it, then the Bot will ask the following question, "what do you want pizza with?" and now how to make it so that we can choose a pizza by the same principle by searching for the name of the pizza in the sentence, for example, "I want Hawaiian pizza" and the bot will find the word "Hawaiian" then the bot will ask the next question, what size, etc. d.
var message = 'я хочу Гавайскую';
  
   if(message.indexOf('пиццу') + 1) { //если в предложении есть слово "Гавайскую"
      alert('Какого размера должна быть ваша пицца?');
   }

So, how to make it so that there would be exactly a chain of questions, so that at the first stages when we write that we want pizza, so that the names of pizzas at that stage are not yet readable, but only after we answer that question, then we can proceed to next, how to implement such code? I will be grateful for your help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita Kamenev, 2016-12-27
@NickStone

The question is kind of vague.
The logical answer to your question is to simply nest the condition in your if(message.indexOf('pizza') + 1).
Preliminarily get a list of pizzas by ajax and then extract the name of the pizza using a regular expression.

O
Oleg, 2016-12-27
@politon

Agree with previous post.
And only this will work) https://jsfiddle.net/sdhqkpxn/4/

S
Sergey, 2016-12-27
@begemot_sun

Remember, your bot should always have a state. Regarding which the bot will expect some answers.
First, draw a diagram of the dialogue, what are the possible questions and what are the answers, and what are the next questions after the answers you will ask.
And then, a bunch of IF / THEN / ELSE in the code (if it's stupid on the forehead), or some DSL to make it all easy to support.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question