Interested how Laravel works under the hood? → Check my Laravel Core Adventures Course

BotMan Quick Tip - How to repeat a question

#chatbot

BotMan is a framework agnostic PHP library for building chatbots. In today's tip I want to show you how to repeat a question.

The scenario

Most chatbots today are like forms. You provide some information and then you'll get something in return. So for example you answer some questions and the bot will show you a product that fits your needs. This means it is important that it understands your responses. If not, the bot needs to ask you again and repeat the question. That's not what we want but it will happen and this why I am very glad BotMan implemented a repeat feature.

Repeat a question

The repeat() method is something you can use inside a Conversation. So let's look at an easy example.


// Inside a BotMan conversation class
$this->ask('Do you want to book a flight?', [
	[
		'pattern' => 'yes|yep',
		'callback' => function () {
			$this->say('Okay - we\'ll keep going');
		}
	],
	[
		'pattern' => 'nah|no|nope',
		'callback' => function () {
			$this->say('That is ok.');
		}
	],
	[
		'pattern' => '.*',
		'callback' => function () {
			// do something else
		}
	],
        
]);

In my last article I explained how to use a default pattern in BotMan. This is our base for today. We have a simple question and three different patterns. If the answer is positive we can go on and ask the next question. When it is negative we may ask something differently. If the first two patterns are not matched, the last default one will be triggered. We didn't understand the user's response and the best way to solve that problem is by asking the user again.


// Inside a callback array

[
	'pattern' => '.*',
	'callback' => function () {
		$this->repeat();
	}
],

BotMan will now sent the question again when we do not get our needed answer. That's quite good so far but you will notice that the second question often doesn't feel that nice. Repeating the exact same question again is something we know from machines but not from humans. So this does not feel natural and it is better to change the second one. Luckily BotMan helps us here again and you can pass an aurgumnt to the repeat method.

// Inside a callback array


[
	'pattern' => '.*',
	'callback' => function () {
		$this->say('Sorry I did not get that.');
		$this->repeat('Are you interested in booking a flight?');
	}
],

The same conversation question will be triggered again but with the new text now. This is so much better now right? Right :-) And as if this is not enough you could even use a Question object instead of the string.


// Inside a callback array

[
	'pattern' => '.*',
	'callback' => function () {
		$this->say('Sorry I did not get that.');
		$question = Question::create('So interested in booking a flight?')
				->addButtons([
					Button::create('Of course')->value('yes'),
					Button::create('Hell no!')->value('no'),
		]);
		$this->repeat($newQuestion);
	}
],

Conclusion

So that was it for today. Repeating is a simple but really powerful BotMan feature that you should use a lot. So start today!

Do you enjoy my posts?

Sign up for my newsletter to receive updates on my latest content.

You will receive monthly updates on my latest articles and products. I do care about the protection of your data. Read my Privacy Policy.