# Choosing a contact

A request to the ["Incoming webhook"](https://support.watbot.org/chatbots/blocks/developer/incoming-webhook) block comes not from a person but from a third-party service. So the code itself says which contact the request belongs to — with one of the functions below. They exist only in this block; in the other blocks the contact is already known.

Common rules for all functions:

- The contact is searched for only among this bot's contacts. Deleted contacts are not found.
- **Returns** `true` if the contact is found, and `false` if not.
- Only one function can be called per request, and only once. A second call — even of a different function — ends the code with an error, and the block responds with code `422`.
- At least one function must be written in the code. Without it, the block does not run the code and responds with `422`.
- Once the contact is found, the code can use the contact's [built-in variables](https://support.watbot.org/api/javascript/builtins#contact-data), variables and tags. Before that, the contact's variables are empty, and referring to `id`, `name` and other contact data stops the code with an error.
- If the contact is not found or no function was called, the code runs but saves nothing: variables, tags and global variables are not written, messages are not sent, the jump and the "Continue" connection do not fire. The block responds with `200` and the message `Code executed without setting contact`.

## setContactById

```js
setContactById(id)
```

Finds the contact by their ID on the platform.

| Parameter | Type | What to pass |
|---|---|---|
| `id` | A number or a string of digits | The contact ID. In a scenario you can get it with the [template](https://support.watbot.org/chatbots/templates/data#contact-data) `{{ id }}` |

```js
if (setContactById(request.query.contact_id)) {
    setContactVariable('payment', 'received');
    sendMessage('Payment received, we are starting to put your bouquet together.');
}
```

For the third-party service to know the ID, pass it in advance — for example, in the payment link or with an "HTTP request" block.

## setContactByTelegramId

```js
setContactByTelegramId(id)
```

Finds the contact by the person's ID in Telegram.

| Parameter | Type | What to pass |
|---|---|---|
| `id` | A number or a string of digits | The Telegram user ID — the same as in the `telegram_id` variable |

```js
var message = request.data.message;

if (message && message.from && setContactByTelegramId(message.from.id)) {
    setContactTag('wrote_in_group');
}
```

## setContactByMaxId

```js
setContactByMaxId(id)
```

Finds the contact by the person's ID in MAX.

| Parameter | Type | What to pass |
|---|---|---|
| `id` | A number or a string of digits | The MAX user ID — the same as in the `max_id` variable |

```js
if (setContactByMaxId(request.data.user_id)) {
    sendMessage('The courier is on the way.');
}
```

## setContactByVkId

```js
setContactByVkId(id)
```

Finds the contact by the person's ID in VK.

| Parameter | Type | What to pass |
|---|---|---|
| `id` | A number or a string of digits | The VK user ID — a number without `id` at the start |

```js
if (setContactByVkId(request.data.vk_user_id)) {
    setContactTag('left_review');
}
```

## setContactByViberId

```js
setContactByViberId(id)
```

Finds the contact by the person's ID in Viber.

| Parameter | Type | What to pass |
|---|---|---|
| `id` | String | The Viber user ID — a string of letters, digits and symbols; do not turn it into a number |

```js
if (setContactByViberId(request.data.viber_id)) {
    sendMessage('Your order has been handed over for delivery.');
}
```
