Most "build an AI chatbot" tutorials end with a server you pay for every month. This one doesn't. We'll put an AI assistant in Telegram using three free tiers: Netlify Functions to host it, Google Gemini to answer, and Groq as a backup when Gemini is busy. There's no server, no database and no npm packages. The whole bot is one JavaScript file.

We'll use the open-source template telegram-ai-bot-lite (MIT).

How it works

Telegram can push each message to a URL of your choice (a webhook). Our URL is a Netlify Function:

Telegram ──POST update──▶ Netlify Function (index.mjs) │ checks secret header ├─▶ Gemini ──(error/empty)──▶ Groq ◀── answer Telegram ◀──sendMessage──────┘ 

The function does four things: it checks Telegram's secret-token header, sends the text to Gemini, tries Groq if Gemini fails or returns nothing, and replies with sendMessage. A request without the right secret is rejected. That matters, because otherwise anyone who finds the URL could run up your API usage.

Step 1: Create the bot (1 minute)

Open @botfather in Telegram, send /newbot, pick a name and copy the token.

Step 2: Get free AI keys (3 minutes)

One key is enough. Two make the bot much more reliable, because free tiers have rate limits and the fallback hides them from your users.

Step 3: Deploy to Netlify (3 minutes)

  1. On GitHub, click Use this template to copy the repo.
  2. In Netlify: Add new site → Import an existing project → GitHub, pick your copy, and deploy with the default settings.
  3. Under Site configuration → Environment variables, add:
Variable Value
TELEGRAM_BOT_TOKEN token from BotFather
TELEGRAM_WEBHOOK_SECRET a long random string (letters, digits, _, -)
GEMINI_API_KEY your Gemini key
GROQ_API_KEY your Groq key (optional)
  1. Deploys → Trigger deploy, so the variables take effect.

Step 4: Connect Telegram (1 minute)

Open this once in your browser:

https://YOUR-SITE.netlify.app/_setup?key=YOUR_TELEGRAM_WEBHOOK_SECRET 

You should see "Webhook was set". Now open your bot, press Start and say hi.

Step 5: Make it yours

Everything is set with environment variables, so you never edit code to customise it:

  • SYSTEM_PROMPT sets the personality and rules ("You are a concise cooking assistant…").
  • GEMINI_MODEL / GROQ_MODEL choose any model your key can use.
  • MAX_OUTPUT_TOKENS caps the answer length (default 1024).
  • PRIVATE_CHATS_ONLY (default true) makes the bot ignore groups.

Testing without keys

npm test runs offline. It mocks Telegram, Gemini and Groq, sends fake webhook updates, and checks three things: a text message gets an answer, Groq takes over when Gemini fails, and a wrong secret is rejected. Run it after every change.

Troubleshooting

  • No reply: open the /_setup URL again, and make sure you redeployed after adding the variables.
  • Forbidden on /_setup: the key doesn't match TELEGRAM_WEBHOOK_SECRET.
  • "AI service is busy": both providers failed. Check your keys and Netlify → Logs → Functions.

Limits to know

The Lite bot is stateless: every message is answered on its own, with no memory of the conversation. Free-tier quotas belong to Netlify, Google and Groq, and they can change. Check them before you give the bot to many users.

Where to go next

From here you can add conversation memory (a Redis store such as Upstash works well on serverless), photo input (Gemini accepts images), or voice notes (transcribe them, then answer). The repo README lists ideas for extending it. The template stays free and MIT-licensed.

This article was written with help from AI and edited by me.