> ## Documentation Index
> Fetch the complete documentation index at: https://takeprofit.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Telegram Notifications via webhook

> This guide will help you configure automatic notifications from the **TakeProfit** platform to **Telegram** using your own bot and Webhook. This allows you to receive timely alerts directly in the messenger, enhancing your workflow efficiency.

## Creating a Telegram Bot

Begin by creating your own Telegram bot, which will send you messages.

### Obtaining the API Token

1. **Open Telegram** and search for [@BotFather](https://t.me/BotFather).
2. **Create a new bot:**
   * Send the command `/newbot`.
   * Follow the instructions to set your bot's name and username.
3. **Receive the API token:**
   * After creation, BotFather will send you an API token. Example: `123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ`.
   * **Save this token** securely; you'll need it for further setup.

***

## Obtaining the Chat ID

To send messages, you need the chat identifier (`chat_id`) where the bot will send notifications.

### For Personal Messages

1. **Start a conversation with your bot:**
   * Open a chat with your bot and send `/start`.
2. **Get your `User ID`:**
   * Search for [@userinfobot](https://t.me/userinfobot).
   * Send any message to it.
   * It will reply with your `User ID` and other information.

### For Group Chats

1. **Create or use an existing group.**
2. **Add your bot to the group.**
3. **Obtain the group's `Chat ID`:**
   * Add [@userinfobot](https://t.me/userinfobot) to the same group.
   * Send a message in the group.
   * [@userinfobot](https://t.me/userinfobot) will provide the group's `Chat ID`.

**Important:** Ensure your bot has the necessary permissions in the group to send messages.

***

## Configuring Webhook in TakeProfit

Setting up a Webhook allows the TakeProfit platform to send notifications to your bot.

### Preparing the Webhook URL

Create the URL for the Telegram API:

```
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage
```

* **Replace** `<YOUR_BOT_TOKEN>` with the token from step 1.

**Example:**

```
https://api.telegram.org/bot123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ/sendMessage
```

### Setting Up Webhook in the Platform

1. **Log in** to your **TakeProfit** account.
2. **Create a new alert** or edit an existing one.
3. **Set up alert conditions:**
   * Specify parameters like ticker, exchange, trigger condition, etc.
4. **Add the Webhook:**
   * In the notifications section, select **Webhook URL**.
   * **Paste the Webhook URL** you prepared.
5. **Configure the Payload:**
   * In the **Message** field, enter JSON with `chat_id` and `text` parameters.

**Example Payload:**

```json theme={null}
{
	"chat_id": "YOUR_CHAT_ID",
	"text": "YOUR_MESSAGE"
}
```

* **Replace** `YOUR_CHAT_ID` with your `User ID` or group `Chat ID`.
* **Replace** `YOUR_MESSAGE` with your notification text or use TakeProfit variables.

***

## Forming a Message Using Variables

### Using TakeProfit Variables

TakeProfit allows the use of variables for dynamic message content.

**Common variables:**

* `{{ticker}}` — The instrument's ticker.
* `{{exchange}}` — Exchange name.
* `{{condition_type}}` — Type of trigger condition.
* `{{condition_source_1}}` — Condition source (e.g., price).

**Example in the Message field:**

```json theme={null}
{
	"chat_id": "987654321",
	"text": "Alert for {{ticker}} on {{exchange}}: {{condition_type}} at {{condition_source_1}}"
}
```

When the alert triggers, the variables will be replaced with real-time values.

***

## Configuration Examples

### Example 1: Simple Text Message

**Details:**

* **Bot Token:** `123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ`
* **Chat ID:** `987654321`
* **Message:** `BTC price exceeded $50,000`

**Webhook URL:**

```
https://api.telegram.org/bot123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ/sendMessage
```

**Payload in the Message field:**

```json theme={null}
{
	"chat_id": "987654321",
	"text": "{{ticker}} price exceeded ${{close}}"
}
```

### Example 2: Sending JSON Data

**Objective:** Send structured JSON with alert data.

**Payload:**

```json theme={null}
{
	"chat_id": "987654321",
	"text": "{ \"symbol\": \"{{ticker}}\", \"exchange\": \"{{exchange}}\", \"price\": \"{{close}}\" }"
}
```

**Important:** Escape quotes inside `text` with a backslash (`\`).

**Example of the final message:**

```json theme={null}
{
	"symbol": "BTC/USD",
	"exchange": "Binance",
	"price": "50000"
}
```

***

## Testing the Configuration

1. **Create a test alert** with the settings you've configured.
2. **Check for the message** in your Telegram app.
   * If the message doesn't arrive, double-check your settings and JSON formatting.
3. **Verify the data**, ensuring variables are correctly replaced.

***

## Troubleshooting

* **Messages are not delivered:**
  * Ensure the `chat_id` and bot token are correct.
  * Confirm the bot isn't blocked and has access to the chat or group.
* **Formatting errors:**
  * Use a JSON validator to check the Payload's correctness.
  * Make sure all special characters are properly escaped.
* **Variables are not replaced:**
  * Verify the variables are spelled correctly.
  * Ensure the variables are supported by TakeProfit.

***

## Helpful Tips

* **Using Telegram Formatting:**

  * Add `"parse_mode": "Markdown"` or `"parse_mode": "HTML"` to the Payload for text formatting.
  * **Example:**

    ```json theme={null}
    {
    	"chat_id": "987654321",
    	"text": "*BTC price exceeded $50,000!*",
    	"parse_mode": "Markdown"
    }
    ```

* **Sending Messages to Multiple Chats:**

  * Create multiple alerts with different `chat_id`s.
  * Alternatively, implement distribution within the bot (requires additional programming).

* **Security:**

  * Never share your bot's token with anyone.
  * Ensure the Webhook URL doesn't contain sensitive information.

***

## Conclusion

You have now set up integration between **TakeProfit** and **Telegram** using your own bot. This enables you to receive instant notifications about important events and respond promptly to market changes.

If you encounter questions or difficulties, refer to the official Telegram Bot API documentation or contact TakeProfit support.

***

**Useful Links:**

* [Telegram Bot API Documentation](https://core.telegram.org/bots/api#sendmessage)
* [TakeProfit — Official Website](https://takeprofit.com/)
* [JSON Validator](https://jsonlint.com/)

***

**Note:** Always verify the APIs and functions you use are up to date, as they may change over time.
