Get your First Parameter
Its time to get our first Parameter VIA this Doc.
Or Look at this Tutorial in Spanish with Mizar.
Why to get a Parameter?
Getting a Parameter give us a possibility to save User information, wherever that user type, we can save it in some cases. These information can help us to change to other paths or move forward in the core flow.
Its great to know that we an save information and use it on our backend.
Two types of Parameters
There are two types of parameters and these are clasified in the space that they reside.
Local Parameter
This parameter lives in the actual intent, can be used in the actual intent. If you go to other intent, this parameter will disappear
In order to use it follow this example
- PHP
$value = getIntentParameter()['variableName'];
As you see we are storing a variable from the intent with the nameFrom the variable. If you develop enough you will find not to much uses for this type of parameter. For most only use cases we use...
Context Parameter
This parameter lives in the contexts of the intent that is storing it. These parameters can be used in every action that uses the context. With this way we can extend the lifespan of parameters and information.
In order to use it follow this example
- PHP
$value = getContextParameter()['variableName'];
We save a variable in $value, this variable is retrieved from a Zaroc function 'getContextParameter', then insert in the single quotes and the keyname of the parameter
So with this explanation let's do a quick tutorial over these two functions.
Getting a Intent Parameter
In Dialogflow
Create a new Intent, name it asking_coffee, set a simple training phrase and a simple response. Bellow training phrases you should be able to see a section called 'action an parameters' go in a blank space inside of it and type cofee as a name, this name will be the name of the variable, in the second part type @sys.any, you should get this entity by Dialogflow. At the third part, there will be the same name that you putted in the first part but with a dolar sign ($) at the start of it.
At the left of this row there is a blank square, hitting that square you are setting this parameter as required and a new option at the right side will open. This option is a prompt. I recommend you to write a prompt like Tell me your favorite coffee type
When the user triggers this intent, the intent will ask first the required parameters (So the bot will ask for your favorite type of coffee) and then you will get a response from it.
In our backend
Let's create an Intent on our backend as we did in the last tutorial, and inside the code lets get the Intent parameter.
Your code to get the parameter of asking_coffee should look as this.
- PHP
if(intent('asking_coffee')){
$coffee = getIntentParameter()['coffee']; #array
$session = []; #array
$wsTextArray = ["The coffee you choose is $coffee"]; #array
$structure = ['paragraph']; #array
$components = [ #array-multi
[$wsTextArray]
];
wsStructureTemplate($session,$structure,$components);
}
Then use this variable on your response function.
- PHP
$coffee = getIntentParameter()['coffee'];
Testing Intent Parameter
With that code, when triggering the intent, first you should get the bot asking you the type of coffee, type for example 'expresso', after that the bot will tell you the coffee you choose is expresso.
Keep In MIND
You can catch up to 20 parameters per intent.
If you got this response ,Yay! if not, check the Errors section or recap again every step of the tutorial, also you can see the video at the top.
Getting a Context Parameter
In Dialogflow
Do the same steps in Dialogflow to get an Intent parameter, The only difference that you have to implement is just add a output context. Try adding a typeOfCoffee context.
In our Backend
Do the same that we did in our Backend, then use the function of
- PHP
$coffee = getContextParameter()['coffee'];
Your code should look at this now
- PHP
if(intent('asking_coffee')){
$coffee = getContextParameter()['coffee']; #array
$session = []; #array
$wsTextArray = ["The coffee you choose is $coffee"]; #array
$structure = ['paragraph']; #array
$components = [ #array-multi
[$wsTextArray]
];
wsStructureTemplate($session,$structure,$components);
}
Testing Intent Parameter
With that code, when triggering the intent, first you should get the bot asking you the type of coffee, type for example 'expresso', after that the bot will tell you the coffee you choose is expresso.
This value of expresso will be available on all the intents that have the same context (typeOfCoffee). Also remmeber that you can store multiple parameters on a context, from the same or diferent intents.
You can set till 20 parameters per Intent, and have multiple contexts on an intent till 8.
Errors
Keep an Eye
If you got haven't got anything, or an Error.
- Look at the Tutorial Again or check the Video Tutorial
- Check the Troubleshooting Section