AI Assistant for a Website — Part 3. How We Taught It Not to Lose Potential Clients

In the first two articles in this series, I explained how we built our own AI assistant for a website.

In Part 1, we created a working system for MODX: visitors can talk to the AI directly on the website, and once the conversation turns into a real lead, the information is sent to the site owner by email and Telegram.

In Part 2, we connected the same AI backend to WordPress. As a result, one Cloud Run + Groq service can now work with websites built on different CMS platforms.

The project source code is available on GitHub:

web86/modx-ai-lead-assistant

But during real-world testing, we discovered another important problem.

What happens if a potential client starts a conversation, leaves an email address, but never reaches the final lead handoff?

That is exactly what we solved in the third stage of the project.

Why this matters

Imagine a typical situation.

A visitor opens the AI assistant on a website and enters their email address. Then they start describing what they need.

For example:

I need some changes made to my WordPress website.

The assistant asks a follow-up question:

What exactly would you like to change?

But at that moment, the visitor gets distracted, closes the browser tab, loses their internet connection, or simply decides to continue later.

In the previous version of the system, that visitor could effectively disappear.

Email and Telegram notifications were only sent when the AI decided that enough information had been collected and the request was ready to be handed over to the site owner.

So the potential client had already visited the website and had already provided contact information — but we could still lose them completely.

For a lead assistant, that is a serious problem.

So we changed the basic logic of the system:

once a visitor gives us a contact address, we should not lose it.

Logging now starts before the AI conversation

There is an important technical detail here.

Logging does not start after the first AI response and it does not start after the final lead submission.

It starts as soon as the visitor enters a valid email address and clicks Continue.

In other words, it starts before the first real AI conversation.

Once the email has been validated, the system immediately records an event:

contact_saved

From that moment, the contact already exists in the lead journal.

Even if the visitor closes the page immediately, we still have the email address and information about the page where the conversation started.

When the visitor sends the first real message to the assistant, the system records another event:

first_request

After that, there are two more possible events:

handoff_success — the lead was successfully delivered to the site owner;

handoff_failed — the system tried to deliver the lead, but neither email nor Telegram succeeded.

So the lifecycle is very simple:

Email saved → Conversation started → Lead handed off

And even if the sequence stops at the first or second stage, the information is no longer lost.

We did not build a full CRM

Of course, we could have connected a database, created client cards, statuses, tasks, managers, and turned the project into a small CRM.

But for this AI assistant, that would have been unnecessary at this stage.

The goal is much simpler:

see everyone who left contact information and understand what happened next.

So the system stores events in lightweight JSONL files.

For example:

2026-10-AILeadLogs.jsonl

A separate file is created for each month.

All events from the same conversation share the same conversation_id, so the viewer can combine several technical events into one simple lead record.

Instead of raw events, the administrator sees understandable statuses:

  • Contact only — the visitor left an email address but did not start the conversation;

  • Conversation started — the visitor started talking to the AI;

  • Handoff failed — lead delivery failed;

  • Sent — the lead was successfully delivered.

For this use case, that is more than enough, while keeping the whole system lightweight.

What it looks like in MODX

For MODX 2 and MODX 3, we created a separate lead viewer.

It is located inside the protected part of the website and is available only to an authenticated MODX administrator with the required permissions.

Instead of opening JSON files manually, the administrator sees a normal table.

It can show the visitor's email address, date of the first contact, source page, first message, collected lead information, and final status.

There are also filters.

You can filter by year, month, status, email address, or request text, and export the results to CSV when needed.

By default, the viewer opens with the current year selected, so there is no need to choose the correct period every time.

The lead journal is stored separately from the standard MODX error log. By default, it is stored here:

core/cache/logs/ai-lead-assistant

This keeps CMS debugging messages separate from real visitor leads.

How it works in WordPress

In WordPress, we implemented the same idea in a more native WordPress way.

After installing the plugin, the admin panel gets a new section:

Tools → AI Lead Log

There, the site owner can see the same lead viewer.

It supports filtering by year, month, status, contact information, and request content, as well as CSV export.

Just like in MODX, the current year is selected by default.

Logging settings are available here:

Settings → AI Lead Assistant

So a WordPress site owner does not need to know where the log files are stored or how JSONL works.

They simply open the WordPress admin panel and see the list of leads.

We deliberately do not log everything

When adding logging, it is easy to make another mistake — saving too much information.

For recovering a potential client, the assistant only needs a small amount of data.

We store the contact information, source page, first request, and the main lead data if the AI managed to collect it.

At the same time, the journal deliberately does not store the visitor's IP address or browser User-Agent.

The idea is simple:

store only what is actually needed to work with the lead.

Another important change — AI behavior can now be managed separately

At the same time as adding logging, we changed another important part of the architecture.

Previously, many of the AI instructions were stored directly inside the application code.

That works, but it is not very convenient.

Imagine that the website owner wants to add a new service or wants the AI to give shorter answers.

