It should be easy to add new intents and there should be a generic way intents are processed. In this video we will create the logic that delegates the processing of an intent to dedicated modules.
- [Narrator] By now we have created a message handler that deals with incoming intent. Now imagine that we are heading complex logic to handle each of those intents. The message handler would soon become a big cascade of if statements with a lot of code in each block. Ideally it should be easy to add new intent handlers and each of them should live in its own code. To achieve that, let's create a folder named intents. So right click here in Server, New Folder, Intents.
And let's create a module that will handle our time intent. So I create a New File and I will call it timeIntent.js. Now we have to work out a convention how all our intents should be structured. And intent needs to accept intent data and process them to provide a result. So let's implement that. On top of the file again, we add our use strict. And we create a function module export process equals the function process and this will get an intentData object and will also take a call back because we have something infamous going on in there later.
First, let's make sure that we are really having a time intent here. So add this if statement, if intent data.intent and this is an array so we want to look into the first element .value does not equal time so it's not time intent, we want to return the call back with an error, turn call back new Error and we will use the back ticks here again to add the message Expected time intent, got and we use this intentData zero value here.
Dollar up front, coded brackets, we put this in there. So that we see what we actually got back and what was wrong here. Now we want to check if the data is complete. So we add another if statement. If not intentData.location, this means we haven't got a location object back. We want also return the call back with an error object, new Error and this should read Missing location in time intent.
Now that we know that our data is same we can process the intent. For now, with the simple response that we don't yet know how to process that. So we return the call back, we have no error now. So the first argument is false. And we use the back ticks again. Return the message, I don't yet know time in dollar, collar brackets and we use this intent Data location object here.
Intent Data Location, the first array, element.value. You might've guess that we now have to change the sect line as well so we open that. We will now re-factor and enhance the call back to nlp.ask. Currently, we call ask for every incoming message. This is most probably not what we want. Let's add a condition that the word errors needs to be contained in the message as well. So here on line 14 before nlp.ask method is called add if message.text to LowerCase because we don't care if it's written in upper or lower case .include the word iris, we want the call or ask method.
In any other case, not, so I copied this out here. And paste it in here, you see now that there is some indentation problem and in every editor you have the possibility to re-indent. In this case, in this kind of code, I can shortcut Shift option and f and everything will be re-indented properly. Now let's add a try catch block because this helps us to handle all our errors in one place so here on line 22, I'm adding try and the catch here that takes an error.
In this try block, I now add an if statement to check if our data is same. So let's test for if res intent is not present or the first element of res intent is not present. So zero or if there is no res intent zero .value. We want to stroll an exception in this case so let's add this row, throw new Error that contains the message Could not extract intent.
In any other case, now we try to require the intent module and if the module is not found, this will throw a module not found Error which will again, then be catched our catch block below so add const intent equals require and we want to look into out intents folder and the file name should be something like res.intent zero .value plus the word intent.
Let's check that so if a time intent comes in, we would require time intent. Now let's call the process method on intent. So add intent.process, our response and then the call back Error and response. And in this function, we add some Error checking. So if there is an Error, we will do a consoler log error.message and return.
In any other case, we will now use our rtm object to send a message so the sendMessage method using the response we just got back and sending it to the message channel. So we are done here, what's now missing is the actual catch error block so in this block, we will first log out what actually happened so I log out the error and also the response object and then we will simply use rtm again, sending a message which will tell us Sorry, I don't know what you are talking about.
And we sent this into the message channel again. Now let's doubt iris again. Node bin run and we open up Slack to ask for the time in Paris but now we have to add the word iris because we have this condition now in that iris needs to be contained in this message and we ask Iris, what the time in Vienna? And we get the reply, I don't yet know the time in Vienna.
We have now a bot that knows its name and will try to understand what is being said if its name is mentioned. We also have an intent routing that lets us dynamically add new intent without changing the code of the message handler. In the next step, we will now finally implement a service that can tell us the local time at the given location.
After explaining some basics about Node.js and microservices, Daniel shows you how to sketch out the planned architecture for your application and get the boilerplate code, modules, and credentials in place. Next, he shows how to create a bot user in Slack, connect to Slack, and post messages. He also shows you how to get your bot to process variations in text by creating logic that delegates the processing of intent to dedicated modules. Lastly, he shows how to register additional services and he covers how to use monitoring to identify architectural or performance issues.
- Using Slack APIs
- Sketching out a Slack bot architecture
- Setting up a project and choosing modules
- Creating and naming your bot
- Connecting to Slack
- Setting up and using natural language processing
- Routing by intents
- Implementing geocoding and time calculation
- Adding and monitoring services