In my previous article, “How to Build an AI Chat for a Website: MODX, Groq, Cloud Run, Email, and Telegram,” I showed how I built an AI assistant for a MODX website.
The assistant can:
-
chat with website visitors;
-
ask a few useful questions about their project;
-
remember the conversation;
-
understand when enough information has been collected;
-
create a short AI summary;
-
send the request by email;
-
send an additional notification through Telegram.
The AI itself works through Groq, while a small Google Cloud Run service acts as a secure gateway between the website and external APIs.
After the MODX version was working, I had a simple question:
Can I use the same Cloud Run service with WordPress?
The answer is yes.
In fact, almost the whole AI infrastructure can stay exactly the same.
We only need to add a small WordPress adapter.
The full source code for both MODX and WordPress is available here:
https://github.com/web86/modx-ai-lead-assistant
What We Do Not Need to Build Again
In the previous article, we already created this part:
Google Cloud Run
│
├── /chat
│ ↓
│ Groq
│ ↓
│ GPT-OSS 120B
│
└── /telegram
↓
Telegram Bot API
Cloud Run already knows how to:
-
receive requests from the website;
-
securely authenticate them;
-
send requests to Groq;
-
receive structured AI responses;
-
send Telegram notifications;
-
keep API keys away from the browser.
None of this depends on MODX.
So when moving the assistant to WordPress, we do not need another Cloud Run service, another Telegram bot, or another AI setup.
The New Architecture
Now the same AI backend can work with two different CMS platforms:
┌── MODX
│ chat.php
Visitor → Chat Widget ───┤
│
└── WordPress
REST API
│
▼
Cloud Run
/chat /telegram
│ │
▼ ▼
Groq Telegram
The only thing that changes is the small layer between the website and Cloud Run.
For MODX we use:
/assets/components/assistant/api/chat.php
For WordPress we use:
/wp-json/web86-ai-lead/v1/chat
Everything after that is shared.
1. A Small WordPress Plugin
For WordPress, the easiest solution is to create a normal plugin.
The structure is very simple:
wp-content/
└── plugins/
└── web86-ai-lead-assistant/
└── web86-ai-lead-assistant.php
In the GitHub repository, the ready-to-use file is here:
wordpress/
└── web86-ai-lead-assistant.php
After uploading it, activate:
Plugins
→ Web86 AI Lead Assistant
The plugin then adds its own settings page:
Settings
→ AI Lead Assistant
2. WordPress Settings
The plugin settings include:
Enabled
Owner name
AI rules
Model
Lead email
History messages
Cloud Run chat URL
Gateway secret
Telegram
Cloud Run Telegram URL
For example:
Model:
openai/gpt-oss-120b
Cloud Run chat URL:
https://YOUR-SERVICE.run.app/chat
Cloud Run Telegram URL:
https://YOUR-SERVICE.run.app/telegram
These are the same Cloud Run endpoints we already used with MODX.
No new deployment is required.
3. AI Rules Can Be Edited from WordPress
One useful improvement in the WordPress version is that the assistant's business rules do not have to be hardcoded inside PHP.
There is an AI rules field in:
Settings
→ AI Lead Assistant
For example, you can write:
Be friendly and concise.
Reply in the visitor's language.
Our main services are:
- WordPress
- MODX
- PHP
- JavaScript
- Node.js
- API integrations
Ask only one useful question at a time.
If the visitor needs a new website, try to understand:
- what type of website they need;
- whether they already have a design;
- what integrations are required;
- what the approximate scope is.
Do not give an exact project price.
Explain that Konstantin needs to review the task first.
This means the assistant's behavior can be changed directly from the WordPress admin panel without editing PHP or redeploying Cloud Run.
Some important rules still remain protected inside the plugin, including:
-
protection against prompt injection;
-
rules for handling secrets;
-
rules for accurate answers;
-
rules for lead handoff.
So the editable field controls business behavior, while the more important technical rules stay protected.
4. Where to Keep the Gateway Secret
The gateway secret should never be visible in JavaScript or in the page source.
It can be saved in WordPress settings, but for a production website I prefer keeping it in:
wp-config.php
For example:
define(
'WEB86_AI_GATEWAY_SECRET',
'YOUR_LONG_RANDOM_SECRET'
);
The plugin will use this value instead of the one stored in the WordPress database.
So the flow looks like this:
Browser
↓
WordPress
↓
Gateway Secret
↓
Cloud Run
The visitor's browser never sees the secret.
5. WordPress Gets Its Own REST API Endpoint
In MODX, the frontend talks directly to a PHP file.
In WordPress, it is more natural to use the built-in REST API.
The plugin creates this endpoint:
POST /wp-json/web86-ai-lead/v1/chat
The important thing is that normal website visitors are allowed to use it.
They do not need to be logged into WordPress.
The plugin still checks the request itself, including:
-
whether it comes from the same website;
-
whether the email looks valid;
-
whether the message is not too large;
-
whether the visitor is sending too many messages;
-
whether the conversation ID is valid.
Only after these checks does WordPress contact Cloud Run.
6. The Frontend Barely Changes
The good part is that the same HTML, CSS, and JavaScript widget can work with both MODX and WordPress.
For MODX we use:
<div
class="fw-assistant"
id="fwAssistant"
data-endpoint="/assets/components/assistant/api/chat.php"
>
For WordPress we simply change the endpoint:
<div
class="fw-assistant"
id="fwAssistant"
data-endpoint="/wp-json/web86-ai-lead/v1/chat"
>
That is basically the main frontend difference.
7. Why We Need a conversation_id
There is one important difference between the MODX and WordPress versions.
The original MODX version keeps the conversation in a normal PHP session.
For WordPress, I decided not to start custom PHP sessions.
Instead, the browser creates a random:
conversation_id
For example:
{
"email": "john@example.com",
"message": "I need a new WordPress website",
"page_url": "https://example.com/",
"conversation_id": "a71ca84258904c58b596d818309cb9730a88"
}
The ID is generated in the browser and kept in:
sessionStorage
It only exists for the current browser session.
8. WordPress Remembers the Conversation with Transients
WordPress receives the conversation_id and uses it to find the current conversation.
The conversation can contain:
email
page URL
message history
handoff status
This information is stored with the WordPress Transients API for up to 12 hours.
So the simplified flow looks like this:
Browser
│
│ conversation_id
▼
WordPress
│
▼
Transient
│
├── email
├── conversation history
├── source page
└── handoff status
For a small website assistant, this is usually enough.
9. Why I Prefer Transients Here
WordPress websites often already use different caching systems:
-
page cache;
-
object cache;
-
Redis;
-
CDN;
-
optimization plugins.
Because of that, adding custom PHP sessions is not always the best idea.
The WordPress Transients API fits the platform more naturally.
It also keeps the WordPress version independent from the MODX implementation.
10. The Conversation History Is Limited
We also do not need to send the entire conversation to the AI forever.
The plugin has a setting called:
History messages
For example:
10
This means only a limited number of recent messages are sent to the AI.
That helps reduce:
-
token usage;
-
request size;
-
response time;
-
the chance of hitting API limits.
11. WordPress Talks to the Same Cloud Run Service
When a visitor sends a message, WordPress sends it to the same Cloud Run /chat endpoint we already created for MODX.
The request includes:
AI model
assistant instructions
conversation history
response format
Cloud Run then forwards it to Groq.
The result comes back to WordPress and then to the visitor.
The important part is that WordPress never needs direct access to the Groq API key.
12. The AI Does Not Care Which CMS We Use
This is one of the biggest advantages of this setup.
The AI does not really know whether the website uses:
MODX
WordPress
Laravel
plain PHP
Node.js
It simply receives:
-
instructions;
-
conversation history;
-
visitor information.
And it returns a structured result such as:
{
"reply": "...",
"ready_to_handoff": true,
"handoff_message": "...",
"lead": {
"name": "John",
"website": "example.com",
"request": "...",
"summary": "..."
}
}
So the AI logic itself stays shared.
13. Lead Handoff Works the Same Way
The AI does not send anything by itself.
It only tells the server:
"ready_to_handoff": true
This means:
I think we now have enough information to send this request to the developer.
WordPress then tries to send the lead by email and, if enabled, Telegram.
Only after at least one real delivery succeeds does the visitor see a message saying that the request has been passed to the developer.
The flow is:
AI
↓
ready_to_handoff
↓
WordPress
↓
Email / Telegram
↓
Success?
↓
Only then:
"Your request has been sent"
This prevents the AI from telling the visitor that something was sent when it actually was not.
14. Email Through wp_mail()
For WordPress, email is sent using the standard:
wp_mail()
This means the assistant can use the same mail setup as the rest of the website.
If you already have an SMTP plugin configured in WordPress, the assistant can normally use it too.
The email can contain:
Visitor email
Name
Website
Source page
Request
AI summary
Conversation
One small detail is worth remembering:
A successful wp_mail() result means WordPress accepted the message for sending.
It does not necessarily mean the email has already reached the final inbox.
15. Telegram Did Not Need Any Changes
Telegram is a good example of why the shared Cloud Run gateway is useful.
WordPress does not need to know the Telegram bot token.
Instead, it simply calls:
Cloud Run /telegram
The flow is:
WordPress
↓
Cloud Run /telegram
↓
Telegram Bot API
↓
Telegram
The Telegram bot token remains stored safely in Google Secret Manager.
WordPress never receives it.
16. One Cloud Run Service Can Work with Several Websites
At this point another advantage becomes obvious.
The same Cloud Run gateway can potentially work with more than one website:
MODX website
\
\
WordPress website
\
→ Cloud Run → Groq
/
Another website
A new website does not need its own Groq integration or Telegram integration.
It only needs a small adapter that knows how to talk to the existing gateway.
17. One Problem I Ran Into: WordPress REST API Returned 301
During the first WordPress test, the chat showed:
Could not get a response. Please try again.
In DevTools I saw:
POST /wp-json/web86-ai-lead/v1/chat
→ HTTP 301
→ Location: https://example.com
At first this looked like a problem with the assistant.
But Cloud Run and Groq were not involved at all.
The request never reached our WordPress REST endpoint.
The reason was a WordPress optimization plugin called Clearfy.
Its REST / JSON API blocking option was enabled, so requests from normal visitors to:
/wp-json/
were redirected to the website homepage.
After disabling the REST / JSON API blocking option in Clearfy, the assistant started working normally.
So if your WordPress assistant returns 301, check security and optimization plugins before debugging Cloud Run or Groq.
The endpoint:
/wp-json/web86-ai-lead/v1/chat
must return JSON, not redirect to another page.
18. Testing the WordPress Adapter Manually
Before testing the chat widget itself, you can test the REST endpoint directly.
For example:
curl -i \
-X POST \
"https://example.com/wp-json/web86-ai-lead/v1/chat" \
-H "Content-Type: application/json" \
-H "Origin: https://example.com" \
-d '{
"email": "test@example.com",
"message": "I need a new WordPress website",
"page_url": "https://example.com/",
"conversation_id": "a71ca84258904c58b596d818309cb9730a88"
}'
A normal response looks something like:
{
"ok": true,
"message": "Sure. Tell me a little more about the website you need.",
"handoff_sent": false,
"handoff_channels": {
"email": false,
"telegram": false
}
}
And once enough information has been collected:
{
"ok": true,
"message": "Thank you! Your request has been passed to the developer.",
"handoff_sent": true,
"handoff_channels": {
"email": true,
"telegram": true
}
}
19. So What Actually Changed?
Not much.
Cloud Run:
no changes
Groq:
no changes
Telegram:
no changes
AI logic:
almost no changes
Frontend:
+ conversation_id
+ WordPress endpoint
And instead of the MODX adapter, we now have a WordPress REST adapter.
The main WordPress-specific tools are:
wp_mail()
Transients
WordPress REST API
wp_remote_post()
Final Architecture
The project now looks like this:
┌─────────────────────────────┐
│ Website Visitor │
└──────────────┬──────────────┘
│
▼
┌─────────────────────────────┐
│ Vanilla JS Assistant │
└──────────────┬──────────────┘
│
┌───────┴────────┐
│ │
▼ ▼
┌─────────────┐ ┌──────────────┐
│ MODX │ │ WordPress │
│ chat.php │ │ REST API │
└──────┬──────┘ └──────┬───────┘
│ │
└────────┬────────┘
│
▼
┌─────────────────────────────┐
│ Google Cloud Run │
│ │
│ /chat /telegram │
└──────┬──────────────┬───────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Groq │ │ Telegram API │
│ GPT-OSS 120B │ └──────────────┘
└──────────────┘
The Main Idea
When I first built the MODX version, Cloud Run looked mostly like a secure bridge between my website and external APIs.
But after adding WordPress, another advantage became much clearer.
The CMS and the AI infrastructure are now separated.
The CMS handles things like:
visitor data
validation
conversation history
email
website settings
Cloud Run handles:
Groq
Telegram
API secrets
external API requests
Because of this separation, moving the assistant from MODX to WordPress was much easier than building a second assistant from scratch.
I did not need another AI backend.
I only needed another small adapter.
And the same idea can later be used for:
MODX 3
Laravel
Drupal
OctoberCMS
plain PHP
Node.js
or almost any other CMS
Source Code
The full project is available on GitHub:
https://github.com/web86/modx-ai-lead-assistant
WordPress adapter:
wordpress/
├── web86-ai-lead-assistant.php
└── README.md
Shared frontend:
frontend/
├── assistant.html
├── assistant.css
└── assistant.js
Cloud Run gateway:
cloud-run/
├── index.js
├── package.json
└── .env.example
If you already created the Cloud Run service using the previous MODX article, you do not need to deploy it again.
Just add the WordPress adapter, change the frontend endpoint, and the same AI backend can serve both websites.
