How to get information about a VK user
In this article, we will tell you how to get user information using VK API.
Basic data such as username, last name and id can be obtained using the $rawRequest variable. And to get additional information, such as, for example, the user’s date of birth and phone number, we will use the VK API. In this article, we will give an example of how you can use the VK API. You can find documentation on using the VK API here: https://vk.com/dev/first_guide
API (application programming interface) is an intermediary between an application developer and any environment with which this application must interact. The API simplifies code generation by providing a set of ready-made classes, functions, or structures to work with existing data.
If your bot is published not only in VK, but also in another channel, then you can use the $channelType system variable to determine the channel
Remember that you need to test the functionality described in this manual in VK. If you use the functionality described below and test it in the Aimylogic test widget, you will receive an error, since the bot is trying to use the VK API and, of course, cannot do it when testing outside VK.
Before proceeding with the instructions, create a basic script consisting of a couple of text screens (welcome + phrases to accept user input), and publish it to VK, as described in this article.
How can I get the user’s country?
Please note that you’ll see this data only if it has been specified in the profile and is not covered by the privacy settings. If the user has not specified the country or the data is available to a limited circle of people, we will not be able to access this information.
Add the Transition block to the welcome screen in your script and connect it to the conditions block, with which we will get the user ID:
$id = $rawRequest.object.user_id
From the Conditions block, make a connection with an HTTP request to get the user’s country from the information about the VK page.
This is what this HTTP request will look like:
- Method —
GET
- The RESPONSE tab of the HTTP request must contain:
$country
variable name and$httpResponse.response[0].country.title
value - You don’t need to fill in the BODY and HEADERS tabs.
- The request URL looks like this:
https://api.vk.com/method/users.get?user_ids=${id}&fields=country&access_token=ACCESS_TOKEN&v=5.101
- Copy this URL, substituting
ACCESS_TOKEN
with the key you received when adding the bot to the community. You can always get this token again in the community settings: Settings — API usage — Access tokens. Learn more about adding a VK channel
- Copy this URL, substituting
- Method —
Let’s understand the structure of this url
We are using the users.get
method. You can read about this method here.
Since we previously got the user ID using the conditions block, our request contains the following value: user_ids=${id}
, instead of {id}
, the real VK user ID will be used.
We use the fields
parameter of the users.get
method
In the fields
parameter we use the value country. This is also defined by the VK API documentation.
access_token
defines our channel token. Therefore, instead of ACCESS_TOKEN
, you need to insert the key obtained from the VK.
And, finally, 5.101
is the current version of the VK API protocol. You need to indicate the current version of the VK API protocol. You can see which version is current at the moment here (the topmost version is current):
You might end up with a URL like this:
We need to assign all the received values to the corresponding variables, which we do by adding a variable to the HTTP request on the RESPONSE tab and writing the received information to this variable.
From the Error
option, make a link to a text block with the text Error $httpStatus
This block will help us understand what the error is if the request is not successful.
If you get an error, go back and make sure you completed your request correctly.
Make sure we get this information from the user
Now, let’s check whether we received this information — whether the country is indicated for this user, and whether this information is not covered by privacy settings. Let’s connect the Success
option of the HTTP request to the Conditions block, in which we write $country
(the same variable that we specified in the HTTP request)
If this condition is met, we can move through the scenario (we will add an HTTP request below). And if not (option else
), add a Text block asking about the country of residence.
Next, after the Success
option, we link to a text block containing the following: You live in $newcountry, right?
How can I get other data?
You can request other data using the users.get
method. Follow the steps in the instructions for obtaining the user’s country, except for the following changes.
How can I get the date of birth?
URL for getting date of birth:
https://api.vk.com/method/users.get?user_ids=${id}&fields=bdate&access_token=ACCESSTOKEN&v=5.101
Here we are using the bdate
parameter of the users.get
method
- Variable name:
bd
- Value:
$httpResponse.response[0].bdate
How do I request a phone number?
Request URL: https://api.vk.com/method/users.get?user_ids=${id}&fields=contacts&access_token=ACCESSTOKEN&v=5.101
- Variable name
$cellphone
- Value
$httpResponse.response[0].mobile_phone
You can get the phone number and city if you add to HTTP $city$country$cellphone$bd
Now, we can send the received data using a regular text block:
Your country is $country.response[0].country.title
Your birthday is $bd.response[0].bdate
Your city is $city.response[0].city.title
Your phone is $cellphone.response[0].mobile_phone
Subsequently, you can refine the script by thinking over the logic of the transition depending on the values of these variables.