11 Dialogflow CX System Functions
Lesson: Introduction
[00:00] So in this video we are going to talk about system functionsSystem functions in Dialogflow CX allows the user to add cus... More in Dialogflow CX and how you can use them to, usually the idea is that you can do things so that you do not have to call the webhookYou can use webhooks to add custom business logic in both Di... More, you can add like logic, business logic directly into your Dialogflow CX inside the CX conversation builder itself. And as you can see, there are a few different ways that you can use the system functions or rather a few different places where you can use it, you can use it in conditions, in static response messages such as text response, custom payload, conditional response and you can also use them in parameterBoth ES and CX support the concept of parameters. If entitie... More presets.
So we are going to take a look at a specific example bot and I will be going over these different types of usages of system functions.
Lesson: The quiz bot
[00:00] To demonstrate the use of system functions, I’ve created an agent called sysfunc, as you can see over here. And it just has no intentsBoth Dialogflow ES and Dialogflow CX have the concept of int... More right now, it just has the start pageEach flow in Dialogflow CX will have a Start Page. As the na... More with the Default Welcome IntentThis is the intent which is automatically added into the Sta... More. And as you know, the Default Welcome Intent will answer to messages like hi and hello and all that. And what I’m going to do is as soon as the user says that, I’m going to just, this is sort of a pretend quiz bot. And the bot is going to ask what is the answer for question one. Of course, there is no actual question here.
As we, as I said, we are simulating a quiz bot. And the user can either provide the right answer or the wrong answer. Here the user, the agent says what is the answer for question one. And what I’m going to do is I’m going to create a new page. And I’m just going to call it await answer one. And I’m going to go ahead and save that. And as soon as, as soon as I do that, you know that it’s going to create this new page called await answer one. And here I’m going to add a route.
[01:03] And I’m going to make a new intent. So this one will be answer.write. And the training phrase is just going to be write. Or maybe I’ll also say, let me leave it like that. Write is the training phrase, just one phrase. And the agent will say what is the answer for question two. And here it’s going to go to a new page.
And that one is going to be await underscore answer number two. Okay, so let’s go ahead and save that. And the user could have also provided the wrong answer. So I’m going to mark that as answer.wrong. And training phrase is going to be just the word wrong. And go ahead and save that. And once again, the agent is going to say incorrect.
[02:03] And it’s going to say what is the answer to question two. And it’s going to go to the same await answer two page. So you might have noticed that in the case of answer.write, it did not say that the answer was correct. So let’s go ahead and add that, correct, is the agent says correct. And then what is the answer for question two. Let’s go ahead and save that. And I’m going to go into await underscore answer two.
And here we can reuse the route, right, answer.write. So the agent is just going to say correct. What is the answer for question three. And it’s going to go to a new page. And it’s going to be called await answer three. Okay, so let’s go ahead and save that. And if the user were to give the wrong answer over here, it’s just going to say incorrect.
[03:06] What is the answer for question number three. And then we are going to go to the await answer three page one more time. And just to make it clear that you have the right and the wrong answer for this one also, let’s just go ahead and add this answer.write. And it’s just going to say correct.
And it will say something like thanks for taking the quiz. Okay, so let’s go ahead and save that. And if the answer is wrong, it’s going to do the same thing. It’s going to say incorrect. And say thanks for taking the quiz. So this is a very simple quiz. And what we have done is just created a flow where the user can provide either yes or no or right or wrong for these three questions.
[04:02] And then it will just give feedback on whether the user got it right or wrong. Okay, so let’s just go ahead and test the agent quickly. So if it says hi, it’s going to say what is the answer for question one, I’m going to say right. So it says correct. What is the answer for question two? And let’s try right again. And what is the answer for question three? Let’s reset it. And this time I’m going to go with the wrong answer for all of them.
So you can see that it works. So let’s just change it slightly and see what happens. And I’m going to say right. This time I’m going to say wrong. And let me try right this time. So this is the basic idea. You have this quiz. So we are going to use system functions to improve this quiz and, you know, give some feedback about the score and things like that.
[05:00] So we will take a look at that in the next lesson.
Lesson: Text response
[00:00] Now that we have created this quiz, the first thing that we want to do is tell the user the score that they got at the end of the quiz. The first thing that I am going to do for that is I am going to create the, in the answer dot write, you can actually set a parameter and parameter is just going to be score underscore 1 and the value is going to be 1. Okay. And correspondingly, what you want to do is in the case of the wrong answer, you will create the same parameter instead of 1, you will say the score is 0 because they did not get any points for that answer.
Okay. Let us go ahead and do the same thing for the other questions. Here I am going to say score underscore 2 and the value is 1, save that and here you add a parameter score underscore 2 and the value is 0.
[01:06] Okay. Let us go ahead and do that. And I am going to do the same thing for the third answer also, score underscore 3, value is 1, save, add a parameter score underscore 3 and the value here is going to be 0.
Alright. Now that we have these three scores, what you can do is when the quiz ends, you can actually, when the quiz ends, you can, what you can do is you can just take this to a new page, end of quiz. Okay. Let us go ahead and save that. And I am going to do the same thing for the case where they get it right at this point, end of quiz.
[02:06] Go ahead and save that. Let us go into the end of quiz page and in the, in the fulfillment, what I am going to do is, I am just going to sum up the values of the score 1, score 2 and score 3. What I will do here is, you scored and the way you do that is, you are going to use the system functions for calculating this. If you take a look at it, you see that there is a system function for adding, but notice that you can only add two parameters at a time, right.
If I copy paste, what you want to have here is session dot params dot score underscore 1. And here, if you do just dollar session dot params dot score underscore 2, you only have the first two scores, you do not have a way to get the third score.
[03:01] Instead of that, what you can do is, you can replace this, this entire expression with another sys dot func, right. What you can do is, you can copy paste the whole thing. You can chain these function calls together like that and here I am going to copy this and paste it over here, change it to 2 and paste it over here, change it to 3, all right. This is a way to use the system function to add the scores that we saw and that will give you the total points that the user got in that quiz.
Let us go ahead and test that. I am going to say hi. Let us say right, right and right. This time, you see that it is actually showing this as a text, it is as if it just, you can see that it did translate something, the session dot parameter scores are coming back with the correct value, but it did not get the, it did not do a computation of the actual value and the reason for that is that you still need to do a sort of a cast to take this from an integer to a string value.
[04:20] You can see that in the sys dot func, there is actually one called to text, okay. We are going to put this also, we are going to add it into the fulfillment over here.
The way you do that is, let us do that and where you have the 3, I want to delete that and I am going to add all this stuff into that parentheses over here, okay. Let us go ahead and save it and let us go ahead and test the agent. You say hi and you say right and right and you say right again and you can see that the answer is you scored 3, okay.
[05:09] To be clear that it is working, let us try it again with all wrong answers, wrong, wrong, wrong and you see that it says you scored 0. This is giving you an example of one of these types of system function usages which is the static response messages inside the text response.
We will improve this quiz bot and I will show you how you can use the system functions for other usages also and we will do that in the next lesson.
Lesson: Conditional response
[00:00] As you can see in the documentation, you can also use these system functions inside of conditional responses. And to show you an example of what a conditional response might look like, you can see that you can construct these if else and end if blocks, you can construct these kind of blocks as text responses, as responses rather in your fulfillment section. I am going to take an, the same example and I am just going to show you how you can do that. Right below where we just provided this information, you scored, you know, something.
I am just going to add out of three because that is the number of questions. What I will do is add another dialogue option and this time I am going to use a conditional response, okay. We will take this as the format and let us just paste it here. And here instead of the condition, okay, you can, you can have conditions which look like this, okay.
[01:03] Instead of the condition, what I am going to do is, I am going to copy this entire expression, do not need the to text, everything other than the to text and, okay, so equals 0, then the response is going to be, they just provide text message like that. You can say, you scored 0 out of 3.
Now, this is, of course, quite a contrived way of putting it because you can just say this as the fulfillment right here, you can just provide the expression and say out of three and we will be done. I am just showing you this as an example of how you can use the same system functions also as part of conditional responses, okay. Let us copy this thing and paste it over here and if it is equal to 1, it is going to be, you scored 1 out of 3 and let us copy this, paste it and if it is equal to 2, it is going to say, you scored 2 out of 3 and in the else condition, it can only be, the only possible value is 3, it cannot be less or more, there is this summation of all these values can either be 0, 1, 2 or 3.
[02:33]
We already know that, so when I check for the last condition, I actually do not need to check it, I can just say, you scored 3 out of 3, okay, so we can just go ahead and do that. Let me go ahead and save this conditional response, go to the test agent, start, write, write, write, you can see that it says, you scored 3 out of 3 because it was able to do the calculation correctly using the conditional responses.
[03:07] If I were to change it and go with wrong, wrong and wrong, you can see that it is going to go with this, you scored 0 out of 3 and this 0 is based on the conditional response as you know. It means that this particular if condition got satisfied, okay, so let us just to test it fully, we can try one more, this time I am going to say, write, wrong and right, you can see that it is coming back with, you scored 2 out of 3.
This is another way to use these system functions, you can also use them as part of conditional responses, as I said this is a very silly kind of an example but you can see why these conditional response blocks can be very powerful because you can have a pretty complex compound expressions in the conditional response and based on that you can provide these messages back to the user.
[04:11] This can be very handy, if you have a bot where you want to do most of the logic inside Dialogflow CX, the conversation flow itself without having to call the webhook, so that is something that is possible because of using system functions and when you combine the system functions with these conditional response blocks, actually you get an option to do a lot of powerful things, so that is what we are seeing in this case and in the next lesson, I will show you how we can improve this bot even further.
Lesson: Custom Payload
[00:00] You can also use system functions with the dot to text in any text you use inside custom payloads. I’m going to just going to show you a very quick example. This is the description response in Dialogflow Messenger. Let’s go ahead and copy this and I’m going to paste it over here and what I’ll do is I’m just going to say results as the title and then in the text, I’m just going to copy the whole thing from the agent over here and then I’m just going to paste it inside this text JSON field that you see over here.
I just pasted the whole thing. I’m going to go ahead and save it. Okay, now we go to manage the integration and we go to the Dialogflow Messenger connected. I already connected it to the draft version. Let’s go ahead and click on it and do this inside the Dialogflow Messenger tester that you have inside the console and I’m going to say write, write and you can see that it added this results description section which says you scored three out of three.
[01:15] Let’s do done and do it one more time. I’m going to say hi, wrong, wrong and then I’ll do right.
You can see that it has you scored one out of three coming and all the different answer sections. There’s not much to see here in this case of the custom payload except that I just showed you that it’s possible and that you can use it pretty much in the same way that you use in the text response. You just have to make sure that you cast it to a text so that you are able to use it inside the custom payload block which you know it expects text over here. Okay, so that’s how you use the system functions in your custom payloads.
Lesson: Parameter Presets
[00:00] So let’s take a look at how we can use system functions inside parameter presets. And I think this one is a much cooler application of system functions, because you can actually do certain things, which I think are much harder to do. Well, I shouldn’t say much harder, but it’s just that you will be able to do certain things without calling the webhook. And you might not have actually expected that. So by using system functions inside parameter presets, you are able to do certain kind of calculations where you used to need a webhook call earlier for these kind of things.
So what I’m going to do is I’m going to go to the start page, and I’m going to add a parameter here called final underscore score. And I’m just going to set it to zero. So as the conversation starts, the final score has been set to zero. And inside await answer one, what I’ll do is I’ll add a parameter. And of course, the parameters left hand side is just going to be the name of the param.
[01:04] On the right side, what I will do is I’m going to add this something which looks like this. So let me copy and paste it over. So this you can see that we are taking the sys.func.add and then we are passing into it. Okay, I’ll just expand it so you can see it more clearly. I don’t think that helps. So sys.func.add session.params.counter. So instead of counter, I’ll just make it final score because we keep incrementing the final score at each step if the user gets the answer right.
Okay, so we incremented by one. Let’s go ahead and save that. Now, what do we do about the answer.wrong? We don’t do anything because they don’t, you don’t have to increment their score. We don’t need to add a zero, because adding zero is effectively doing nothing, right? So you just leave it as it is.
[02:00] And we go to await answer two, answer.right. And once again, we are going to do the same thing, final score. And here again, we’ll do the same thing. Copy this and put the sort of counter, you make sure that you use final underscore score. I just want to show you one more thing. You have to put it inside quotes. So even if you don’t put the quotes, you can see that Dialogflow CX automatically adds those quotes to the end.
So I’m not sure why they use that, but that’s something which is the convention. Okay. And then await underscore answer three, same thing, answer.right. We’re going to add a parameter and it’s going to be final underscore score. And once again, we copy this and paste it, final underscore score, and you increment it by one again.
[03:05] Before we test it, we have to make sure that we use the value at the end. So we go to the end of the quiz, go to the edit fulfillment. And by the way, I’m going to delete this custom payload now, because it’s just noise. And let’s add a dialog option of text. And we are going to say, you scored dollar session dot params dot final underscore score out of three.
Okay. So this is the final score that you’re going to compute over here. And let’s do this, test this. So high and then right, right. You can see that the final score is getting incremented on this left hand side, right. And notice that it’s coming back with, you scored three out of three. Okay. So reset, let’s try it again.
[04:00] So this time we go with wrong, wrong, and the third wrong. And you see that it says you scored zero out of three, the final score is still zero. And let’s try it one more time. So if you say right, you can see the final score has increased to one. And I say wrong, final score hasn’t changed. And then I say right. You can see that the final score has now become two. And you can see that the responses are consistent.
It says that you scored two out of three, which is what the final score has been. That’s the value which has been computed in this parameter. So that is how you can use the system functions inside the parameter presets. And as I mentioned, you can see that when you’re, when it’s possible for you to compute in inside the Dialogflow CX itself, if you’re able to do this kind of complex calculations and do like assignments of variables as the flow is happening, that just generally means that you have much more options to improve and make your business logic, put the business logic inside the Dialogflow CX console itself without having to call the webhook.
[05:18] One of the things that I would like to point out, if you are familiar with the quiz bot from the Dialogflow ES courses, you kind of know that I mentioned before that one of the big limitations that you have in ES is that you can’t do this kind of business logic inside of ES without having to call the webhook.
So in other words, if you want to do a score calculation at the end, that’s something that you had to use the webhook call to be able to do that for doing mathematical calculations and things like that. And you can see that in those aspects, Dialogflow CX provides a really big improvement, right? So this is how you use the system functions in parameter presets inside of Dialogflow CX.
Lesson: Condition Routes
[00:00] So finally we can also use the system functions inside conditions and I’m going to show you how you can do that. So this time what I’m going to do is, I’m going to tell the user, so let’s go to the end of the quiz and here I’m going to add another message where I tell the user if they got all the answers right, I’m just going to congratulate them, okay. And the way I’m going to do that is going to be a bit, once again it’s a bit like convoluted you might say, but I think it is a very nice way to showcase the power of this conditions inside of the Dialogflow CX and I mean when I say conditions combining all the things that we have learned.
So one of the things that I’m going to do is, I’m going to say add a route and this one is going to be a condition route, okay. And here you’re going to customize this expression, okay.
[01:00] So what I’m going to do is, there is a, let’s go to the conditions reference and you notice that you can do something called sys.func.contain and then you can see that there is a list that you can have and then you can check to see if it has a value and if it is equal to true, okay. So what I am going to do is, I’m going to do the same thing and I’m going to construct an actual list, okay. So here I’m going to say equal to true, I’m going to, instead of true I’m going to say equal to false and the thing I’m checking for is the value zero and instead of session.params.list I’m going to construct an actual list and inside the actual list what I’m going to do is I’m going to add the three different parameters that we already have, okay.
Session.params.score underscore one and let me copy that, comma, another comma, another comma.
[02:06] So you have two and you have three and if this condition is satisfied I’m going to say well done, all correct, okay. So let’s check what this is saying one more time. I’m going to copy the whole thing and paste it into this text editor so it’s clearer what’s going on. So we have sys.func.contain and you have session.params.score underscore one, session.params.score underscore two, session.params.score underscore three.
So as you can see we created, we constructed a list out of that and we check to see if it has a zero and if that condition is false which is to say that there are no zeros in this list which means all of them are one that means all the answers are right, okay. So that’s exactly what it means. So let’s go ahead and save that.
[03:00] I’m going to go and test the agent. Let’s say hi and write, write and write again. You can see that it says well done, all correct, okay. So to make sure that it’s actually working let’s try it one more time with all wrong answers and what you would expect is you don’t see that last statement which says all correct, okay. That’s because the calculation turned out, this calculation turned out to be false, okay.
That is if you go back to this expression we have zeros in this list. So this is, this condition is going to evaluate to true and that in turn is going to, that is this overall condition is going to evaluate to false, okay. I know this expression is a bit complicated but if you give it some thought you understand what’s going on here and that’s how you can do, that’s how you can use the system functions inside of the conditional routes.
[04:00] And just to be, you know, very sure let’s do this. We make two writes and just one wrong and you notice that it’s still not going to come back with this well done message because that still has at least one item in that list which is score underscore three is actually equal to zero. So this expression is going to evaluate to be a false expression.
Note: This is my old website and is in maintenance mode. I am publishing new articles only on my new website.
If you are not sure where to start on my new website, I recommend the following article:
Is Dialogflow still relevant in the era of Large Language Models?