There should be no need to open a PHP file and modify the application code.

So we moved the business-specific AI instructions into a separate editable setting.

The system now has two logical layers:

protected system rules + editable business rules

The protected layer handles security, lead handoff, and other critical behavior that the site owner should not accidentally break.

The editable layer explains to the AI who you are, what you do, and how it should communicate with visitors.

Where the AI instructions are stored in MODX

In MODX 2 and MODX 3, we use a separate Chunk:

portfolio_assistant.ai_rules

This turned out to be a very convenient solution.

The site administrator can open a normal Chunk in the MODX Manager and edit the instructions just like any other website content.

For example:

We build websites with MODX and WordPress, work with PHP and JavaScript, and integrate APIs and third-party services.

Or:

Reply in a friendly and concise way. Usually 2–5 sentences are enough.

After saving the Chunk, the new rules are automatically used by the assistant.

The Chunk is read as plain text, so MODX does not try to process tags or execute anything inside it.

In WordPress, it is even simpler

In WordPress, we added a separate AI rules field under:

Settings → AI Lead Assistant

The website owner simply opens the plugin settings and sees a large text area containing the assistant instructions.

There, they can change services, tone of voice, languages, qualification questions, and other AI behavior.

This is much more convenient than editing PHP code inside the plugin.

Most importantly, the user is only changing the business logic.

The protected system rules remain inside the application and have higher priority.

What should you write in AI rules?

There is an interesting point here.

At first, it may seem that the more instructions we give the AI, the better it will work.

In practice, that is not always true.

I would not recommend turning the AI rules into a huge document with dozens of pages.

It is better to give the assistant short and specific information.

For example, it is useful to describe:

What you do

List your main services and technologies.

For example:

WordPress and MODX website development. PHP, JavaScript, Node.js, API integrations, automation, website maintenance, and modification of existing websites.

This helps the AI understand whether the visitor's request matches your services.

How it should communicate

For example:

Be friendly, professional, and easy to understand. Do not write overly long answers. Usually 2–5 sentences are enough.

This is much more useful than simply writing:

Be a good assistant.

Which language it should use

In my case, the rule is simple:

Always reply in the language the visitor is currently using.

If the visitor starts in Russian, the AI answers in Russian.

If they switch to English, the assistant switches too.

What it should learn about a potential client

There is no need to make the AI immediately ask ten questions like a questionnaire.

A better instruction is:

Gradually find out what the visitor wants to build, change, or fix, the website URL, CMS or technology, desired result, required integrations, and deadline. Ask one useful question at a time.

This makes the conversation feel much more natural.

What the AI must not invent

This is especially important on a commercial website.

For example:

Do not invent prices, deadlines, discounts, developer availability, or guarantees. If the project needs to be reviewed before giving an estimate, collect the necessary information and suggest sending the request to the developer.

AI models are very good at generating convincing answers.

But a potential client does not need a convincing imaginary price.

What should not be placed in AI rules

I would not recommend using this field to describe system security.

For example, there is no need to write rules about API keys, JSON response formats, Telegram delivery logic, or how a successful handoff is verified.

That is the application's responsibility.

For this reason, the project has a separate protected layer called:

SYSTEM SAFETY AND HANDOFF CONTRACT

It controls things such as making sure the AI cannot tell the visitor:

Great, your request has been sent!

until the server has actually confirmed successful delivery by email or Telegram.

This separation between system instructions and editable business instructions turned out to be very useful.

The site owner can freely teach the assistant about the business without touching critical application logic.

It is no longer just a chatbot

When I started this project, the idea was quite simple.

Add a small chat window to the website where AI could answer visitor questions.

But gradually, the project started turning into something more interesting.

The assistant can now:

understand the visitor's language, explain services, ask follow-up questions, gradually collect project details, send qualified leads by email and Telegram, save contact information before the conversation is complete, and show all leads to the site owner in a convenient admin interface.

And now its behavior can also be changed without editing the application code.

For me, this is already much closer to a real AI Lead Assistant than just another website chatbot connected to an AI model.

The main rule: do not lose the contact

The most important change in this version can be described very simply:

if a visitor trusted us with their contact information, the system should save it.

It does not matter whether they finished the conversation.

It does not matter whether the AI collected all the necessary information or the visitor closed the page ten seconds later.

From that moment, the potential client is no longer lost.

And I think it is exactly these kinds of small practical features that gradually turn AI on a website from an impressive technology demo into a genuinely useful business tool.

In the next stages of the project, we can go even further: improve lead management, add more integrations, and gradually give the assistant more capabilities for real customer communication.

The complete project, including versions for MODX 2, MODX 3, and WordPress, is available on GitHub:

github.com/web86/modx-ai-lead-assistant

Хотите заказать вебсайт? Давайте познакомимся и обсудим ваш проект. 30 минут бесплатно

Контакты
Спросить моего помощника