DEV Community

Alex Kane
Alex Kane

Posted on

Build a Telegram AI chatbot with n8n in 15 minutes — full workflow JSON

Built a Telegram bot last month that answers questions using GPT-4o-mini. Setup took 20 minutes in n8n — sharing the full workflow JSON here.

What it does

  • Listens for any message sent to a Telegram bot
  • Routes /start and /help commands to friendly welcome messages
  • Sends all other messages through GPT-4o-mini for an AI answer
  • Returns the AI response directly in Telegram

Total nodes: 7. No code required.

The workflow JSON

{
  "name": "Telegram AI Assistant",
  "nodes": [
    {
      "parameters": { "updates": ["message"] },
      "id": "t1",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "jsCode": "const text = $json.message?.text || '';\nconst chatId = $json.message?.chat?.id;\nconst command = text.startsWith('/') ? text.split(' ')[0] : 'question';\nreturn [{ json: { chatId, text, command } }];"
      },
      "id": "t2",
      "name": "Parse Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [460, 300]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            { "conditions": { "string": [{ "value1": "={{ $json.command }}", "operation": "equals", "value2": "/start" }] } },
            { "conditions": { "string": [{ "value1": "={{ $json.command }}", "operation": "equals", "value2": "/help" }] } }
          ]
        },
        "fallbackOutput": "extra"
      },
      "id": "t3",
      "name": "Route Command",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chatId }}",
        "text": "Hi! I am an AI assistant. Ask me anything.\n\n/start - This message\n/help - Help"
      },
      "id": "t4",
      "name": "Send Welcome",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [900, 180]
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "messages": {
          "values": [
            { "role": "system", "content": "You are a helpful assistant. Answer concisely." },
            { "role": "user", "content": "={{ $json.text }}" }
          ]
        }
      },
      "id": "t5",
      "name": "AI Answer",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1,
      "position": [900, 420]
    },
    {
      "parameters": {
        "chatId": "={{ $node['Parse Message'].json.chatId }}",
        "text": "={{ $json.message.content }}"
      },
      "id": "t6",
      "name": "Send AI Reply",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [1120, 420]
    }
  ],
  "connections": {
    "Telegram Trigger": { "main": [[{ "node": "Parse Message", "type": "main", "index": 0 }]] },
    "Parse Message": { "main": [[{ "node": "Route Command", "type": "main", "index": 0 }]] },
    "Route Command": {
      "main": [
        [{ "node": "Send Welcome", "type": "main", "index": 0 }],
        [{ "node": "AI Answer", "type": "main", "index": 0 }]
      ]
    },
    "AI Answer": { "main": [[{ "node": "Send AI Reply", "type": "main", "index": 0 }]] }
  }
}
Enter fullscreen mode Exit fullscreen mode

Setup in 5 steps

1. Create your Telegram bot
Open @botfather → send /newbot → follow prompts → copy the token.

2. Add credentials in n8n
Settings → Credentials → New → Telegram → paste your bot token. Same for OpenAI.

3. Import the workflow
Workflows → New → three-dot menu → Import from JSON → paste the JSON above.

4. Activate
Toggle the workflow to Active. The Telegram Trigger automatically registers a webhook.

5. Test
Message your bot. /start returns welcome. Any other text → GPT-4o-mini answers.

What to customize

  • System prompt: Change "You are a helpful assistant" to give the bot a persona or restrict topics
  • Model: Swap gpt-4o-mini for gpt-4o for stronger responses (~10x cost)
  • Add memory: Connect a database node to pass conversation history to the AI node

Cost to run

With GPT-4o-mini at ~150 tokens/exchange:

  • ~$0.001 per message
  • 1,000 messages/month ≈ $1 in API costs
  • n8n self-hosted is free

I packaged this with a full setup guide (screenshots included), extended examples, and a version with conversation memory as part of a template set at https://stripeai.gumroad.com — but the JSON above is the complete working core, free to use.

Happy to answer questions.

Top comments (0)