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.
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.
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.
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.
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.");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
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
5:42
24:16
33:42
41:18
28:05
3:12