Telegram assistant bot for a dental clinic, with a web admin
A demo of an LLM bot you can trust to talk to a clinic's clients: it answers only from the knowledge base, never shows a made-up price or the system prompt, never loses a request, and the admin sees every message and knows what each answer costs. The clinic and all data are fictional.
My role: Author: idea, architecture and code.
The task
Clinics want a bot that answers about services, prices, address and appointments. The main LLM risk here is a confidently stated untruth: a made-up price or an answer that is not about the clinic. The second risk is clients trying to take control of the bot.
Hence the requirements: answer only from the knowledge base, check the answer before sending it, keep requests through any failure, log every message for review and count the model costs. The price list, questions, requests and settings are managed in a web admin, without a programmer.
Architecture
The bot and the admin are two processes over one SQLite database in WAL mode: the bot sees admin changes without a restart. A message goes through a middleware chain and checks before and after the model call; handlers stay thin, and every database query goes through a repository.
- ChannelsTelegramclinic clients write to the botWeb adminFastAPI, SQLAdmin, HTMX
- Bot middlewareClientMiddlewareclient tracking, blocking, settingsThrottlingMiddlewareat most N messages per M seconds
- LogicClinicAssistantknowledge base, history, prompt, Claude callChecksattack attempts on input; prices and prompt leaks on outputRequestsa phone in a message becomes a request and an alertLog and costsevery message, tokens, latency, cost
- Data and modelSQLite (WAL)database shared by the bot and the adminClaude APIthe model is chosen in the adminAlembicmigrations; a test catches drift from the models
Key decisions
Prices only from the price list
The model's answer is scanned for prices like “800 lei”. If even one of them is not in the knowledge base, the client never sees that answer.
- Why
- A made-up price is worse than no answer: the client will come to the clinic with it.
- Trade-off
- The check works by pattern: a price written out in words slips through.
Prompt-injection protection in two layers
On input, heuristics in Russian, Romanian and English flag attempts to take control: a signal for the log and the admin. On output, the answer is checked for fragments of the system prompt, and such an answer is not sent. A suspicious exchange never enters the dialog history.
- Why
- The heuristics are honestly called a signal, not protection; the real protection is the prompt rules and the answer check.
- Trade-off
- Input heuristics can be bypassed by rephrasing, so every new rule comes with tests for hits and for false positives.
An audit log of every message
Everything incoming is logged, including rejected messages: outcome, raw and sent answer, tokens, latency, attack signs. The admin hears about suspicious messages right away, but no more than once per 15 minutes per client.
- Why
- Any bot answer can be reviewed after the fact.
- Trade-off
- The log grows, so its retention period is a setting.
Money in whole microdollars
The cost of each answer is computed from a model price list that is editable in the admin, and stored in whole microdollars. Daily totals roll up into a monthly report with a forecast for the current month.
- Why
- Sums add up without rounding errors. Handy too: “$X per million tokens” is exactly X microdollars per token.
- Trade-off
- A new model needs a price-list row, otherwise its requests land in “no price”.
A request is never lost
If a message contains a phone number, a request with the status “New” is created and forwarded to the admin — even when the model is down or the message is over the limit.
- Why
- Requests are the bot's main value for the clinic.
- Trade-off
- Phones are found by pattern, so an extra request can occasionally appear.
Settings in the database, one database for both
The Claude model, limits, history depth, greeting and log retention live in a settings table and are changed in the admin. The bot and the admin share one SQLite database in WAL mode.
- Why
- The admin changes the bot's behaviour without a programmer and without a restart; deployment for one clinic is one container and one database file.
- Trade-off
- The design targets one clinic and one server: rate-limit counters live in memory.
Hard parts
Decimal prices in admin forms
SQLAdmin could not find a form field for decimal columns in SQLAlchemy 2.1. The fix: a custom field that accepts both a dot and a comma, plus a test that opens every admin form.
Help that keeps up with the code
Every admin section, field and column has a “?” hint and detailed help. To keep it from going stale, a test finds fields and sections without explanations.
False positives of the attack detector
Ordinary client questions sometimes look like an attempt to control the bot. New detector rules are added only together with tests on ordinary questions.
Stack
- Bot
- Python, aiogram 3, Anthropic SDK (Claude)
- Admin
- FastAPI, SQLAdmin, HTMX, SortableJS
- Data
- SQLite (WAL), SQLAlchemy 2 (async), Alembic
- Quality and deployment
- pytest, Ruff, basedpyright, Docker, GitHub Actions
Need a similar system?
Tell me about the task — I reply within a day in RU, RO or EN.