How to get client data with $rawRequest
Aimylogic 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 VK channels, you should specify the following expressions in the Conditions block:
$channelType === "telegram"
$channelType === "vk"
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
.
VK
In the VK channel, the $rawRequest
variable returns client data in the following format:
{
"group_id": 217351294,
"type": "message_new",
"event_id": "8c35d766090285a4e0ac89109b831200547c60e0",
"v": "5.103",
"object": {
"message": {
"entity_version": 0,
"date": 1700464462,
"from_id": 1259764,
"id": 191,
"out": 0,
"attachments": [],
"conversation_message_id": 177,
"fwd_messages": [],
"important": false,
"is_hidden": false,
"peer_id": 1259764,
"random_id": 0,
"text": "89000"
},
"client_info": {
"button_actions": [
"text",
"vkpay",
"open_app",
"location",
"open_link",
"callback",
"intent_subscribe",
"intent_unsubscribe"
],
"keyboard": true,
"inline_keyboard": true,
"carousel": true,
"lang_id": 0
}
}
}
You can receive Client ID using the variable $rawRequest.object.message.from_id
.
In the script, you can generate a link to the client’s profile in VK via the Text block as follows: https://vk.com/id$rawRequest.object.message.from_id
.
Moreover, you can allow access to your bot for certain VK accounts as follows:
Here, 1234567
is a client’s ID.
From the condition, create a welcome screen.
From the else option, create a screen with the text: Access denied
.
In the same way, you can deny access for the list of accounts.
To do it, specify accounts in the Conditions block, connect these conditions with the Access denied
screen, and connect the else option with the welcome screen.
The $rawRequest
variable is part of $request
. You can get the client’s first and last name through $request.userFrom.firstName
and $request.userFrom.lastName
.
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
.
Alice
The $rawRequest
variable returns the following from the Yandex Alice channel:
{
"meta": {
"locale": "ru-RU",
"timezone": "UTC",
"client_id": "ru.yandex.searchplugin/7.16 (none none; android 4.4.2)",
"interfaces": {
"screen": {},
"payments": {},
"account_linking": {}
}
},
"session": {
"message_id": 0,
"session_id": "23c0b51a-8a66-4bfd-b21f-ec825e8599ee",
"skill_id": "5f93817f-2a48-4de1-b509-72b0e041b35c",
"user": {
"user_id": "E112A36A50441869F6CEC8G1CC240607D3061A6C21030D22ACD9D9424605B592"
},
"application": {
"application_id": "20C726B0F7E65E595BF89F3CF7870C0FF930D889237E8B916C63137DC4BE1125"
},
"new": true,
"user_id": "20C726B0F7E65C595AM89F3CF7870C0FF930D889217E8B916C63137DC4BE1125"
},
"request": {
"command": "",
"original_utterance": "",
"nlu": {
"tokens": [],
"entities": [],
"intents": {}
},
"markup": {
"dangerous_context": false
},
"type": "SimpleUtterance"
},
"version": "1.0"
}
Telephony
In the phone channel, the $rawRequest
variable returns data in the following format:
{
"event": "accepted",
"trunkId": 0,
"accountId": 247854563,
"caller": "79123456789",
"extension": "79123456789",
"channel": "SIP/0-0000001f",
"originateData": {
"callTaskId": 12754,
"callId": 12757,
"botToken": "TufcgsZw:d82fe12af68ff365455d1365e33fc7f3487699c1",
"payload": {
"name": "Alex",
"phone": "79123456789"
// 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"
}