> This page location: Functions > Templates > Telegram AI chat bot
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Telegram AI chat bot

A Telegram bot that runs on Neon Functions, answers messages through AI Gateway, and stores the conversation in Postgres.

- First Party: maintained by Neon
- Built by: Neon
- Categories: First Party, Bots

## Install

```bash
neon functions new telegram-bot
```

## About this template

This template is a minimal [Telegram](https://telegram.org/) webhook bot on Neon Functions. It verifies Telegram's secret token, sends each text message to a model through [Neon AI Gateway](https://neon.com/docs/ai-gateway/overview), and replies with the answer. It stores the conversation in Postgres and sends the model the last 10 messages of the chat as context.

### Create the bot

1. Open [@BotFather](https://t.me/BotFather), send `/newbot`, and copy the token.
2. Generate a webhook secret, which may contain only letters, digits, `_`, and `-`:

```bash
openssl rand -hex 32
```

### Scaffold and configure

```bash
npm i -g neon@latest
neon login
neon link

neon functions new telegram-bot
```

Add both values to `.env.local` without replacing the Neon values already there:

```bash
echo 'TELEGRAM_BOT_TOKEN=<bot token>' >> .env.local
echo 'TELEGRAM_WEBHOOK_SECRET=<secret from above>' >> .env.local
```

| Variable | Description | Required |
| --- | --- | --- |
| `TELEGRAM_BOT_TOKEN` | The bot token from @BotFather, which the function uses to reply to messages. | Yes |
| `TELEGRAM_WEBHOOK_SECRET` | A random secret of letters, digits, \_ and - that the function uses to verify that incoming requests come from Telegram. | Yes |

Enable AI Gateway on the branch:

```bash
neon config add ai-gateway
```

You can also set `aiGateway: true` in `neon.ts`. AI Gateway requires a paid Neon plan with prepaid credits and a project in a supported region. See the [AI Gateway quickstart](https://neon.com/docs/ai-gateway/get-started).

`neon link` writes `DATABASE_URL` to `.env.local`. When you open the function URL in a browser or send it a `GET` request, the function creates the `telegram_messages` table and its index, then registers the webhook with Telegram so the bot can receive messages. If Telegram rejects the request, for example because the bot token is wrong, the function returns `502` with Telegram's error message. `neon deploy` prints the function URL.

### Deploy

```bash
neon deploy --env .env.local
neon functions get telegrambot
```

### Try it

Open the bot in Telegram, send `/start`, then send any text. Follow up with another message to see the bot use the earlier turns.

## Related

- Previous: [MCP server with OAuth sign-in](https://neon.com/functions/templates/mcp-oauth.md)
- Next: [REST API with Hono and Postgres](https://neon.com/functions/templates/rest-api-postgres.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/telegram-bot)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/telegram-bot"}` to https://feedback.neon.tech/ (no auth required).