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 city?
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 city 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.message.from_id
-
From the Conditions block, make a connection with an HTTP request to get the user’s city 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:
$city
variable name and$httpResponse.response[0].city.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=city&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 city. 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.199
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:
https://api.vk.com/method/users.get?user_ids=${id}&fields=city&access_token=vk1.a.l7Gv1Vdy-9rUb6dUY_2YGHF8Wdvuw8mj4tnE3pQV10R5_HOSzTPv7NbBlUZvYiJIxKTTBEM2tOX51a6bbN3TDgSDvyx1UzwnQtmpwMQZ97ivh54p2KBjx0qmNGAGA_v8z8YoYbsjQp-VVz2laQQVYihyUiNygCN_7yZqFOk24J3h0FpSFdUydIBN8sY_yWhESciiaQghf1BcMSzlbHG7Rw&v=5.199
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 city 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 $city
(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 city of residence.
Next, after the Success
option, we link to a text block containing the following: You live in $newcity, 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 city, 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.199
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.199
- Variable name
$cellphone
- Value
$httpResponse.response[0].mobile_phone