DEV Community

Cover image for Build a No-Code AI Telegram Bot with n8n (On-Premise & Free)
Akshay Kumar BM
Akshay Kumar BM

Posted on

Build a No-Code AI Telegram Bot with n8n (On-Premise & Free)

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
Enter fullscreen mode Exit fullscreen mode

This command:

  1. Downloads the n8n package
  2. Installs the necessary dependencies
  3. Launches the n8n editor at http://localhost:5678

Setup Screenshots

npx n8n install output

npx n8n running


2. Create Your Telegram Bot

  1. Open the BotFather in Telegram.
  2. Send the command /newbot and follow the instructions:
  • Choose a display Name
  • Set a Username (must end with bot, e.g., MyAIHelperBot)
    1. Copy the Bot Token provided after creation.

BotFather create new bot


3. Configure the Telegram Trigger in n8n

  1. In the n8n editor, drag in a Telegram Trigger node.
  2. Authenticate using your Bot Token.
  3. Optionally specify filters such as chat IDs.

Telegram Trigger


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).

AI agent config

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.

Telegram reply setup


5. Test the Bot Locally Using ngrok

  1. Save and Activate the workflow in n8n.
  2. ngrok automatically starts when you run it for the first time using the n8n start command. Copy the HTTPS URL generated by ngrok.
  3. Set this HTTPS URL as the Webhook URL in n8n’s settings.
  4. Send a message to your bot in Telegram.
  5. View the live execution logs in n8n to confirm the workflow is triggered and a response is sent.

Agent in action


Final Architecture Overview

Here's a visual overview of how all components—Telegram, n8n, and the AI model—interact in this workflow:

n8n Architecture Overview


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)