Receiving Data from Matched Intent Dialogs in the Bot Framework

by Michael Szul on

No ads, no tracking, and no data collection. Enjoy this article? Buy us a ☕.

The Microsoft Bot Framework Node.JS SDK offers a nice layer between the low-level API and the bot application for you to do your work with little data parsing, but all dialogs that receive data from the user are going to be in a JSON structure with minor differences for you to parse through. Standard dialog responses are slightly different from intent responses, which are slightly different from LUIS matched responses.

If you've been following my Bot Framework tutorials on YouTube, you've seen me go over a few of these differences. Surprisingly (at least as of this writing), these data structures are not documented in the official Bot Framework documentation.

Intent dialogs are the quick-and-dirty way to prototype a bot that isn't going to be a series of choose-your-own-adventure steps. The bot will listen, parse, and hopefully respond (if your regular expression matches are correct).

When you do a standard regular expression intent match, you put it on the intent.matches() method:

intents.matches(/depart/i, [
          (sess, args, next) => {
              ...
          },
          (sess, result) => {
              ...
          }
      ]);
      

When the user types in a message that matches something to do with the word "depart", control is passed to this method, and the args variable will contain the data that you need. The data structure will look something like this:

{
        "score": 0.4842105263157895,
        "intent": "/depart/i",
        "expression": {},
        "matched": ["depart"]
      }
      

You can see from the provided response that the Bot Framework gives a score for the possible match, returns the intent matched upon, and contains a matched array with the actual match.

You can access matched information in a variety of ways, and in my tutorials, you've seen me access this data with args.matched.input and args.matched.index to get the matched term and the index in the string at which the match occurs.

What about for multiple matches though?

In the examples from the tutorial video, I provided a terrible regular expression match to show how regular expressions can only take you so far without becoming incredibly complex, and still can't perform as well as a natural language processor:

{
          "score": 0.6620689655172414,
          "intent": "/(departs|departing|depart).*(arrive|arrival|arriving)/i",
          "expression": {},
          "matched": ["departing Charlottesville and arriving", "departing", "arriving"]
      }
      

You can see that the regular expression in the intent has grouped matches. When you look at the matched array, you can see multiple items: the string that was initially matched and the two grouped items.

This means that you have the ability to not just match a string based on regular expressions, but you also retain the regular expression ability to create matched groups in order to extract information that we need, and the Bot Framework provides that in the same array as the matched string.