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.
-
In your dashboard, create a new Connection.
-
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
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.
- Click the gear icon on your connection to open the settings panel.
- Navigate to the Transformations tab and click New Transformation.
-
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;
}); -
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
).
- Replace
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.