# Build a free Telegram AI bot on Netlify with Gemini + Groq

> Source: <https://dev.to/nimaai1528/build-a-free-telegram-ai-bot-on-netlify-with-gemini-groq-l23>
> Published: 2026-12-05 18:33:55+00:00

**Recommendation:** Add a second LLM provider as a fallback behind your primary so free-tier rate limits and outages stay invisible to users, and test the fallback path offline with mocked services.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

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](https://github.com/nimaai1528/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](https://dev.to/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)**

| 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) | 

**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**

`/_setup` URL again, and make sure you redeployed after adding the variables.`Forbidden` on `key` doesn't match `TELEGRAM_WEBHOOK_SECRET`.
**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.*
