Bot framework dialog interruption and cancellation
Let’s first explain what an interruption and cancellation is in context of a ChatBot. It’s used to stop or interrupt the running bot dialog. Interrpution works like a real life interruption in a conversation while cancellation stops the dialog fully. Each of them will have their own benefits and use case.
Bot dialog interrupts
Bot framework dialog interruption works kinda like an interruption in real life conversation. You can interrupt a dialog anytime you want, when something else is really important, and get back to the conversation later. For example, you’re talking to someone outside and something interesting happens. You’re going to stop talking and watch what’s going on and comment on it a bit. When it’s over, you’re going to return to the conversation you had before. Maybe not that “robotic” and stiff, but something like that.
Bot dialog cancellation
Dialog cancellation can be achieved in a few ways, we are going to explain 2 of them here:
- Bot dialog quitting
- Bot dialog expiration
The point of dialog cancellation is to cancel a dialog if you don’t want to finish it, for whatever reason. It’s important to have a cancellation option because dialog doesn’t just end by itself. If you accidentally start a dialog, you’d want to have a way to stop it. Also, dialogs can have major consequences like creating profiles, starting scripts, logging user and more, depends on what kind of dialogs you’ve written. Therefore, you might want to have a way to stop any dialogs that you don’t want to finish.
Quitting a dialog is pretty much self explanatory. When you want to quit a dialog, you simply type quit or cancel and the dialog is canceled. You can set other keywords for quiting a dialog, of course. This enables you to stop the dialog anytime you want because you maybe typed the input wrong or you simply want to do something else at the moment.
Dialog expiration is achieved through dialog timeout. Basically, if you don’t answer to a dialog prompt for a certain amount of time, the dialog ends and your bot goes back to idling. It’s smart to have this covered because, if you have to go somewhere in a hurry, it’s great to come to a clean chat. Otherwise, your dialog would stay active and you’d have to cancel it or finish it to have your bot go to idling again.
Implementing dialog interruption and cancellation isn’t complicated, but you have to have a dialog already coded and your bot ready. It also depends on a type of dialog you have chosen. We’ve chosen waterfall dialog so we’re gonna show you how to make interrupts and cancellation in a waterfall dialog.
We need to add this line to our .env file. Here, we’ll control after how many seconds, of not answering, our dialog expires and ends. It’s an environment variable that we can easily change whenever we want. It’s currently set to 30 seconds, but you can put what ever number you want.
Our environment variable should be passed to our bot in order for it to use it.
We need to add our environment variable to our constructor in bot.js file. Also, we need to create a expiration time property called lastAccessedTimeProperty.
The Run function needs some modification as well, actually a lot. You’ll see the function’s code bellow, we included all the code here because there are quite a lot of changes.
So, not answering for over 30 seconds means that your dialog was canceled. Well, we actually put 10 seconds timeout in the video because waiting 30 seconds would be boring and pointless.
Dialog interrupts and quitting
We’re actually gonna explain there 2 together because they’re actually achieved very similarly. They’re gonna be written in the same file as different functions. That’s great because you can just add different interrupts as you see fit.
We’re gonna create a new file called helpDialog.js. It’s not exactly a dialog like our previous dialog was, it’s a “dialog” that gives us the opportunity to quit the dialog and to interrupt the dialog.
You can see that our help dialog extends the component dialog class. As mentioned, we have 2 functions, one for our interrupt and one for quitting the entire dialog.
Finally, we need to change our dialog to use our new help dialog. Our dialog now extends the helpDialog class, not the component dialog class anymore. We also need to pass the id in the constructor and of course, import our help dialog.
And the dialog starts all over again when called, but here your bot is idling and you can do whatever you want with it.
We hope this article about bot framework dialog interruption and cancellation was somewhat helpful and that you managed to understand everything. If you want more complex dialogs that can branch into other dialogs, come back and circle around, you should probably look into adaptive dialogs.