# Пользовательские переменные

## Получить список переменных

<mark style="color:blue;">`GET`</mark> `https://app.onebot.tech/api/v1/getContactVariables`

Этот метод позволяет получить список пользовательских переменных для определенного контакта.

#### Path Parameters

| Name        | Type   | Description |
| ----------- | ------ | ----------- |
| contact\_id | String | ID контакта |

{% tabs %}
{% tab title="200: OK Запрос успешно обработан" %}

```javascript
{
  "data": [
    {
      "id": 1,
      "value": "Значение переменной",
      "created_at": "2022-11-22T20:54:20+00:00",
      "updated_at": "2022-11-22T20:54:31+00:00",
      "variable": {
        "id": 14,
        "name": "Имя переменной",
        "created_at": "2022-11-11T20:54:20+00:00",
        "updated_at": "2022-11-11T20:54:20+00:00"
      }
    },
    {
      "id": 2,
      "value": "Киев",
      "created_at": "2022-11-11T20:54:20+00:00",
      "updated_at": "2022-11-11T20:54:31+00:00",
      "variable": {
        "id": 25,
        "name": "Город",
        "created_at": "2022-11-22T20:54:20+00:00",
        "updated_at": "2022-11-22T20:54:20+00:00"
      }
    }
  ],
  "links": {
    "first": "https:\/\/onebot.tech\/api\/v1\/getContactVariables?page=1",
    "last": "https:\/\/onebot.tech\/api\/v1\/getContactVariables?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 1,
    "path": "https:\/\/onebot.tech\/api\/v1\/getContactVariables",
    "per_page": 100,
    "to": 2,
    "total": 2
  }
}
```

{% endtab %}

{% tab title="422: Unprocessable Entity Переданные данные некорректны." %}

```javascript
{
  "message": "The given data was invalid.",
  "errors": {
    "contact_id": [
      "Поле contact id обязательно для заполнения."
    ]
  }
}
```

{% endtab %}
{% endtabs %}

## Создать/обновить переменную

<mark style="color:green;">`POST`</mark> `https://app.onebot.tech/api/v1/setContactVariable`

Этот метод позволяет создать или обновить пользовательскую переменную. Если такой переменной не существует в боте, то она создастся и привяжется к пользователю.

#### Query Parameters

| Name        | Type    | Description                                                                                                                                                  |
| ----------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| contact\_id | Integer | ID контакта                                                                                                                                                  |
| name        | String  | Имя переменной                                                                                                                                               |
| value       | String  | Значение переменной                                                                                                                                          |
| deletable   | Integer | <p>Возможные значения: </p><p>0 - переменная не должна удалиться после заявки </p><p>1 - переменная должна удалиться после заявки </p><p>По умолчанию: 0</p> |

{% tabs %}
{% tab title="200: OK Переменная успешно создана/отредактирована." %}

```javascript
{
  "data": {
    "id": 2,
    "value": "Значение переменной",
    "created_at": "2022-11-11T11:46:15+00:00",
    "updated_at": "2022-11-11T11:46:28+00:00",
    "variable": {
      "id": 1,
      "name": "Имя переменной",
      "created_at": "2022-11-23T11:46:15+00:00",
      "updated_at": "2022-11-23T11:46:15+00:00"
    }
  }
}
```

{% endtab %}

{% tab title="403: Forbidden Доступ запрещен" %}

```javascript
{
    "message": "Forbidden"
}
```

{% endtab %}

{% tab title="422: Unprocessable Entity Переданы некорректные данные" %}

```javascript
{
  "message": "The given data was invalid.",
  "errors": {
    "contact_id": [
      "Поле contact id обязательно для заполнения."
    ],
    "name": [
      "Поле Имя обязательно для заполнения."
    ],
    "value": [
      "Поле value обязательно для заполнения."
    ]
  }
}
```

{% endtab %}
{% endtabs %}

## Удалить переменную по ID

<mark style="color:green;">`POST`</mark> `https://app.onebot.tech/api/v1/deleteContactVariable`

Этот метод позволяет удалить пользовательскую переменную по ID переменной контакта

#### Query Parameters

| Name | Type    | Description            |
| ---- | ------- | ---------------------- |
| id   | Integer | ID переменной контакта |

{% tabs %}
{% tab title="204: No Content Переменная успешно удалена." %}

```javascript
{
    // Response
}
```

{% endtab %}

{% tab title="403: Forbidden Доступ запрещен." %}

```javascript
{
    // Response
}
```

{% endtab %}

{% tab title="422: Unprocessable Entity Переданы некорректные данные." %}

```javascript
{
  "message": "The given data was invalid.",
  "errors": {
    "id": [
      "Поле id обязательно для заполнения, когда Имя не указано."
    ],
    "contact_id": [
      "Поле contact id обязательно для заполнения, когда id не указано."
    ],
    "name": [
      "Поле Имя обязательно для заполнения, когда id не указано."
    ]
  }
}
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
**Обратите внимание!** \
Нужно передавать ID переменной контакта, а не ID переменной бота.&#x20;
{% endhint %}

```
{    
    "id": 1, // ID переменной контакта
    "value": "Значение переменной",
    "created_at": "2022-11-22T20:54:20+00:00",
    "updated_at": "2022-11-22T20:54:31+00:00",
    "variable": {
        "id": 2, // ID переменной бота
        "name": "Имя переменной",
        "created_at": "2022-11-22T20:54:20+00:00", 
        "updated_at": "2022-11-22T20:54:20+00:00"
    }
}
```

## Удалить переменную по ее имени и ID контакта

<mark style="color:green;">`POST`</mark> `https://app.onebot.tech/api/v1/deleteContactVariable`

Этот метод позволяет удалить пользовательскую переменную по ID контакта и имени переменной.

#### Query Parameters

| Name        | Type    | Description    |
| ----------- | ------- | -------------- |
| contact\_id | Integer | ID контакта    |
| name        | String  | Имя переменной |

{% tabs %}
{% tab title="200: OK Запрос успешно обработан" %}

```javascript
{
    // Response
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.onebot.tech/onebot-help/rabota-s-api-platformy/kontakty/polzovatelskie-peremennye.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
