Botkit: Asking questions based on condition from other answer

Created on 21 Feb 2017  路  3Comments  路  Source: howdyai/botkit

Hi,
I try to build a flow where the user is asked some questions.
I want to :

  • Be able to ask some questions depending of the previous answer (or based on a db info).
  • If the user input match no possible answer for a question i want to ask this question again.

Like this :

ask Question 1
    if(Answer 1 !== 'something'){
         reAsk Question1
    }
if(user.variable === 'test'){
    ask Question 2
        ask Question 2.1
        ask Question 2.2
}
ask Question 3
if(answer 1 === 'something else') { 
     ask Question 4 
     ask Question 5
} 
else{
    ask Question 6
}

Is this possible with botkit ? I tried many different way but i can't figure out how to convo.repeat() without asking the next question ( #669 )
And i don't want to call Question 2 from Question 1 because Question 1is not required (i may have already the answer in database.)

I don't if it's clear enough... tell me if not !

Thanks for your help and this awesome OSS !

help wanted

Most helpful comment

What @GautierT mentioned. Threads are exactly what this is for.

So you'd start the conversation with bot.startConversation(message, (err, convo) or createConversation keeping in mind if you use createConversation you'll need to convo.activate(); or it'll just sit there.

Within your conversation you can do something along the lines of...

convo.ask({
   text: "this is my question", 
   [
     {
     pattern: 'the text you want to match',
     callback: function(response, convo) {
      convo.changeTopic('some_other_convo');
     }
     }
   ]

So there's your ask method, which will ask a question, if it matches the pattern it'll change the topic to in our case some_other_convo. We can use two methods here, addMessage or addQuestion you can read a bit more about them on the docs https://github.com/howdyai/botkit.

So in our addMessage method we'd having something that looks like...

convo.addMessage({
   text: 'the response you want from the question`,
   action: 'you can add an action which will send it to another topic, optional though'
}, 'some_other_convo');

You can chain the patterns together and do something like this at the bottom...

{
   default: true,
   callback: function(response, convo) {
      convo.changeTopic('bad_response');
   },
}

If nothing matches we send them to a new topic called bad_response but obviously you can just change a convo.addQuestion method which tells the user you didn't recognise what they send and give them the questions again.

All 3 comments

I used the thread functionality to do that : https://github.com/howdyai/botkit#conversation-threads
Listing all my addQuestions and ref them with a thread_name then going through them with convo.gotoThread.

convo.addQuestion('Q1', (res, convo)=>{
    convo.gotoThread('question2')
}, {}, 'question1')
convo.addQuestion('Q2',  (res, convo)=>{
    convo.gotoThread('question4')
}, {}, 'question2')
convo.addQuestion('Q3',  (res, convo)=>{
    convo.gotoThread('question2')
}, {}, 'question3')
convo.addQuestion('Q4',  (res, convo)=>{
    convo.gotoThread('question3')
}, {}, 'question4')

What @GautierT mentioned. Threads are exactly what this is for.

So you'd start the conversation with bot.startConversation(message, (err, convo) or createConversation keeping in mind if you use createConversation you'll need to convo.activate(); or it'll just sit there.

Within your conversation you can do something along the lines of...

convo.ask({
   text: "this is my question", 
   [
     {
     pattern: 'the text you want to match',
     callback: function(response, convo) {
      convo.changeTopic('some_other_convo');
     }
     }
   ]

So there's your ask method, which will ask a question, if it matches the pattern it'll change the topic to in our case some_other_convo. We can use two methods here, addMessage or addQuestion you can read a bit more about them on the docs https://github.com/howdyai/botkit.

So in our addMessage method we'd having something that looks like...

convo.addMessage({
   text: 'the response you want from the question`,
   action: 'you can add an action which will send it to another topic, optional though'
}, 'some_other_convo');

You can chain the patterns together and do something like this at the bottom...

{
   default: true,
   callback: function(response, convo) {
      convo.changeTopic('bad_response');
   },
}

If nothing matches we send them to a new topic called bad_response but obviously you can just change a convo.addQuestion method which tells the user you didn't recognise what they send and give them the questions again.

@pourmesomecode : Thanks for this very good explanations !
Do you have an idea of how can i quit/pause a convo when i "hear" something live quit or pause ? Without having to add a

if (response.text === 'quit'){
    convo.stop()
}

in all my addQuestion handler ?
Thanks !

P.S : this answer should go in an exemple or a wiki. Very clear !

EDIT : Solution here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matueranet picture matueranet  路  4Comments

benbrown picture benbrown  路  3Comments

seriousssam picture seriousssam  路  3Comments

koubenecn picture koubenecn  路  3Comments

imjul1an picture imjul1an  路  3Comments