Case study · AI agent · n8n
An AI sales agent that sells in Arabic, around the clock
I designed and built a Telegram sales agent for Dakhlio, a digital-products brand I founded. It answers every buyer instantly in natural Arabic, qualifies them, sends the checkout link at the right moment, follows up when they go quiet, and hands any payment or problem to a human without the customer noticing.
24/7
instant Arabic replies to every buyer
45 nodes
in one production n8n workflow
15 / 15
end-to-end scenarios passing
- Role
- Sole builder: product and conversation design, prompt engineering, workflow architecture, testing, and deployment
- Stack
- n8n · OpenAI GPT-4.1 (LangChain agent) · Telegram Bot API · n8n Data Tables · JavaScript · Python test harness
- Scope
- 45-node production workflow, plus an error-monitoring workflow and an automated setup workflow
- Deployment
- Ran in production on n8n Cloud; v2 is packaged for self-hosting on n8n 2.49 (scripted install, backups, and a secure tunnel) and runs on demand for demos

The challenge
Dakhlio sells a digital guide to Arabic-speaking buyers who arrive through Telegram. Sales depended on three things a small team could not do by hand:
- Reply in seconds, at any hour, in natural Arabic: a slow answer is a lost sale.
- Keep humans on the moments that need them, such as payment receipts and complaints, without the buyer seeing a handover.
- Follow up with every buyer who asks for the link and goes quiet.
The solution
- An AI consultant with a defined persona qualifies the buyer in three short messages, handles objections, and sends the checkout link only after a clear yes.
- Payment and problem messages bypass the AI and reach staff immediately; staff take over or hand back from a single Telegram group.
- A follow-up engine re-engages quiet buyers twice, respecting quiet hours.
- Every customer's mode, sales stage, and timers live in one state table, so the AI, the staff, and the scheduler always agree.
Architecture
One n8n workflow with two entry points: a Telegram webhook for every message and a 5-minute schedule for follow-ups. Telegram is both the customer channel and the staff console, so the team needs no extra tool.

AI agent design
The model never drives the workflow with free text. Every turn, it returns one JSON object, which a code node validates before anything reaches the customer:
{"reply": "...", "intent": "hot|warm|cold", "stage": "greeting|discovery|objection|checkout_sent|paid|lost", "objection": "...", "next_action": "await_reply|send_checkout|followup_30m|followup_24h|escalate_human|stop|block_user"}- Persona: a named consultant writing Modern Standard Arabic with a light Levantine tone, two or three lines per reply.
- Locked wording for the moments that matter: the price, the checkout message, and the refund and affiliate policies.
- Each next_action is wired to a real branch: send the link, arm a follow-up, escalate, stop, or block.
- The saved sales stage is fed back every turn, so a conversation resumes exactly where it left off, even after a restart.
- A defensive parser guarantees the customer always gets a clean reply, whatever the model returns.
Human-in-the-loop
Staff work from one Telegram group. They take a chat over by replying to the bot's alert, and the customer keeps talking to the same brand account throughout.
| From | Trigger | To |
|---|---|---|
| AI | Payment or problem keywords: AI skipped, staff and admin alerted | Human |
| AI | The agent decides a person should answer | Human |
| Any | Staff reply to an alert in the group | Human |
| Human | Staff send the hand-back command | AI |
| AI | Abusive user | Blocked |
Follow-up engine
- A buyer who receives the checkout link and goes quiet gets two nudges: after 30 minutes and after 24 hours.
- No messages between 23:00 and 09:00 local time; any reply from the customer resets the cycle.
- Each send is independent, so one failed delivery never blocks the rest of the batch.
- Runs every 5 minutes on self-hosted n8n, with execution history pruned automatically after 14 days.
Reliability and security
- Two attempts on the OpenAI call, then a polite holding reply to the customer and an alert to staff.
- A dedicated error workflow reports any failed run to the staff room.
- All outgoing text is HTML-escaped, so names and symbols always render correctly in Telegram.
- Webhooks are verified with Telegram's secret token using a constant-time comparison; n8n listens only on localhost, behind an HTTPS tunnel.
- Credentials stay in n8n's encrypted store; chat IDs and settings come from environment variables.
- Group chats and blocked users are filtered out before any AI call, which keeps model costs down.
Quality assurance
I built an automated end-to-end suite that runs the real workflow on a real n8n instance, with only Telegram and OpenAI replaced by a Python mock server. Each test sends a signed update and checks what the bot sent, to whom, and what the state table holds afterward. All 15 scenarios pass.
| # | Scenario | Verified |
|---|---|---|
| 1–2 | Greeting, then a request for the link | One AI call per turn; stage saved; follow-up armed |
| 3–6 | Payment message, staff takeover, and hand-back | No AI call; staff alerted; mode switches both ways |
| 7–8 | Customer photo; message from a group | Photo forwarded to staff; group ignored at zero cost |
| 9–11 | Malformed AI output, special characters, and stop | Clean reply delivered; text escaped; nothing sent on stop |
| 12–15 | Request for a human, shared location, abuse, and a blocked user | Escalated; staff alerted; block applied; zero AI calls |
Production deployment
- Version 2 is a production-hardening release: every agent decision wired to an action, configuration moved to the environment, safe message formatting everywhere, private-chat filtering, and the error workflow.
- Self-hosted on Windows without Docker: one-click install, start, stop, and backup scripts; an HTTPS tunnel; and automatic backups of workflows, encrypted credentials, and the database every time n8n stops.
- Workflows, scripts, and the full test suite are packaged so the system can be rebuilt on a new machine from scratch.