# Telegram Bot

A Telegram Bot is an automated program that interacts with users and groups on Telegram. These bots can perform various functions such as sending messages, managing groups, processing commands, and integrating with third-party services. They operate through the Telegram Bot API, allowing businesses, developers, and individuals to automate workflows, provide customer support, and enhance communication.

### **How to Integrate Telegram Bot with ZBrain Flow** <a href="#how-to-integrate-telegram-bot-with-zbrain-flow" id="how-to-integrate-telegram-bot-with-zbrain-flow"></a>

#### **Steps to Integrate Telegram Bot** <a href="#steps-to-integrate-telegram-bot" id="steps-to-integrate-telegram-bot"></a>

1. **Click the ‘+’ Button in ZBrain Flow:** Start a new integration.
2. **Search for Telegram Bot:** Type "Telegram Bot" in the search bar.
3. **Choose an Action:** Select the action you want the bot to perform, such as sending messages or managing group members.

### **How to Create a Telegram Bot Connection in ZBrain** <a href="#how-to-create-a-telegram-bot-connection-in-zbrain" id="how-to-create-a-telegram-bot-connection-in-zbrain"></a>

To establish a connection between Telegram and ZBrain Flow, follow these steps:

#### **Steps to Connect Telegram Bot** <a href="#steps-to-connect-telegram-bot" id="steps-to-connect-telegram-bot"></a>

1. **Provide a Connection Name:** Assign a unique name to identify the connection.
2. **Enter the Bot Token:** This token grants API access to your Telegram bot.
3. **Click ‘Save’** to finalize and establish the connection.

### **How to Get a Bot Token for Your Telegram Bot** <a href="#how-to-get-a-bot-token-for-your-telegram-bot" id="how-to-get-a-bot-token-for-your-telegram-bot"></a>

To obtain a bot token, follow these steps:

1. **Open Telegram:** Launch the Telegram app.
2. **Search for BotFather:** Type `@BotFather` in the search bar and select it.
3. **Start a Chat with BotFather:**
   * Send the command `/start` to begin.
   * To create a new bot, send `/newbot`.
4. **Follow the Prompts:**
   * Provide a name for your bot.
   * Assign a unique username ending with "bot" (e.g., `MyAwesomeBot`).
5. **Receive Your Token:**
   * BotFather generates and provides a unique API token.
   * Copy and securely store this token for authentication.

### **How to Get the Chat ID and User ID in Telegram** <a href="#how-to-get-the-chat-id-and-user-id-in-telegram" id="how-to-get-the-chat-id-and-user-id-in-telegram"></a>

#### **Method 1: Using @getmyid\_bot** <a href="#method-1-using-getmyid_bot" id="method-1-using-getmyid_bot"></a>

1. Open Telegram and search for `@getmyid_bot`.
2. Start a chat with the bot by tapping ‘Start’ or sending the command /start.
3. The bot will reply with a message containing your Chat ID.

#### **Method 2: Using a Telegram Bot** <a href="#method-2-using-a-telegram-bot" id="method-2-using-a-telegram-bot"></a>

1. **Create or Use an Existing Bot**
   * If you don’t have a bot, create one using BotFather.
   * Get the bot token from BotFather.
2. **Send a Message to Your Bot**
   * Open a chat with your bot and send any message.
3. **Use the getUpdates API Method**
   * Open your browser and enter the following URL, replacing `<YourBOTToken>` with your bot’s API token:

{% code lineNumbers="true" %}

```
https://api.telegram.org/bot<YourBOTToken>/getUpdates
```

{% endcode %}

1. **Check the API Response**
   * The response will contain user details, including the chat ID and user ID.
   * Example response:

{% code lineNumbers="true" %}

```
{
  "ok": true,
  "result": [
    {
      "update_id": 123456789,
      "message": {
        "message_id": 1,
        "from": {
          "id": 987654321,  // User ID
          "is_bot": false,
          "first_name": "John",
          "username": "john_doe",
          "language_code": "en"
        },
        "chat": {
          "id": 987654321,  // Chat ID
          "first_name": "John",
          "username": "john_doe",
          "type": "private"
        },
        "date": 1618325643,
        "text": "Hello, bot!"
      }
    }
  ]
}
```

{% endcode %}

#### **Method 3: Using @userinfobot** <a href="#method-3-using-userinfobot" id="method-3-using-userinfobot"></a>

1. Open Telegram and search for `@userinfobot`.
2. Start a chat with the bot.
3. It will display your user ID and chat ID.
4. To get a group chat ID, add `@userinfobot` to the group and send a message. The bot will reply with the group chat ID.

#### **Method 4: Using @RawDataBot** <a href="#method-4-using-rawdatabot" id="method-4-using-rawdatabot"></a>

1. Search for `@RawDataBot` in Telegram.
2. Start a chat with the bot.
3. The bot will provide your chat ID and user ID, along with additional raw data.

### **Actions Available in Telegram Bot Integration** <a href="#actions-available-in-telegram-bot-integration" id="actions-available-in-telegram-bot-integration"></a>

#### **1. Send Text Message** <a href="#id-1.-send-text-message" id="id-1.-send-text-message"></a>

