Contents
n8n Telegram Bot Tutorial: Build an AI Chatbot
In this section, we'll build a Telegram AI assistant: users message the bot, n8n uses AI to generate replies, and it remembers each user's conversation.
[Telegram Trigger] ─▶ [AI Agent] ─▶ [Telegram: Send Message]
├ Chat Model
└ Memory (scoped by user ID)
1. Create a Telegram bot
- Search for @BotFather on Telegram.
- Send
/newbot. - Follow the prompts to enter a display name for your bot and a username ending in
bot(for example,my_helper_bot). - BotFather will return a Bot Token. Store it securely and don't share it.
2. Create credentials
In n8n, add a Telegram API credential and paste in the Bot Token.
3. Telegram Trigger
Create a workflow and select Telegram Trigger as the first node:
- Trigger On: Message
Telegram Trigger uses a webhook to receive messages, so:
- n8n must be reachable from the internet over HTTPS (see Self-hosting).
- For local testing, you can temporarily expose it to the internet with a tool such as Cloudflare Tunnel.
Click Listen for test event, then send a message to your bot on Telegram. The node will receive data like this:
{
"message": {
"message_id": 42,
"from": { "id": 123456789, "first_name": "Alex" },
"chat": { "id": 123456789, "type": "private" },
"text": "Hello"
}
}
A bot can have only one webhook configured. When you switch the same bot between a test workflow and a production workflow, the last one activated takes effect.
4. AI Agent
Add an AI Agent node:
- Source for Prompt: Define below
- Prompt (User Message):
{{ $json.message.text }} - System Message:
You are a friendly Telegram assistant named "Helper".
Reply concisely in Traditional Chinese commonly used in Taiwan, and make your answers easy to read on a phone.
Connect a Chat Model (OpenAI, or a local Ollama model).
5. Remember conversations by user
Add Simple Memory (for production, Postgres / Redis Chat Memory is recommended):
- Session ID: Define below
- Key:
{{ $('Telegram Trigger').item.json.message.chat.id }}
Using the chat ID as the Session Key gives each user (or group) a separate conversation memory, so conversations don't get mixed together.
6. Send a reply
Add a Telegram node and choose the Send Message operation:
- Chat ID:
{{ $('Telegram Trigger').item.json.message.chat.id }} - Text:
{{ $json.output }}
The AI Agent's response is in the output field.
You can set Parse Mode under Additional Fields. If Markdown symbols in the model's response cause sending to fail, leave Parse Mode unset and send the reply as plain text.
7. Test and publish
- Click Execute workflow and message your bot to test it.
- Once everything works, Save and Publish.
Advanced features
Allow only specific users
Add an If node after Telegram Trigger:
{{ $json.message.from.id }} equals 123456789
For users who don't match, use another branch to reply, "This bot is for internal use only," or ignore the message. You can also restrict user IDs in Telegram Trigger's settings (depending on the version). You can find your own ID in the data received by the Trigger.
Handle commands
Use a Switch node to route messages based on their content:
| Condition | Action |
|---|---|
text equals /start | Send a welcome message and instructions |
text equals /reset | Clear memory |
| Other | Pass to AI Agent |
Handle non-text messages
When a user sends an image, sticker, or voice message, message.text will be empty. Before AI Agent, use If to check whether {{ $json.message.text }} exists to prevent errors.
To process images, use Get File in the Telegram node to download the image, then pass it to a vision-enabled model for analysis.
Add tools
Connect tools introduced in AI Agent basics (Google Sheets, HTTP Request, Calculator) to this Agent so the bot can look up information and take actions.
Send messages proactively
You can use a Telegram node in any workflow to send messages proactively, not just reply to messages. For example, send daily reports, error alerts, or order notifications. You only need to know your Chat ID.
Updated October 12, 2026