How to get client data with $rawRequest
Tovie DialogStudio has the $rawRequest
variable that can be used to obtain client data.
You can use the contents of the $rawRequest
variable to:
- address clients by name;
- restrict access to the bot for a certain list of clients;
- collect and transfer client data.
How to define channel type
The $rawRequest
variable contains the text of the current client’s request.
The format and contents of the $rawRequest
variable depend on the channel.
If your bot is connected to multiple channels, you need to determine the channel type before you start using the $rawRequest
variable. The channel type is stored in the $channelType
variable.
You can determine the channel type with the help of the Conditions block by specifying expressions for each channel.
For example, if you use Telegram and Facebook channels, you should specify the following expressions in the Conditions block:
$channelType === "telegram"
$channelType === "facebook"
How can I test the script?
$rawRequest
.If you add the $rawRequest
variable to your script and then click Test, you will get an error. The bot does not get the data because you are not testing the bot in the channel.
To test the script, connect the channel and talk to the bot in this channel.
If you want to test the script in a test widget, first create a script, test it, and after that add the $rawRequest
variable.
Telegram
The $rawRequest
variable returns data from Telegram in the following format:
{
"update_id": 123456789,
"message": {
"message_id": 67,
"from": {
"id": 123456789,
"is_bot": false,
"first_name": "John",
"username": "johndoe",
"language_code": "en"
},
"chat": {
"id": 123456789,
"first_name": "Doe",
"username": "johndoe",
"type": "private"
},
"date": 1560773004,
"text": "/start",
"entities": [
{
"offset": 0,
"length": 6,
"type": "bot_command"
}
]
}
}
If you use inline buttons in your script, the $rawRequest
variable will return data in the following format:
{
"update_id": 123456789,
"callback_query": {
"message_id": 67,
"from": {
"id": 123456789,
"is_bot": false,
"first_name": "John",
"username": "johndoe",
"language_code": "en"
},
"chat": {
"id": 123456789,
"first_name": "Doe",
"username": "johndoe",
"type": "private"
},
"date": 1560773004,
"text": "/start",
"entities": [
{
"offset": 0,
"length": 6,
"type": "bot_command"
}
]
}
}
You can address the client by name using a Text block:
You can use other elements of the array you received from $rawRequest
:
- Username via
$rawRequest.message.from.username
. - Client’s language via
$rawRequest.message.from.language_code
. - Chat ID via
$rawRequest.message.from.id
.
JivoChat
The $rawRequest
variable returns the following from the JivoChat channel:
{
"message": {
"type": "TEXT",
"text": "Hey",
"timestamp": 1616147069
},
"id": "b3418c9e-8897-11eb-8931-79179b127c32",
"event": "CLIENT_MESSAGE",
"sender": {
"id": "300",
"name": "John",
"url": "https://app.example.com/"
},
"startProcessingTime": 36460577079987682,
"client_id": "300",
"chat_id": "481"
}
Here you can get the client’s name using $rawRequest.sender.name
.
Telephony
In the phone channel, the $rawRequest
variable returns data in the following format:
{
"event": "accepted",
"trunkId": 0,
"accountId": 247854563,
"caller": "16123456789",
"extension": "16123456789",
"channel": "SIP/0-0000001f",
"originateData": {
"callTaskId": 12754,
"callId": 12757,
"botToken": "TufcgsZw:d82fe12af68ff365455d1365e33fc7f3487699c1",
"payload": {
"name": "Alex",
"phone": "16123456789"
// Additional data
}
}
}
Here you can get the client’s name, phone number, and other data.
Additional actions
You can also send $rawRequest
values to your own account in Telegram messages, in an email via IFTTT, or write them to a cell in a Google Sheet.
For example, here is an example of an HTTP request for sending a client’s name in a Telegram message:
{
"chat_id": 123456780,
"text": "Client name: $rawRequest.message.from.first_name"
}