Skip to content

Instantly share code, notes, and snippets.

@OAyomide
Last active November 17, 2018 09:27
Show Gist options
  • Select an option

  • Save OAyomide/8d774a72a9bbdac69891cbafaa88f2ee to your computer and use it in GitHub Desktop.

Select an option

Save OAyomide/8d774a72a9bbdac69891cbafaa88f2ee to your computer and use it in GitHub Desktop.
How to use bp.convo the right way
askName:
- typing: true
text: What is your full name?
askEmail:
- typing: true
text: Please input your email
selectGender:
- typing: true
text: Select a gender below
quick_replies:
- <.male> Male
- <.female> Female
- <.other> Other
age:
- typing: true
text: How old are you?
bp.hear({
type: 'postback',
text: 'something_here'
}, (event, next) {
const txt = txt => bp.messenger.sendText(event.user.id, txt);
//since we are using a convo here, we have to make sure the user has no previous unresolved convo
//this is to prevent using the convo response in a different convo thread, thereby messing up our bot and
//confusing our bot. So we look for an existing convo event. If we find, we abort it and ask user to start the
//convo again
if (bp.convo.find(event)) {
bp.convo.find(event).abort('Stopped previous convo');
return txt(`Oops! I discovered an unresolved convo. I stopped it, but you have to start again');
}
bp.convo.start(event, convo => {
convo.messageTypes = ['postback', 'quick_reply', 'messages', 'text'] //we are listening for button clicks and text.
convo.threads['default'].addQuestion('#askName', response => {
let name = response.text; //catpture our response from user input, i.e User's name
` convo.set('name', name) //we are saving the response so we can get it back after the convo is donw. This is better than using th KVS/DB in this case
convo.switchTo('email') //the next thread to switch to after this has been done
})
convo.createThreads('email');
convo.threads['email'].addQuestion('#askEmail', response => {
//of course, the beauty of convo is that you can parse and validate the response and act accordingly
// if we wanted to validate if the user input is a valid mail, we simply add a callback here
// and validate using regex
let email = response.text;
convo.set('email', email);
convo.switchTo('gender');
})
convo.createThreads('gender');
convo.threads['gender'].addQuestion('#selectGender', response => {
let gender = response.text.replace('GENDER.', '').toLowerCase().trim(); // we parse our event text to retrieve the user gender from the event text
convo.set('gender', gender);
convo.switchTo('age');
});
convo.createThreads('age');
convo.threads['age'].addQuestion('#age', [
{
pattern: /(\d+)/,
callback: (response => {
let age = response.match;
if (age < 18){
txt(`You must be older than 18. Please try again`);
convo.repeat();
} else {
convo.set('age', age);
convo.next();
}
}
}),
{
default: true
callback: (response => {
txt(`That doesnt look like a number. Please try again`);
convo.repeat();
})
`}
])
convo.on('done', () => {
let name = convo.get('name')
let age = parseInt(convo.get('age'));
let gender = convo.get('gender');
let email = convo.get('email');
txt(`Your name is ${name}, You are ${age} years old. Your email is: ${email} and you are ${gender}`);
})
});
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment