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
- PHP
- 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.
- PHP
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
- PHP
$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 in the main context
- PHP
$parameterValue = getContextParameter()['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
- PHP
$eventName = ["event1"]; #array
$params = []; #array
triggerEvent($eventName,$params);
triggerError
- Displays an Error, works for all the integrations.
- Receives as Array, session (optional) if needed to change context values/variables, and the error message. (Check Context section if any doubt)
- PHP
$session = []; #array
$errorMessage = ["Query wasnt executed 💥"]; #array
triggerError($session, $errorMessage);
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).
- Receives as Array, session (optional) if needed to change context values/variables, and the prompt message. (Check Context section if any doubt)
- PHP
$session = []; #array
$prompt = ["Whats your favourite food"]; #array
triggerPropmt($session, $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.
setContextParameters
- Creates a new context based in the old one, that you can use them into session to apply the changes.
- If the parameter exists, only changes value, if not then the parameter is created with the value.
- PHP
$parameters = [ #associative-array (could be an arrays of arrays of arrays)
'color' => 'white',
'animal' => 'bird',
'coffee' => 'expresso'
];
$contextBody = setContextParameters($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.
- PHP
$input = getInput();
getIntent
- Returns the current intent that has been triggered.
- PHP
$intent = getIntent();
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.
- PHP
$fileName = ["request"]; #array
createInput($fileName);
getTimeOut
- Returns the time in ms that took all code execution to be done.
- As webhook has a limit of 5 miliseconds, you only can timeout till that maximum time minus the time that takes your code to be proccesed.
- PHP
$time = getTimeOut();
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.
- PHP
$fileName = ["request"]; #array
getSourceData($fileName);
getPlatform
- This function retrieves the current platform that was used for the current request.
- PHP
$platform = getPlatform();
getUserInput
- Returns the Input from the user that Triggered the intent
- PHP
$userInput = getUserInput();
getBotInput
- Returns the Input from the bot, or the response that the bot gived to the user
- PHP
$botInput = getBotInput();
getSessionID
- Returns the sessionId of the current chat/session
- Id format changes depending of the platform/integration
- PHP
$sessionId = getSessionId();
getProjectName
- Retrieves the name of the project
- PHP
$projectName = getProjectName();
.
Miscelaneous
These functions are not commonly used, but sometimes are helpfull.
getWhatsAppPhoneNumber
- Retrieves a WhatsApp PhoneNumber if you are in Session VIA whatsApp
- This data is provided for mostly wWhatsApp Providers
- PHP
$phoneNumber = getWhatsAppPhoneNumber();
getContactId
- Retrieves facebook contact id used for the user that is chatting with the bot
- PHP
$contactId = getContactId();
getTelegramChatId
- Returns the ChatId of a Telegram Chat
- Only obtainable if you are chatting via Telegram
- PHP
$chatID = getTelegramChatId();
getTelegramCallbackQueryId
- Returns callbackqueryId of Telegram if exists
- Only obtainable if you are chatting via Telegram
- PHP
$callbackQueryId = getTelegramCallbackQueryId();