Sends a text message to users, groups, or channels.

**Steps:**

* Enter the Bot Token.
* Enter the Chat ID (identifies the recipient).
* Enter the Message Thread ID (for forum supergroups only).
* Select the Format – Markdown or HTML for message styling.
* Enable/Disable Web Page Preview – Controls link previews in messages.
* Enter the Message to be sent.
* Enter Reply Markup – Optional, used for buttons or keyboard options.

#### **2. Send Media** <a href="#id-2.-send-media" id="id-2.-send-media"></a>

Sends images, videos, stickers, or GIFs.

**Steps:**

* Enter the Bot Token.
* Enter the Chat ID.
* Enter the Message Thread ID (for forum supergroups only).
* Select the Media Type – Choose from image, video, sticker, or GIF.
* Select the Format – Markdown or HTML for captions.
* Enable/Disable Web Page Preview – Controls link previews.
* Enter the Message to accompany the media.
* Enter Reply Markup – Optional, used for interactive buttons.

#### **3. Get Chat Member** <a href="#id-3.-get-chat-member" id="id-3.-get-chat-member"></a>

Retrieves information about a user in a group, supergroup, or channel.

**Steps:**

* Enter the Bot Token.
* Enter the Chat ID of the group or channel.
* Enter the User ID of the member to retrieve details.

#### **4. Create Invite Link** <a href="#id-4.-create-invite-link" id="id-4.-create-invite-link"></a>

Generates a new invite link for a chat or group with specific access parameters.

**Steps:**

* Enter the Bot Token.
* Enter the Chat ID where the invite link should be created.
* Provide the Name of the invite link (max 32 characters).
* Specify the Expiration Date (point in time when the link will expire).
* Enter the Member Limit – Maximum users who can join via this link (1-99,999).

#### **5. Custom API Call** <a href="#id-5.-custom-api-call" id="id-5.-custom-api-call"></a>

Allows you to send customized API requests to Telegram Bot's API for advanced functionality.

**Steps:**

**1. Enter the Bot Token**

The bot token is essential for authenticating API requests.

**2. Provide the API Endpoint**

The API endpoint is the specific URL where the request is sent. You can either:

* Use the full URL for direct API calls.
* Use the relative path if working within a system that appends the base URL.

**Example API Endpoints:**

* **Send a Message:**

{% code lineNumbers="true" %}

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

{% endcode %}

* **Get Chat Details:**

{% code lineNumbers="true" %}

```
https://api.telegram.org/bot<YourBOTToken>/getChat
```

{% endcode %}

* **Relative Path Example:**

{% code lineNumbers="true" %}

```
/bot<YourBOTToken>/getUpdates
```

{% endcode %}

**3. Select the HTTP Method**

The HTTP method determines the type of action performed:

| **Method** | **Description**                                      |
| ---------- | ---------------------------------------------------- |
| **GET**    | Retrieve chat, user, or message details.             |
| **POST**   | Send new messages, media, or commands.               |
| **PATCH**  | Modify existing data, like editing messages.         |
| **PUT**    | Update bot settings or configurations.               |
| **DELETE** | Remove messages, revoke access, or delete chat data. |
| **HEAD**   | Fetch response headers without retrieving full data. |

**4. Add Headers**

Headers provide additional metadata and authentication details for requests.

* Authorization headers are automatically included when using the bot token.
* You can add custom headers by clicking ‘+ Add Item’ for additional functionality.

**Example Headers:**

{% code lineNumbers="true" %}

```
{
  "Content-Type": "application/json",
  "Accept": "application/json"
}
```

{% endcode %}

**5. Add Query Parameters**

Query parameters allow you to filter results, set options, or customize API calls.

**Example Query Parameters:**

* To limit the number of updates retrieved:

{% code lineNumbers="true" %}

```
GET https://api.telegram.org/bot<YourBOTToken>/getUpdates?limit=5
```

{% endcode %}

* To specify a particular user in a group:

{% code lineNumbers="true" %}

```
GET https://api.telegram.org/bot<YourBOTToken>/getChatMember?chat_id=-123456789&user_id=987654321
```

{% endcode %}

**How to Add Query Parameters:**

* Click `+ Add Item` to include multiple parameters.
* Ensure values are correctly formatted to avoid request failures.

**6. Enter JSON Body**

For POST, PUT, and PATCH requests, structured data must be sent in JSON format.

**Example JSON Body for Sending a Message:**

{% code lineNumbers="true" %}

```
{
  "chat_id": "123456789",
  "text": "Hello, this is a test message!",
  "parse_mode": "Markdown"
}
```

{% endcode %}

**Example JSON Body for Sending Media:**

{% code lineNumbers="true" %}

```
{
  "chat_id": "123456789",
  "photo": "https://example.com/image.jpg",
  "caption": "Here is an image",
  "parse_mode": "HTML"
}
```

{% endcode %}

**7. Set Timeout**

The timeout setting ensures that requests do not run indefinitely.

* Define the timeout in seconds to prevent API stalling.

**Example Timeout Configuration:**

{% code lineNumbers="true" %}

```
Timeout: 15 seconds
```

{% endcode %}

**Note:** Click 'f' to insert dynamic values for flexible configurations.
