Building a Telegram Bot with n8n and Webhooks (Free JSON Template)
Building real-time interactive notifications and automated customer response systems no longer requires writing custom server backends from scratch. Utilizing an n8n telegram bot template json configuration gives you an event-driven, production-ready foundation that processes incoming user commands via webhooks instantly. In this technical guide, we will step through creating a custom bot with Telegram's BotFather, connecting incoming webhook triggers, executing custom logic, and responding with formatted HTML messages.
⚡ Why Use Webhook Triggers for Telegram Bots?
Unlike polling mechanisms which constantly query Telegram servers for updates (wasting CPU resources), Webhooks deliver incoming messages in real time as soon as a user hits send. Combined with self-hosted n8n, this architecture ensures sub-second response times without incurring API usage fees.
Prerequisites for Building an n8n Telegram Bot
Before assembling your nodes on the n8n canvas, prepare the following requirements:
- Telegram Account: Access to the Telegram app to register a new bot token via
@BotFather. - Active n8n Instance: A running n8n deployment accessible over public HTTPS (required for Telegram webhook verification).
- Telegram Bot Token: The HTTP API access token provided by BotFather (format:
123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ).
Step-by-Step Blueprint: Building the Telegram Webhook Bot
Step 1: Generate Bot Credentials with BotFather
Open Telegram and search for the official @BotFather account to generate your bot token:
- Send the command
/newbot. - Provide a display name for your bot (e.g., PromptLogic Helper Bot).
- Provide a unique username ending in
bot(e.g., promptlogic_helper_bot). - Copy the HTTP API access token provided by BotFather and save it securely.
Step 2: Configure the Telegram Trigger Node in n8n
Add the Telegram Trigger Node to your n8n workflow canvas:
- Create a new credential under Telegram API and paste your Bot Access Token.
- Set Event:
message(triggers on any incoming text, media, or command). - Click Listen for Event to test incoming payloads. Send a text message like
/startto your bot in Telegram to verify data capture.
Step 3: Route Commands and Format Responses
Use a Switch Node or Code Node to evaluate incoming text and generate dynamic outputs based on user intent:
// Example Code Node: Parse Command & Prepare Response
const userMessage = $input.item.json.message?.text || "";
const chatId = $input.item.json.message?.chat?.id;
const firstName = $input.item.json.message?.from?.first_name || "User";
let replyText = "";
if (userMessage.startsWith("/start")) {
replyText = `Hello ${firstName}! 👋\n\nWelcome to PromptLogic Bot. How can I assist you today?\n\nType /help for available commands.`;
} else if (userMessage.startsWith("/help")) {
replyText = `Available Commands:\n- /start - Restart the bot\n- /status - Check system status\n- /help - Show this menu`;
} else {
replyText = `Received your message: "${userMessage}". Processed successfully!`;
}
return {
json: {
chatId,
replyText
}
};
Step 4: Dispatch Messages via the Telegram Node
Add a Telegram Node to send the processed message back to the user:
- Resource: Message
- Operation: Send Message
- Chat ID: Map to
={{ $json.chatId }} - Text: Map to
={{ $json.replyText }} - Parse Mode: Select
HTML(allows using<b>,<i>, and<code>formatting tags).
Workflow Architecture Summary
| Node Name | Type | Function |
|---|---|---|
| Telegram Trigger | Trigger Node | Listens for real-time incoming messages via Telegram Webhook. |
| Command Logic | Code Node (JS) | Parses message text and structures formatted HTML response. |
| Telegram Action | Action Node | Sends reply back to the originating Telegram Chat ID. |
Free Importable n8n Telegram Bot Template JSON
To import this workflow directly into your n8n instance, copy the JSON code block below and paste it on your n8n canvas using Ctrl + V (or Cmd + V):
Troubleshooting & Common Telegram Webhook Issues
- Webhook Not Registering (400 Bad Request): Telegram webhooks require a valid, trusted SSL certificate (HTTPS). If running n8n locally without a public domain, use Cloudflare Tunnels or a reverse proxy like Caddy.
- Bot Responds Repeatedly (Infinite Loop): Ensure your workflow filter ignores messages sent by the bot itself or only processes specific incoming commands.
- HTML Formatting Errors (Can't Parse Entities): Unclosed tags like
<b>without</b>will cause Telegram's API to reject the message. Use standard string escaping or plain text fallback.
Related Guides & Resources
Explore more self-hosted workflow automation guides on PromptLogic:
Post a Comment for "Building a Telegram Bot with n8n and Webhooks (Free JSON Template)"