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
- Node.js
- 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
}
- Js works in a different Way, so we need to use a different workaround to handle it.
- In App.js we got the current intentName and then we use it on our basics handler function.
const intentName = getIntentName(body);
if (basics(body, intentName) && basics(body, intentName)?.trim !== "") {
res.send(basics(body, intentName));
}
- Basics file will receive these two params and will switch depending if the intent name is inside here, if that happens a case will be triggered
- If the intent is not inside here, it will return false and the code will be skipped next to the other handler function like (info, test)
- Inside of the case of every intent the dialog will be implemented.
const basics = (body, intentName) => {
lang = getContextParameter(body, 0)["basics_language"];
switch (intentName) {
case "basics_fallback":
result = triggerEvent("basics_start");
return result;
default:
// return `basics module break, ${intentName} was expected ๐ฅ`;
break;
}
};
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
- Node.js
if(action('actionName')){
//Do some code
}
ALPHA;
.
Parameters
These functions are used to handle Parameters
getIntentParameter
- Returns the value of the specified Parameter in the living Intent
- PHP
- Node.js
$parameterValue = getIntentParameter()['parameterName'];
parameterValue = getIntentParameter(body)["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']; )
- PHP
- Node.js
// 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'];
// Pass context index as a number if you want that specific context and you know how contexts works Ex: 0
parameterValue = getContextParameter(body, 0)["parameterName"];
// Pass context index as "auto" to retrieve your context
parameterValue = getContextParameter(body, "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
- PHP
- Node.js
$eventName = ["event1"]; #array
$params = []; #array
triggerEvent($eventName,$params);
- Result statement have to be returned in the current switch case
eventName = ["event1"];
params = [];
result = 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.
- PHP
- Node.js
$context = false; #string
$contextBody = []; #array
$intentName = ["intent1"]; #array
$errorMessage = ["Query wasnt executed ๐ฅ"]; #array
$params = []; #array
triggerError($context,$contextBody,$intentName,$errorMessage,$params);
- Result statement have to be returned in the current switch case
context = false;
contextBody = [];
intentName = ["intent1"];
errorMessage = ["Query wasnt executed ๐ฅ"];
params = [];
result = 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).
- PHP
- Node.js
$context = false; #string
$contextBody = []; #array
$prompt = ["Whats your favourite food"]; #array
triggerPropmt($context,$contextBody,$prompt);
- Remember to Return the result in the current switch case
context = false;
contextbody = false;
prompt = ["Whats your favourite food"];
result = 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
- PHP
- Node.js
$contextIndex = [0]; #array
$variableName = ["fruit"]; #array
$variableValue = ["coco"]; #array
$contextBody = setContextParameter($contextIndex,$variableName,$variableValue);
contextIndex = [0];
variableName = ["fruit"];
variableValue = ["coco"];
contextBody = setContextParameter(
body,
variableName,
variableValue
contextIndex,
);
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
- PHP
- Node.js
$contextIndex = [0]; #array
$variableName = ["fruit"]; #array
$variableValue = ["Banana"]; #array
$contextBody = updateContextParameter($contextIndex,$variableName,$variableValue);
contextIndex = [0];
variableName = ["fruit"];
variableValue = ["Banana"];
contextBody = updateContextParameter(
body,
variableName,
variableValue,
contextIndex
);
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
- PHP
- Node.js
$contextIndex = [0]; #array
$parameters = [ #associative-array
'color' => 'white',
'animal' => 'bird',
'coffee' => 'expresso'
];
$contextBody = setContextParameters($contextIndex, $parameters);
contextIndex = [0];
parameters = [
'color' => 'white',
'animal' => 'bird',
'coffee' => 'expresso'
];
contextBody = setContextParameters(body,parameters,contextIndex);
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
- PHP
- Node.js
$contextIndex = [0]; #array
$parameters = [ #associative-array
'color' => 'black',
'fruit' => 'apple',
'coffee' => 'capuccinno',
'animal' => 'gorilla'
];
$contextBody = updateContextParameters($contextIndex, $parameters);
contextIndex = [0];
parameters = [
'color' => 'black',
'fruit' => 'apple',
'coffee' => 'capuccinno',
'animal' => 'gorilla'
];
contextBody = updateContextParameters(body,parameters,contextIndex);
.
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
- Node.js
$input = getInput();
- Result statement have to be returned in the current switch case
result = getInput(data);
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
- Node.js
$fileName = ["request"]; #array
createInput($fileName);
- This function doesnt need to be returned or catched, just do this in case you would like to see if worked
fileName = ["request"];
createInput(data, 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.
- PHP
- Node.js
$fileName = ["request"]; #array
getSourceData($fileName);
- This function doesnt need to be returned or catched, just do this in case you would like to see if worked
fileName = ["request"];
getSourceData(data, 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).
- PHP
- Node.js
$time = getTimeOut();
ALPHA;
.
Miscelaneous
These functions are not commonly used, but sometimes are helpfull.
getUserInput
- Returns the Input from the user that Triggered the intent
- PHP
- Node.js
$userInput = getUserInput();
ALPHA;
getSessionID
- Returns the sessionId of the current chat/session
- Id format changes depending of the platform/integration
- PHP
- Node.js
$sessionId = getSessionId();
sessionId = getSessionId(body);
getProjectName
- Retrieves the name of the project
- PHP
- Node.js
$projectName = getProjectName();
ALPHA;
getWhatsAppPhoneNumber
- Retrieves a WhatsApp PhoneNumber if you are in Session VIA whatsApp
- This data is provided for mostly wWhatsApp Providers
- PHP
- Node.js
$phoneNumber = getWhatsAppPhoneNumber();
ALPHA;
getTelegramChatId
- Returns the ChatId of a Telegram Chat
- Only obtainable if you are chatting via Telegram
- PHP
- Node.js
$chatID = getTelegramChatId();
ALPHA;
getTelegramCallbackQueryId
- Returns callbackqueryId of Telegram if exists
- Only obtainable if you are chatting via Telegram
- PHP
- Node.js
$callbackQueryId = getTelegramCallbackQueryId();
callbackQueryId = getTelegramCallbackQueryId(body);