A solution for an AI agent

Website leads to Telegram, done properly: never lost, aggregated and analysed

Claude will write a form that sends leads to Telegram from one short request. The trouble starts later: Telegram is down for a minute and the lead is gone; the bot gets flooded with spam; leads have no source, so nobody knows which campaign brought them; the bot token sits in the page code where anyone can see it.

This is a solution that runs inside an AI agent: a detailed spec prompt that gets a working version without these gaps on the first try, plus a checklist to verify it before launch. The forms on this site run on this setup.

A quick note

If you already have a CRM, send leads there and use Telegram for notifications. This solution is for when there is no CRM yet or it would be overkill: a personal site, a landing page, a small team, a test of a new product.

What you get

  • A lead in Telegram within seconds. Name, contacts, request and every form field in one message.
  • A source for every lead. UTM tags, landing page and referrer from the first visit, plus the page and block where the form was filled in.
  • No lost leads. If Telegram doesn’t respond, the handler retries for up to a day, and a health check endpoint reports the problem to monitoring.
  • Spam protection. A hidden honeypot field for bots, a rate limit per IP, field and size validation.
  • Contacts at hand. Every lead is stored on the server, and the /contacts bot command sends a CSV of all contacts. Commands only work in your chat.
  • An analytics event. After a successful submit the form sends generate_lead to Google Tag Manager.
  • The token stays on the server. The page posts the form to your own endpoint, and the bot key lives in an environment variable.

What a lead looks like

New lead from the siteTelegram · now

Topic: AI audit

Name: Anna

Company: Northwind Shop

Email: anna@northwind.example

Industry: E-commerce and retail

We want to understand why our Meta cost per lead keeps rising and what we can automate in reporting.

Source: threads / social / launch · landing: /solutions/ · form: /work/ (request block)

An example with fictional data.

How it works

  1. The page. On the first visit a script remembers the UTM tags, landing page and referrer. The form sends its data with them to your server, not straight to Telegram.
  2. The handler. A small Node.js server with no external dependencies validates the data, filters spam, stores the contact and sends the message through the Telegram Bot API.
  3. The queue. If Telegram doesn’t respond, the lead stays in a queue and is sent again.
  4. The bot. The same bot takes the owner’s commands and sends the contacts export.

Prompt 1. Handler spec

Give it to an agent that works with your site’s code: Claude Code, Cursor or another. Replace anything in square brackets with your own values.

Build a handler for website form leads that sends them to Telegram. The requirements are below; deviate only with an explanation.

Stack and hosting:
- Node.js 18+ with no external dependencies (http, fetch, fs).
- The service listens on 127.0.0.1:[8787]; the web server [Caddy / Nginx] proxies /api/* to it.
- Secrets only in environment variables: TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID. The token must not appear in page code.
- Runs as a systemd service with auto-restart [or a Docker container].

Endpoints:
- POST /api/lead: accepts the form JSON.
- GET /api/health: {ok, queued}; 503 if the queue has unsent messages.

Form fields: [name*, email*, company, telegram, sphere, request*, topic]. An asterisk marks a required field.
Service fields: lang, page, section, utm_source, utm_medium, utm_campaign, landing, referrer.

Protection:
- Hidden honeypot field "website": if filled, reply 200 and send nothing.
- Limit of 5 leads per IP per 10 minutes (IP from X-Forwarded-For), otherwise 429.
- Body size up to 20 KB, email format check, trim long fields.
- Escape HTML in values before sending to Telegram (parse_mode=HTML).

Telegram message:
- Title "New lead from the site", then "Field: value" lines, skipping empty ones.
- The request text as a separate paragraph.
- The last line is the source: utm_source / utm_medium / utm_campaign, landing page, form page and block.

Reliability:
- If Telegram returns an error or doesn't respond, put the message in an in-memory queue and retry with increasing delays for up to 24 hours.
- Reply 200 to the visitor right after validation, without waiting for Telegram.

Storage and export:
- Append every lead as one JSON line to contacts.jsonl in the service data directory.
- The bot uses long polling and only accepts commands from the TELEGRAM_CHAT_ID chat: /contacts sends a CSV of all contacts (unique by email, with sources, first and last date and number of leads).

Front end:
- A script that on the first visit saves utm_source, utm_medium, utm_campaign, landing and the external referrer to localStorage and adds them to every form submit.
- Form submit via fetch with JSON, with "Sending", "Thank you" and "Couldn't send" states.
- After a successful submit, dataLayer.push({event: "generate_lead", form_topic, form_sphere}).

At the end give me:
1. The code files.
2. The web server config for /api/*.
3. The systemd service file.
4. Commands to start it and to test it with curl.
5. A line for the privacy policy about where leads are stored.

Prompt 2. Pre-launch check

Test the lead handler before launch and show the result of every check:
1. A normal lead reaches Telegram, and the source line is filled in.
2. A lead without a required field gets 400 and is not sent to Telegram.
3. A lead with the "website" honeypot filled gets 200, but nothing arrives in Telegram.
4. The sixth lead from one IP within 10 minutes gets 429.
5. HTML in fields (<b>, <script>) arrives in Telegram as text, not markup.
6. With a wrong token the lead goes to the queue and /api/health returns 503; after fixing the token the queue is sent.
7. The /contacts command from another chat is ignored; from your chat a CSV arrives.
8. The page source contains neither the bot token nor the chat_id.

How to launch

  1. Create a bot with @BotFather and get its token.
  2. Send the bot /start and find the chat_id: open https://api.telegram.org/bot<token>/getUpdates in a browser and look for chat.id. For leads in a team chat, add the bot to the group and use the group’s chat_id.
  3. Give the agent prompt 1, then prompt 2.
  4. Put the token and chat_id into the service environment file on the server, start the service and send a test lead from the site.
  5. Connect /api/health to your uptime monitoring so you hear about a problem before a client tells you.

Tips from practice

  • Don’t send the form from the browser straight to Telegram. That would put the token into the page code, and anyone could post as your bot.
  • Store the first touch, not the last. Someone may come from an ad and leave a lead a week later via a direct visit. Without the first tag, that lead counts as “direct”.
  • Reply to the visitor at once. If you wait for Telegram, the form hangs every time Telegram has a hiccup.
  • State in your privacy policy where leads go and where contacts are stored.