Skip to content

Telegram Basic Bot#

Learn how to create a simple but powerful Telegram bot using n8n for automated messaging and file handling.

🎯 What You'll Build#

A Telegram bot that can: - Receive and respond to text messages - Handle file uploads and downloads - Process commands and provide information - Send notifications and updates

πŸ“‹ Requirements#

  • Telegram account
  • Bot token from BotFather
  • n8n instance running
  • Basic understanding of webhooks

πŸ”§ Workflow Overview#

Key Components#

  1. Telegram Trigger - Receives messages and commands
  2. Message Router - Routes different message types
  3. Response Handler - Generates appropriate replies
  4. File Processor - Handles file uploads and downloads

πŸ“ Step-by-Step Guide#

1. Create Your Telegram Bot#

  1. Start a chat with BotFather in Telegram
  2. Create a new bot with /newbot
  3. Choose a name and username for your bot
  4. Save your bot token - it's required for n8n configuration

2. Set Up the Telegram Trigger#

  1. Add a Telegram Bot Trigger node
  2. Configure the connection: - Bot Token: Enter your token from BotFather - Updates: Select the types of updates to receive
  3. Test the connection by sending a message to your bot

3. Configure Message Routing#

  1. Add a Switch node to route different message types
  2. Set up branches for: - Text messages - Commands (starting with /) - File uploads - Images/documents - Other media types

4. Create Response Handlers#

Basic Text Response#

  1. Add Set nodes for each message type
  2. Configure response logic:
    1
    2
    3
    4
    // Example: Echo message
    return {
      text: `You said: ${$json.message.text}`
    };
    

Command Processing#

  1. Handle common commands: - /start - Welcome message - /help - Available commands - /status - Bot status - /about - Bot information

File Handling#

  1. Process uploaded files: - Download files to temporary storage - Analyze file content - Send confirmation or processing results

πŸ€– Bot Commands Setup#

Essential Commands#

/start Command

1
2
3
{
  text: "πŸ‘‹ Welcome to my bot!\n\nAvailable commands:\n/start - Start the bot\n/help - Show help\n/status - Check bot status\n/about - About this bot"
}

/help Command

1
2
3
{
  text: "πŸ“š Help Menu\n\nβ€’ Send me any text and I'll echo it back\nβ€’ Upload files and I'll process them\nβ€’ Use commands for specific actions"
}

/status Command

1
2
3
{
  text: `🟒 Bot Status: Online\nπŸ•’ Uptime: ${new Date().toLocaleString()}\nπŸ“Š Messages processed: Counter here`
}

πŸ“ File Processing#

Handle File Uploads#

  1. File Download - Use HTTP Request node to download files - Handle different file types (images, documents, etc.) - Store temporarily for processing

  2. File Analysis - Extract metadata (size, type, dimensions) - Perform content analysis if needed - Generate summary or insights

  3. Response Generation - Acknowledge receipt - Provide processing status - Share results or next steps

Example File Handler#

1
2
3
4
5
6
// Process uploaded image
if ($json.message.photo) {
  return {
    text: `πŸ“· I received your photo!\n\nSize: ${fileSize}\nType: ${fileType}\n\nProcessing complete!`
  };
}

πŸ”Œ Advanced Features#

Interactive Keyboards#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  text: "What would you like to do?",
  reply_markup: {
    inline_keyboard: [
      [
        { text: "πŸ“Š Get Info", callback_data: "get_info" },
        { text: "πŸ”” Subscribe", callback_data: "subscribe" }
      ],
      [
        { text: "❓ Help", callback_data: "help" }
      ]
    ]
  }
}

Rich Media Responses#

  • Send images with captions
  • Share documents and files
  • Create photo galleries
  • Send location data

Scheduled Messages#

  1. Add Cron Trigger node
  2. Configure schedule (daily, weekly, etc.)
  3. Create broadcast messages
  4. Send to subscribed users

πŸ§ͺ Testing Your Bot#

Local Testing#

  1. Use a tunneling service (ngrok, localtunnel)
  2. Update webhook URL in Telegram
  3. Test all message types
  4. Verify file handling

Test Scenarios#

  • Text messages of various lengths
  • File uploads (images, documents)
  • Command processing
  • Error handling
  • Concurrent users

🎨 Customization Ideas#

Personal Assistant Bot#

  • Set reminders and alarms
  • Note-taking capabilities
  • Weather information
  • News updates

Customer Support Bot#

  • FAQ automation
  • Ticket creation
  • Live chat handoff
  • Knowledge base integration

Content Distribution Bot#

  • Daily news summaries
  • Scheduled announcements
  • RSS feed processing
  • Social media posting

πŸ” Troubleshooting#

Common Issues#

Webhook Not Working - Check your webhook URL is accessible - Verify SSL certificate (Telegram requires HTTPS) - Ensure port is open and accessible

Bot Token Issues - Validate token format - Check token hasn't expired - Verify bot permissions

Message Not Received - Check bot is running - Verify webhook configuration - Check n8n execution logs

Debug Tools#

  1. n8n Execution History - Review workflow executions
  2. Telegram Bot API - Use getUpdates method for debugging
  3. Webhook Testing Tools - Verify endpoint accessibility
  4. Browser DevTools - Check network requests

πŸ“Š Analytics & Monitoring#

Track Bot Usage#

  • Message count per user
  • Popular commands
  • File upload statistics
  • Response times

Monitor Performance#

  • API response times
  • Error rates
  • User engagement metrics
  • Resource usage

πŸ›‘οΈ Security Best Practices#

  • Protect your bot token - Store securely in n8n credentials
  • Validate user input - Sanitize all incoming data
  • Rate limiting - Prevent spam and abuse
  • Privacy compliance - Handle user data responsibly

πŸŽ“ Next Steps#

Once your basic bot is working:

  1. Add database storage - Save user preferences and history
  2. Implement AI responses - Connect to chatbot APIs
  3. Create admin panel - Manage bot settings and users
  4. Deploy to production - Scale for multiple users

πŸš€ Deployment#

Production Setup#

  1. Use a reliable hosting - Cloud service or VPS
  2. Set up SSL certificates - Required for Telegram webhooks
  3. Configure monitoring - Track uptime and performance
  4. Implement backup - Regular data backups

Scaling Considerations#

  • Load balancing for high traffic
  • Database optimization
  • Caching strategies
  • Error handling and recovery

Related Tutorials: - Telegram File Automation - Advanced file processing - LINE Messaging API - Multi-platform messaging

Resources: - Telegram Bot API Documentation - BotFather Guide - n8n Telegram Integration