Skip to main content

Send a Message to Telegram

This guide will walk you through setting up a webhook to send messages to a Telegram chat.

First, you'll need a Telegram Bot. If you don't have one, follow the official Telegram Bot Tutorial to create one and get your Bot Token.


Step 1: Create a Connection

The first step is to create a new Connection that will point to Telegram's API.

  1. In your dashboard, create a new Connection.

  2. For the Destination URL, use the Telegram sendMessage API endpoint. Make sure to replace {{BOT_TOKEN}} with your bot's token. It's highly recommended to store your token as a secret.

    https://api.telegram.org/bot{{BOT_TOKEN}}/sendMessage

Creating a new connection Editing a destination Adding a secret


Step 2: Configure the Transformation

Next, you need to create a Transformation. This will format the incoming data from your webhook into the structure that the Telegram API requires.

  1. Click the gear icon on your connection to open the settings panel.
  2. Navigate to the Transformations tab and click New Transformation.

Editing the connection settings Adding the transformation

  1. Replace the default code with the following Javascript snippet. This script sets the correct headers and constructs the message body.

    // Formats the request for the Telegram Bot API
    addHandler('transform', (request, context) => {
    // You can access incoming webhook data from the context object.
    // For example, if your webhook sends `{"message": "Hello!"}`,
    // you can use `context.body.message`.
    const messageText = context.body.message || 'This is a test message.';

    request.headers = {
    'Content-Type': 'application/json'
    };
    request.method = 'POST';
    request.body = JSON.stringify({
    // Replace with your target Telegram Chat ID.
    // You can get this from a bot like @userinfobot.
    chat_id: 'YOUR_CHAT_ID',
    text: messageText,
    parse_mode: 'Markdown' // Other options: 'HTML'
    });

    return request;
    });
  2. Important:

    • Replace 'YOUR_CHAT_ID' with the actual Chat ID of the user or group you want to send the message to.
    • Customize the text field to use data from your incoming webhook (e.g., context.body.message).

Step 3: Save and Test

Finally, save your transformation and the connection.

That's it! 🚀 Trigger your webhook source to send a request. If everything is configured correctly, your message will appear in the specified Telegram chat.