Built-in Functions | Chatbot Library
Skip to main content

Built-in Functions

Zaroc has many useful Functions that can help us handle Dialogflow's Request in our backend and provide a good response.

Zaroc Functions

Intents

These Functions help us to handle Intents

Intent

  • Receives the name of the intent and returns true or false, is used in IF statements to do an specific behavior for an intent in our code
if(intent('intentName')){
//Do some code
}

Action

  • Receives the name of an action and returns true or false, is used in IF statements to filter intents.
  • You can use action as other filter to work with specific rules for some intents.
if(action('actionName')){
//Do some code
}

.

Parameters

These functions are used to handle Parameters

getIntentParameter

  • Returns the value of the specified Parameter in the living Intent
$parameterValue = getIntentParameter()['parameterName'];

getContextParameter

Keep in Mind

When going deep into context, check Contexts section in Zaroc Advanced

  • Returns the value of the specified Parameter and Context
  • Receives a Number as the index of the specified Context when the variable is living
  • You can pass "auto" as parameter to retrieve the main context if you are not able to retrieve correctly your context: Example ( getContextParameter(auto)['parameterName']; )
// Pass context index as a number if you want that specific context and you know how contexts works Ex: 0
$parameterValue = getContextParameter(0)['parameterName'];
// Pass context index as "auto" to retrieve your context
$parameterValue = getContextParameter("auto")['parameterName'];

.

Triggers

These functions help us to Trigger Special Behaviors on our Chatbot from the Backend

triggerEvent

  • Triggers the Intent that has the event typed as argument
  • Receives the Event name and Params as Array
  • Params are optional, these params helps to send data to the context from the current intent to the nextIntent that is being trigger by this Function
$eventName = ["event1"];             #array
$params = []; #array

triggerEvent($eventName,$params);

triggerError

  • Displays an Error, works for all the integrations.
  • Receives as Array, the name of the intent and the message of the error to trigger.
$context = false;                                          #string
$contextBody = []; #array
$intentName = ["intent1"]; #array
$errorMessage = ["Query wasnt executed ๐Ÿ’ฅ"]; #array
$params = []; #array

triggerError($context,$contextBody,$intentName,$errorMessage,$params);

triggerPrompt

  • Triggers a Prompt when needed, has the same format as the default required parameters that dialogflow has.
  • Its used in Addition with SlotFilling (Check Documentation for SlotFilling).
  • context and contextBody are optional. (Remember to check Doc for Context if you wanna use it).
$context = false;                                           #string
$contextBody = []; #array
$prompt = ["Whats your favourite food"]; #array

triggerPropmt($context,$contextBody,$prompt);

.

Context

Context help us to save information, with these backend functions we can create and update variables on an specific context.

  • Be noticed that we cant delete variables neither contexts. Just create and update.
  • To have a whole picture of how Context works, check Context on Zaroc's Advanced.

setContextParameter

  • This Function allow us to add a Parameter to the Selected Context.
  • You can pass "auto" as a replace of the contextIndex number you want to retrieve the main context and create new parameters on it
$contextIndex = [0];                                      #array
$variableName = ["fruit"]; #array
$variableValue = ["coco"]; #array

$contextBody = setContextParameter($contextIndex,$variableName,$variableValue);

updateContextParameter

  • This functions allow us to update the value of a parameter inside of the selected context.
  • You can pass "auto" as a replace of the contextIndex number you want to retrieve the main context and update it
$contextIndex = [0];                             #array
$variableName = ["fruit"]; #array
$variableValue = ["Banana"]; #array

$contextBody = updateContextParameter($contextIndex,$variableName,$variableValue);

setContextParameters

  • Creates multiple values on the selected Context.
  • You can pass "auto" as a replace of the contextIndex number you want to retrieve the main context and create new parameters inside of it
$contextIndex = [0];                        #array

$parameters = [ #associative-array
'color' => 'white',
'animal' => 'bird',
'coffee' => 'expresso'
];

$contextBody = setContextParameters($contextIndex, $parameters);

updateContextParameters

  • Updates multiples values on the selected context.
  • You can pass "auto" as a replace of the contextIndex number you want to retrieve the main context and create new parameters inside of it
$contextIndex = [0];                        #array

$parameters = [ #associative-array
'color' => 'black',
'fruit' => 'apple',
'coffee' => 'capuccinno',
'animal' => 'gorilla'
];

$contextBody = updateContextParameters($contextIndex, $parameters);

.

Troubleshoot

In order to Troubleshoot we can use some functions. (Check troubleshoot section for more info)

getInput

  • Returns the Entire input Request that Dialogflow has sent
  • This input helps you to look at the body and troubleshoot.
$input = getInput();

createInput

  • This function allow us to create a file.txt on ourServer with all the Request.
  • Receives an array with the name of the file that you would like to create.
$fileName = ["request"];                             #array
createInput($fileName);

getSourceData

  • This function allow us to create file with the data that comes from a Different Source that isnt Dialogflow.
  • Receives an array with the name of the file that you would like to create.
$fileName = ["request"];                                    #array
getSourceData($fileName);

getTimeOut

  • Returns the time in ms that took all code execution to be done.
  • getTimeOut can't take ms that cost the request in being proccesed by Dialogflow (4000 ms).
$time = getTimeOut();

.

Miscelaneous

These functions are not commonly used, but sometimes are helpfull.

getUserInput

  • Returns the Input from the user that Triggered the intent
$userInput = getUserInput();

getSessionID

  • Returns the sessionId of the current chat/session
  • Id format changes depending of the platform/integration
$sessionId = getSessionId();

getProjectName

  • Retrieves the name of the project
$projectName = getProjectName();

getWhatsAppPhoneNumber

  • Retrieves a WhatsApp PhoneNumber if you are in Session VIA whatsApp
  • This data is provided for mostly wWhatsApp Providers
$phoneNumber = getWhatsAppPhoneNumber();

getTelegramChatId

  • Returns the ChatId of a Telegram Chat
  • Only obtainable if you are chatting via Telegram
$chatID = getTelegramChatId();

getTelegramCallbackQueryId

  • Returns callbackqueryId of Telegram if exists
  • Only obtainable if you are chatting via Telegram
$callbackQueryId = getTelegramCallbackQueryId();