Back to Templates

Create a Telegram Bot with Mistral Nemotron AI and Conversation Memory

Created by

Created by: Ajith joseph  || codzz

Ajith joseph

Last update

Last update 9 hours ago

Share


πŸ€– Create a Telegram Bot with Mistral AI and Conversation Memory

A sophisticated Telegram bot that provides AI-powered responses with conversation memory. This template demonstrates how to integrate any AI API service with Telegram, making it easy to swap between different AI providers like OpenAI, Anthropic, Google AI, or any other API-based AI model.

πŸ”§ How it works

The workflow creates an intelligent Telegram bot that:

  • πŸ’¬ Maintains conversation history for each user
  • 🧠 Provides contextual AI responses using any AI API service
  • πŸ“± Handles different message types and commands
  • πŸ”„ Manages chat sessions with clear functionality
  • πŸ”Œ Easily adaptable to any AI provider (OpenAI, Anthropic, Google AI, etc.)

βš™οΈ Set up steps

πŸ“‹ Prerequisites

  • πŸ€– Telegram Bot Token (from @BotFather)
  • πŸ”‘ AI API Key (from any AI service provider)
  • πŸš€ n8n instance with webhook capability

πŸ› οΈ Configuration Steps

  1. πŸ€– Create Telegram Bot

    • Message @BotFather on Telegram
    • Create new bot with /newbot command
    • Save the bot token for credentials setup
  2. 🧠 Choose Your AI Provider

    • OpenAI: Get API key from OpenAI platform
    • Anthropic: Sign up for Claude API access
    • Google AI: Get Gemini API key
    • NVIDIA: Access LLaMA models
    • Hugging Face: Use inference API
    • Any other AI API service
  3. πŸ” Set up Credentials in n8n

    • Add Telegram API credentials with your bot token
    • Add Bearer Auth/API Key credentials for your chosen AI service
    • Test both connections
  4. πŸš€ Deploy Workflow

    • Import the workflow JSON
    • Customize the AI API call (see customization section)
    • Activate the workflow
    • Set webhook URL in Telegram bot settings

✨ Features

πŸš€ Core Functionality

  • πŸ“¨ Smart Message Routing: Automatically categorizes incoming messages (commands, text, non-text)
  • 🧠 Conversation Memory: Maintains chat history for each user (last 10 messages)
  • πŸ€– AI-Powered Responses: Integrates with any AI API service for intelligent replies
  • ⚑ Command Support: Built-in /start and /clear commands

πŸ“± Message Types Handled

  • πŸ’¬ Text Messages: Processed through AI model with context
  • πŸ”§ Commands: Special handling for bot commands
  • ❌ Non-text Messages: Polite error message for unsupported content

πŸ’Ύ Memory Management

  • πŸ‘€ User-specific chat history storage
  • πŸ”„ Automatic history trimming (keeps last 10 messages)
  • 🌐 Global state management across workflow executions

πŸ€– Bot Commands

  • /start 🎯 - Welcome message with bot introduction
  • /clear πŸ—‘οΈ - Clears conversation history for fresh start
  • Regular text πŸ’¬ - Processed by AI with conversation context

πŸ”§ Technical Details

πŸ—οΈ Workflow Structure

  1. πŸ“‘ Telegram Trigger - Receives all incoming messages
  2. πŸ”€ Message Filtering - Routes messages based on type/content
  3. πŸ’Ύ History Management - Maintains conversation context
  4. 🧠 AI Processing - Generates intelligent responses
  5. πŸ“€ Response Delivery - Sends formatted replies back to user

πŸ€– AI API Integration (Customizable)

Current Example (NVIDIA):

  • Model: mistralai/mistral-nemotron
  • Temperature: 0.6 (balanced creativity)
  • Max tokens: 4096
  • Response limit: Under 200 words

πŸ”„ Easy to Replace with Any AI Service:

OpenAI Example:

{
  "model": "gpt-4",
  "messages": [...],
  "temperature": 0.7,
  "max_tokens": 1000
}

Anthropic Claude Example:

{
  "model": "claude-3-sonnet-20240229",
  "messages": [...],
  "max_tokens": 1000
}

Google Gemini Example:

{
  "contents": [...],
  "generationConfig": {
    "temperature": 0.7,
    "maxOutputTokens": 1000
  }
}

πŸ›‘οΈ Error Handling

  • ❌ Non-text message detection and appropriate responses
  • πŸ”§ API failure handling
  • ⚠️ Invalid command processing

🎨 Customization Options

πŸ€– AI Provider Switching

To use a different AI service, modify the "NVIDIA LLaMA Chat Model" node:

  1. πŸ“ Change the URL in HTTP Request node
  2. πŸ”§ Update the request body format in "Prepare API Request" node
  3. πŸ” Update authentication method if needed
  4. πŸ“Š Adjust response parsing in "Save AI Response to History" node

🧠 AI Behavior

  • πŸ“ Modify system prompt in "Prepare API Request" node
  • 🌑️ Adjust temperature and response parameters
  • πŸ“ Change response length limits
  • 🎯 Customize model-specific parameters

πŸ’Ύ Memory Settings

  • πŸ“Š Adjust history length (currently 10 messages)
  • πŸ‘€ Modify user identification logic
  • πŸ—„οΈ Customize data persistence approach

🎭 Bot Personality

  • πŸŽ‰ Update welcome message content
  • ⚠️ Customize error messages and responses
  • βž• Add new command handlers

πŸ’‘ Use Cases

  • 🎧 Customer Support: Automated first-line support with context awareness
  • πŸ“š Educational Assistant: Homework help and learning support
  • πŸ‘₯ Personal AI Companion: General conversation and assistance
  • πŸ’Ό Business Assistant: FAQ handling and information retrieval
  • πŸ”¬ AI API Testing: Perfect template for testing different AI services
  • πŸš€ Prototype Development: Quick AI chatbot prototyping

πŸ“ Notes

  • 🌐 Requires active n8n instance for webhook handling
  • πŸ’° AI API usage may have rate limits and costs (varies by provider)
  • πŸ’Ύ Bot memory persists across workflow restarts
  • πŸ‘₯ Supports multiple concurrent users with separate histories
  • πŸ”„ Template is provider-agnostic - easily switch between AI services
  • πŸ› οΈ Perfect starting point for any AI-powered Telegram bot project

πŸ”§ Popular AI Services You Can Use

Provider Model Examples API Endpoint Style
🟒 OpenAI GPT-4, GPT-3.5 https://api.openai.com/v1/chat/completions
πŸ”΅ Anthropic Claude 3 Opus, Sonnet https://api.anthropic.com/v1/messages
πŸ”΄ Google Gemini Pro, Gemini Flash https://generativelanguage.googleapis.com/v1beta/models/
🟑 NVIDIA LLaMA, Mistral https://integrate.api.nvidia.com/v1/chat/completions
🟠 Hugging Face Various OSS models https://api-inference.huggingface.co/models/
🟣 Cohere Command, Generate https://api.cohere.ai/v1/generate

Simply replace the HTTP Request node configuration to switch providers!