A customer emails your support team about a damaged order. They describe the problem, provide the order number, explain what happened. Your AI agent replies, acknowledging the damage and asking for a photo. Then it reaches out on WhatsApp: "Hi Maria, could you send us a photo of the damaged headphones?"
Maria sends the photo on WhatsApp. The AI sees it, confirms a replacement, and sends a shipping confirmation back via email with the tracking number.
Two channels. One conversation. Zero repeated context.
This is the opposite of what most support systems do. In a typical setup, email and messaging are separate silos. The WhatsApp agent does not know about the email. The email agent does not know about the photo. The customer ends up re-explaining the problem on every channel.
This example builds a single Flask app that unifies Email and WhatsApp support with shared memory, so the AI agent carries context across both channels automatically.
What This Example Builds
| Component | Detail |
|---|---|
| Language | Python (Flask) |
| Channels | Email, WhatsApp |
| AI | Telnyx Inference API (OpenAI-compatible, tool-calling) |
| Context store | SQLite (cross-channel conversation history) |
| Identity registry | SQLite (email-to-phone customer resolution) |
| Order tracking | SQLite (status pipeline: delivered → damage_reported → replacement_shipping → resolved) |
| Dashboard | Real-time SSE with Customer, Email Thread, WhatsApp Chat, and Order Timeline panels |
| Demo mode | Runs the full journey without any credentials |
| Lines of code | ~900 (app.py) + ~530 (demo_server.py) |
Products Used
- Telnyx Email API -- receive inbound emails via webhook, send replies programmatically
- Telnyx WhatsApp Business API -- receive inbound WhatsApp messages (text + images) via webhook, send replies with template and freeform support
- Telnyx AI Inference API -- OpenAI-compatible chat completions with tool-calling (send_email, send_whatsapp, update_order, resolve_case)
All APIs authenticate with the same TELNYX_API_KEY. One platform, one bill.
Cross-Channel Identity Resolution
The entire cross-channel experience depends on one function:
def resolve_customer(email=None, phone=None):
"""Resolve a customer by email or phone. Returns customer dict or None."""
if email:
customer = lookup_customer_by_email(email)
if customer:
return customer
if phone:
customer = lookup_customer_by_phone(phone)
if customer:
return customer
return None
When an email arrives from maria.santos@example.com, the email webhook calls resolve_customer(email="maria.santos@example.com"). When a WhatsApp message comes in from +31626781895, the WhatsApp webhook calls resolve_customer(phone="+31626781895"). Both return the same customer record.
The customers table uses unique partial indexes on email and phone:
CREATE UNIQUE INDEX idx_cust_email ON customers (email) WHERE email IS NOT NULL;
CREATE UNIQUE INDEX idx_cust_phone ON customers (phone) WHERE phone IS NOT NULL;
That shared identity is the foundation. Without it, the WhatsApp handler has no way to know that the person sending a damage photo is the same person who emailed about a damaged order.
The Agentic Loop
The AI brain uses Telnyx Inference API with four tools:
TOOLS = [
{"type": "function", "function": {
"name": "send_email",
"description": "Send a formal email to the customer.",
"parameters": {...}
}},
{"type": "function", "function": {
"name": "send_whatsapp",
"description": "Send a WhatsApp message to the customer.",
"parameters": {...}
}},
{"type": "function", "function": {
"name": "update_order",
"description": "Update an order's status or details.",
"parameters": {...}
}},
{"type": "function", "function": {
"name": "resolve_case",
"description": "Mark the support case as resolved.",
"parameters": {...}
}},
]
The agentic loop calls inference, executes any returned tool calls, feeds results back, and repeats until the model stops requesting tools:
while True:
data = call_inference(messages)
choice = data["choices"][0]
msg = choice["message"]
if not msg.get("tool_calls"):
break # No more tools to call -- done
messages.append(msg)
for tc in msg["tool_calls"]:
fn = tc["function"]
tool_name = fn["name"]
tool_input = json.loads(fn.get("arguments", "{}"))
result = execute_tool(tool_name, tool_input, customer)
messages.append({"role": "tool", "tool_call_id": tc["id"], "content": result})
The AI decides the workflow. When Maria emails about a damaged order, the AI chooses to: (1) acknowledge via email, (2) update the order status to damage_reported, and (3) reach out on WhatsApp requesting a photo. No hardcoded sequence -- the model reads the system prompt guidelines and selects the appropriate channel for each action.
WhatsApp Business API: Templates vs Freeform
WhatsApp enforces a messaging window: businesses can only send freeform messages within 24 hours of the customer's last message. Outside that window, you must use an approved template.
The send_whatsapp() function handles both:
def send_whatsapp(to_number, text, template=None, template_params=None, media_url=None):
payload = {"from": WHATSAPP_NUMBER, "to": to_number}
if template:
# Business-initiated: use an approved template
payload["whatsapp_message"] = {
"type": "template",
"template": {
"name": template,
"language": {"policy": "deterministic", "code": "en_US"},
"components": [...]
},
}
elif media_url:
payload["whatsapp_message"] = {
"type": "image",
"image": {"link": media_url, "caption": text},
}
else:
payload["whatsapp_message"] = {"type": "text", "text": {"body": text}}
resp = requests.post(
"https://api.telnyx.com/v2/messages/whatsapp",
headers={"Authorization": f"Bearer {TELNYX_WHATSAPP_API_KEY}", ...},
json=payload,
)
The execute_tool() function also includes fallback logic: if a template message fails (template not approved, or window already open), it falls back to plain text:
try:
send_whatsapp(to_number, text, template=template, template_params=template_params)
except requests.HTTPError:
if template and text:
send_whatsapp(to_number, text) # Fall back to plain text
The Dashboard
The Flask app streams Server-Sent Events (SSE) to a browser dashboard with four panels:
- Customer -- profile card with name, email, phone
- Email Thread -- subject and body cards, inbound (blue border) and outbound (green border)
- WhatsApp -- phone-frame UI with chat bubbles, delivery ticks, image thumbnails
- Order & Timeline -- order status badge transitions (delivered → damage_reported → replacement_shipping → resolved), damage photo, chronological event log
Every significant event -- identity resolution, tool calls, channel messages, order updates -- is published via sse_publish() and rendered in real time.
Run the Demo
The demo runs "Maria's Damaged Headphones" -- the full email-to-WhatsApp journey -- without any Telnyx credentials.
git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/email-whatsapp-support-python
pip install -r requirements.txt
python demo/demo_server.py
Open http://localhost:5050/ and click "Run Demo Journey."
The scripted scenario:
- Maria emails about damaged headphones (order ORD-2024-7891)
- AI acknowledges via email with reference SR-2024-0456
- AI reaches out on WhatsApp requesting a damage photo
- Maria sends the photo on WhatsApp with a description
- AI confirms replacement on WhatsApp
- AI sends shipping confirmation email with tracking number
- Case resolved -- order status:
delivered → damage_reported → replacement_shipping → resolved
Before Using in Production
This example is a working prototype. Areas to address before production:
- Webhook signature verification. Verify the Telnyx signature on every inbound webhook to prevent spoofed events.
- PostgreSQL. Replace SQLite with PostgreSQL for concurrent writes under production load.
- WhatsApp template approval. Submit your templates to Meta for approval before using them in production. The demo uses scripted responses that bypass template requirements.
- DKIM for email deliverability. Configure DKIM, SPF, and DMARC records for your sending domain.
- Task queue. Replace background threads with Celery or a similar task queue for reliability at scale.
- Media handling. The demo uses a placeholder URL for the damage photo. In production, download and store WhatsApp media via the Telnyx media retrieval endpoint.
Why This Pattern Is Useful
The damaged-order scenario is specific, but the pattern generalizes. Any business that communicates with customers across email and messaging faces the same problem: siloed channels, repeated context, fragmented conversation history.
The fix is identity resolution plus shared memory. If the system knows that maria.santos@example.com and +31626781895 are the same person, and every message from every channel is stored against that person's ID, then any channel can pick up where the last one left off.
Email for formal correspondence. WhatsApp for quick updates and photo collection. One AI brain that decides which channel fits each step. One conversation history that spans both.