IntegrationsIntermediate

How to make a Telegram bot reply with AI answers

Wire a Telegram bot to an LLM so every incoming message gets an intelligent generated reply.

11 minIntermediate

An AI reply bot listens for incoming messages, sends the text to a language model, and returns the model's answer to the user. This guide builds a small Node service that does exactly that. We use the Anthropic Claude API for the brains, but the same shape works with any provider.

What you need

  • A bot token from BotFather
  • Node 18 or newer
  • An Anthropic API key from console.anthropic.com
  • Basic comfort with the terminal

Step 1: Set up the project

Create a folder, initialise it, and install the official Telegram bot library plus the Anthropic SDK.

zsh - setup
$mkdir ai-tg-bot && cd ai-tg-bot
$npm init -y
$npm install node-telegram-bot-api @anthropic-ai/sdk
added 32 packages in 3s
$

Step 2: Store your keys

Keep secrets out of the code. Put both keys in environment variables so they never end up in version control.

.env
TELEGRAM_BOT_TOKEN="7843201955:AAH9k...redacted"
ANTHROPIC_API_KEY="sk-ant-...redacted"

Step 3: Write the bot

The bot polls Telegram for new messages, forwards each one to Claude, and sends the reply back to the same chat. The model id below is current as of mid 2026; check the provider docs if you want the newest one.

bot.mjs
import TelegramBot from "node-telegram-bot-api";
import Anthropic from "@anthropic-ai/sdk";

const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, {
  polling: true,
});
const ai = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

bot.on("message", async (msg) => {
  if (!msg.text) return;
  await bot.sendChatAction(msg.chat.id, "typing");

  const reply = await ai.messages.create({
    model: "claude-opus-4-5",
    max_tokens: 600,
    system: "You are a concise, friendly assistant in a Telegram chat.",
    messages: [{ role: "user", content: msg.text }],
  });

  const text = reply.content
    .filter((b) => b.type === "text")
    .map((b) => b.text)
    .join("");

  await bot.sendMessage(msg.chat.id, text);
});

console.log("AI bot running.");
Show a typing indicator
The sendChatAction(chatId, "typing") call makes the bot look responsive while the model thinks. Without it, users may resend their message during the wait.

Step 4: Run and chat

zsh - run
$node --env-file=.env bot.mjs
AI bot running.
$
Telegram - chat with the bot
You
Explain a webhook in one sentence.
Agent
A webhook is a URL you give a service so it can push you a notification the moment something happens, instead of you polling for it.
Watch the cost
Each message is a paid API call. For a public bot, add per-user rate limiting and a max message length so a few heavy users cannot run up your bill.

Result

Your bot now answers any message with a model-generated reply. Swap the system prompt to give it a personality, or feed in extra context to make it a domain expert.

Watch related tutorials

Tags
#telegram#ai#llm#bot#chatbot