Contexts | Chatbot Library
Skip to main content

Contexts

Context will be our friend, if you know how to handle and use it :).

Or Look at this Tutorial in Spanish with Mizar.

  • Contexts helps us to maintain information throught the entire session that we have with the user.
  • With Contexts we can save information that the user provided with Parameters.
  • But also there are other capacities that we can do with our context, these features can be only used from the Backend.

Where the Context comes from

  • If you have been able to look at a Dialogflow Request, you can check that there are lot of important parts in the JSON request, but as Contexts, we are just going to focus on outputContext this array contains all the contexts.

Context Part on our Structure

  • Context by itself can't be sent to Dialogflow, we need to append the Context with any of the templates/structure messages.
  • The context wont change in the actual intent but for the next intent.
  • Keep in mind that contexts can be used only in functions that have an outputContext, and these are the only that can change/ alter the values of a context.
  • Let's look at the Context Functions.

Context Functions

  • These functions create a Copy of the Actual context, (As you see we are not updating it, we are just creating a copy of the actual context).
  • Most of the functions add an aditional variable, or updates a new one and save the entire context in a variable called contextBody
  • This variable is sent in addition of context = True.
  • This context/context body is sent with a Structure/Template.

Why creating a Copy of the Actual context, append variable(s) and sent again ?

When we sent a structure as a Context, the old context will be erased. So in order to avoid losing variable and values from our context, we decided to do context handling in this way.

Lets do an Example!

tip

Most of the contexts functions need a contextIndex in order to select the context that is going to be changed or created. In recent releases of Zaroc you can insert "auto" as a parameter of the function to retrieve the main context where most context parameters live.

Implementing Context

  • First lets create an Intent, name as you want. I'll name start, remember to create an outputContext, ill name init
  • Create it on our backend and let's open a sample structure
if(intent('start')){
//do whatever you like for this context
}
  • Right now lets think that we want to store some information on the context that we created, I want to use a function to get a sessionId and then store on the context inside of a variable named config
  • Inside of our intent brackets use the function shown bellow.
$sessionId = getSessionId();

$contextIndex = [];
$parameters = [
"config" => [
"sessionID" => "$sessionId",
"variable2" => "valor2"
]
];

$contextBody = setContextParameters($contextIndex, $parameters);
  • With this code we would be able to get the sessionID and store in an associative array named config, this array is saved as contextBody.
  • Use a simple structure as show bellow to display an structure and apply changes.
$context = true;
$webTitle = ["intentName"];

$imageArray = ["imageurl"];
$aTitleArray = ["Card Title"];
$aSubtitleArray = ["Card Subtitle"];
$aTextArray = ["Card Accordeon Text"];

$bTitleArray = ["Button Title 1","ButtonTitle2"];
$bCustomArray = ["button Custom 1","button Custom 2"];
$bIconArray = ["Button Icon","Button Icon 2"];
$bColorArray = ["#00efff","#000fff"];


$structure = ['image','comma','card','superDivider','button'];
$components = [
[$imageArray,$webTitle],
[],
[$imageArray,$aTitleArray,$aSubtitleArray,$aTextArray],
[],
[$bTitleArray,$bCustomArray,$bIconArray,$bColorArray]
];

webStructureTemplate($context, $contextBody, $webTitle, $structure, $components);
  • We are using this simple structure to send a image + button + card and our new context that has the sessionId, try triggering this intent and then look at the response, you should be able to see that our context has been changed to the values we setted.
  • I strongly recommend you to try this exercise in more than 1 intent, just to see how Contexts lives and works.