DEV Community

brian austin
brian austin

Posted on

Building a Telegram bot with Claude API for under $2/month (complete tutorial)

Building a Telegram bot with Claude API for under $2/month

I've been running a Telegram bot powered by Claude for several months. Last month it cost me less than $2. Here's the complete setup.

Why Telegram + Claude?

Telegram bots are everywhere — customer support, personal assistants, group moderation, notification systems. The problem: most tutorials show you how to hook up OpenAI at $20/month or build your own Anthropic API key at $15+ per million tokens.

There's a cheaper path.

The Stack

  • Node.js + node-telegram-bot-api
  • SimplyLouie API ($2/month, Claude-powered)
  • A free VPS or Fly.io hobby tier

Setup in 10 minutes

1. Create your Telegram bot

# Talk to @BotFather on Telegram
# Send: /newbot
# Follow the prompts, get your token
export TELEGRAM_TOKEN="your-bot-token-here"
Enter fullscreen mode Exit fullscreen mode

2. Install dependencies

mkdir my-claude-bot && cd my-claude-bot
npm init -y
npm install node-telegram-bot-api axios dotenv
Enter fullscreen mode Exit fullscreen mode

3. The complete bot (< 80 lines)

require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const axios = require('axios');

const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true });

// Conversation history per user
const conversations = {};

async function askClaude(userId, message) {
  if (!conversations[userId]) {
    conversations[userId] = [];
  }

  conversations[userId].push({
    role: 'user',
    content: message
  });

  // Keep last 10 messages to control token usage
  if (conversations[userId].length > 10) {
    conversations[userId] = conversations[userId].slice(-10);
  }

  const response = await axios.post(
    'https://simplylouie.com/api/chat',
    {
      messages: conversations[userId],
      model: 'claude-3-5-haiku-20241022'
    },
    {
      headers: {
        'Authorization': `Bearer ${process.env.LOUIE_API_KEY}`,
        'Content-Type': 'application/json'
      }
    }
  );

  const reply = response.data.content[0].text;

  conversations[userId].push({
    role: 'assistant',
    content: reply
  });

  return reply;
}

bot.on('message', async (msg) => {
  const chatId = msg.chat.id;
  const userId = msg.from.id.toString();
  const text = msg.text;

  if (!text) return;

  // Show typing indicator
  bot.sendChatAction(chatId, 'typing');

  try {
    const reply = await askClaude(userId, text);
    bot.sendMessage(chatId, reply, { parse_mode: 'Markdown' });
  } catch (err) {
    console.error('Claude error:', err.message);
    bot.sendMessage(chatId, 'Sorry, something went wrong. Try again.');
  }
});

bot.on('polling_error', (err) => {
  console.error('Polling error:', err.message);
});

console.log('Bot running...');
Enter fullscreen mode Exit fullscreen mode

4. Environment file

# .env
TELEGRAM_TOKEN=your-telegram-token
LOUIE_API_KEY=your-api-key-here
Enter fullscreen mode Exit fullscreen mode

Get your API key at simplylouie.com/developers — $2/month includes Claude access.

5. Deploy to Fly.io (free tier)

# Install flyctl
curl -L https://fly.io/install.sh | sh

# Deploy
fly launch
fly secrets set TELEGRAM_TOKEN=xxx LOUIE_API_KEY=xxx
fly deploy
Enter fullscreen mode Exit fullscreen mode

Your bot is now running 24/7 for free (Fly.io hobby) + $2/month for the Claude API.

Real-world use cases I've built

Personal assistant bot — answers questions, drafts emails, summarizes articles I paste

Customer FAQ bot — trained on a system prompt with product info, handles 80% of support questions automatically

Code review bot — add it to a dev group, paste code snippets, get instant review

Language learning bot — practice conversations in Spanish/French with corrections

Controlling costs

The slice(-10) in the code above is important — it limits conversation history to 10 messages, keeping token usage predictable. For a personal bot with moderate usage, you'll comfortably stay within the $2/month plan.

If you're building for multiple users (like a customer support bot), the usage scales up. Check the developers page for API tier pricing.

Extending the bot

Add a system prompt for personality:

async function askClaude(userId, message) {
  const systemPrompt = 'You are a helpful assistant for a software development team. Be concise, technical, and use code examples when helpful.';

  // ... rest of the function
  const response = await axios.post(
    'https://simplylouie.com/api/chat',
    {
      system: systemPrompt,
      messages: conversations[userId],
      model: 'claude-3-5-haiku-20241022'
    },
    // ...
  );
}
Enter fullscreen mode Exit fullscreen mode

Add /reset command to clear conversation history:

bot.onText(/\/reset/, (msg) => {
  const userId = msg.from.id.toString();
  conversations[userId] = [];
  bot.sendMessage(msg.chat.id, 'Conversation cleared ✓');
});
Enter fullscreen mode Exit fullscreen mode

The full code on GitHub

I've open-sourced the complete bot template: the code above is production-ready as-is.

What this costs

  • Telegram Bot API: free
  • Fly.io hobby tier: free
  • SimplyLouie Claude API: $2/month

Total: $2/month for a production-grade AI Telegram bot.


SimplyLouie is a $2/month Claude API wrapper with no rate limits and no complex billing. Get your API key at simplylouie.com/developers.

Top comments (0)