Create a fully functional AI-powered Telegram bot without writing a single line of code! In this tutorial, you'll learn how to set up n8n locally and connect it to Telegram and an AI model—completely free and on-premise.
Prerequisites
- Node.js (v14 or higher) installed on your system
- Telegram account to create a bot using BotFather
- OpenAI API key or Google AI credentials for LLM integration
-
n8n (installed using
npx
) for automation workflows - ngrok (optional) to expose local webhooks over HTTPS
1. Install n8n via npx
To install and run n8n locally, simply use the following command:
npx n8n
This command:
- Downloads the
n8n
package - Installs the necessary dependencies
- Launches the n8n editor at http://localhost:5678
Setup Screenshots
2. Create Your Telegram Bot
- Open the BotFather in Telegram.
- Send the command
/newbot
and follow the instructions:
- Choose a display Name
- Set a Username (must end with
bot
, e.g.,MyAIHelperBot
)- Copy the Bot Token provided after creation.
3. Configure the Telegram Trigger in n8n
- In the n8n editor, drag in a Telegram Trigger node.
- Authenticate using your Bot Token.
- Optionally specify filters such as chat IDs.
4. Build the AI Agent Workflow
a. Connect to an AI Model
Use one of the following nodes to process user messages with AI:
- OpenAI Node: For GPT-3.5/GPT-4
- Google Vertex AI Node: For models hosted by Google
Configure the node with your API key and set the model to use (e.g., gpt-3.5-turbo
).
b. (Optional) Store Chat History
To support context or logs, add a Database or Google Sheet node to save messages and responses.
c. Send AI Responses Back to Telegram
Use the Telegram Send Message node and link it to the output of the AI node.
5. Test the Bot Locally Using ngrok
- Save and Activate the workflow in n8n.
- ngrok automatically starts when you run it for the first time using the n8n start command. Copy the HTTPS URL generated by ngrok.
- Set this HTTPS URL as the Webhook URL in n8n’s settings.
- Send a message to your bot in Telegram.
- View the live execution logs in n8n to confirm the workflow is triggered and a response is sent.
Final Architecture Overview
Here's a visual overview of how all components—Telegram, n8n, and the AI model—interact in this workflow:
Conclusion
With just a few tools and no coding, you've built an intelligent, conversational Telegram bot powered by AI and managed entirely within your own infrastructure. Whether you're prototyping or building a production bot, n8n makes it easy and flexible.
Happy automating! 🚀
Top comments (0)