WhatsApp

How to Build a WhatsApp Restaurant Reservation Bot with Python, Telnyx, and AI Function Calling

Customers expect to book a table the same way they message a friend: open WhatsApp, say "table for 4 tonight," and get a confirmation. No app download, no phone tree, no web form. Building that experience requires a messaging API that supports interactive messages (lists, buttons), an AI layer that can call real tools (search, availability check, booking), and a way to push updates to a dashboard in real time.

This walkthrough covers DineNyx, an open-source Python/Flask application that combines the Telnyx WhatsApp Business API with Telnyx AI Inference to create a fully agentic restaurant reservation concierge. A customer sends a WhatsApp message, the AI agent searches real restaurants via the Google Places API, checks table availability, makes the booking, and sends an interactive confirmation -- all within a single conversation. Telnyx AI Communications Infrastructure provides every layer of this stack: WhatsApp messaging, AI inference with function calling, and speech-to-text transcription for voice notes.

The full code is open source and runs locally in under five minutes.

What Does the WhatsApp Reservation Bot Do?

DineNyx supports three booking paths, all converging on the same AI agent:

PathHow it works
Dashboard deeplinkClick "Book via WhatsApp" on a restaurant card. Opens WhatsApp with a pre-filled message.
Freeform textMessage the WhatsApp number directly: "Table for 4 at De Kas, Friday 7pm."
Voice noteRecord an audio message. The AI transcribes it and processes the booking request.

The AI concierge uses five tools via function calling to handle each step of the reservation lifecycle:

  • search_restaurants -- Find restaurants by name, cuisine, or city
  • check_availability -- Query open time slots for a date and party size
  • make_reservation -- Create a confirmed booking
  • cancel_reservation -- Cancel an existing reservation
  • send_whatsapp -- Reply with text, interactive list, or button messages

A three-panel web dashboard (restaurant browser, live WhatsApp phone mockup, reservation board) updates in real time via Server-Sent Events as conversations and bookings progress.

Architecture: How the Components Fit Together

Customer WhatsApp
        |
        v
Telnyx WhatsApp Webhook (POST /webhooks/whatsapp)
        |
        v
Flask App (app.py)
  ├── Webhook handler: dedup, extract text/audio/interactive reply
  ├── AI Inference (Telnyx API, GLM3-flash model)
  │     └── Tool calling loop (up to 10 iterations)
  │           ├── search_restaurants  --> Google Places API + SQLite cache
  │           ├── check_availability  --> Table inventory (6 tables, 8 time slots)
  │           ├── make_reservation    --> SQLite INSERT + SSE push
  │           ├── cancel_reservation  --> SQLite UPDATE + SSE push
  │           └── send_whatsapp       --> Telnyx WhatsApp API (text/list/buttons)
  ├── Voice transcription (Telnyx STT, Whisper large-v3-turbo)
  └── SSE stream (/stream) --> Real-time dashboard

This is an Infrastructure (Agent Platform) pattern: a single Telnyx platform replaces what would otherwise require separate providers for messaging, AI inference, and speech-to-text. No orchestration framework, no third-party LLM vendor, no separate transcription service. One API key surface, one bill.

Prerequisites for Building a WhatsApp AI Bot

  • Python 3.9 or higher
  • A Telnyx account with an API key -- sign up at portal.telnyx.com for free trial credit
  • A Telnyx account with WhatsApp Business API enabled (can be the same account)
  • A WhatsApp-enabled phone number and messaging profile configured in the Telnyx Portal
  • (Optional) A Google Places API key for live restaurant data
  • ngrok or another tunnel for local webhook testing

Step 1: Clone the Repository and Install Dependencies

git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/whatsapp-restaurant-reservation-python
pip install -r requirements.txt

The dependency footprint is minimal -- three packages:

flask>=3.0
python-dotenv>=1.0
requests>=2.31

No heavy ML libraries, no LangChain, no orchestration framework. The AI agent loop is implemented in plain Python against the Telnyx AI Inference REST API.

Step 2: Configure Environment Variables

cp .env.example .env

Edit .env with your credentials:

TELNYX_API_KEY=KEY_your_main_api_key
TELNYX_WHATSAPP_API_KEY=KEY_your_whatsapp_api_key
WHATSAPP_NUMBER=+15551234567
WHATSAPP_MESSAGING_PROFILE_ID=your_messaging_profile_id
GOOGLE_PLACES_API_KEY=your_google_places_key  # optional
DEFAULT_CITY=Amsterdam
VariableRequiredPurpose
TELNYX_API_KEYYesMain account -- AI Inference and speech-to-text
TELNYX_WHATSAPP_API_KEYYesWABA account -- WhatsApp messaging
WHATSAPP_NUMBERYesYour WhatsApp-enabled phone number (E.164)
WHATSAPP_MESSAGING_PROFILE_IDYesMessaging profile for WhatsApp routing
GOOGLE_PLACES_API_KEYNoLive restaurant data with ratings, photos, hours
DEFAULT_CITYNoCity for restaurant search (default: Amsterdam)

Why two API keys? WhatsApp Business API requires a WABA-enabled Telnyx account. If both services are on a single account, the keys can be identical.

Step 3: Build the AI Agent with Telnyx Inference and Function Calling

The core of the application is an agentic loop that calls Telnyx AI Inference, executes any tool calls the model requests, feeds results back, and repeats until the model finishes or sends a WhatsApp message.

INFERENCE_URL = "https://api.telnyx.com/v2/ai/chat/completions"
AI_MODEL = os.getenv("AI_MODEL", "THUDM/GLM3-flash")

def call_inference(messages):
    resp = requests.post(
        INFERENCE_URL,
        headers={
            "Authorization": f"Bearer {TELNYX_API_KEY}",
            "Content-Type": "application/json",
        },
        json={
            "model": AI_MODEL,
            "messages": messages,
            "tools": TOOLS,
            "max_tokens": 4096,
        },
        timeout=30,
    )
    resp.raise_for_status()
    return resp.json()

The TOOLS array defines five functions with JSON Schema parameters. At inference time, the model decides which tools to call and with what arguments. The loop runs up to 10 iterations:

max_iterations = 10
for _ in range(max_iterations):
    data = call_inference(messages)
    choice = data["choices"][0]
    msg = choice["message"]

    if not msg.get("tool_calls"):
        # No more tools -- AI is done
        break

    messages.append(msg)

    sent_reply = False
    for tc in msg["tool_calls"]:
        fn = tc["function"]
        tool_input = json.loads(fn.get("arguments", "{}"))
        result = execute_tool(fn["name"], tool_input, customer_phone)
        messages.append({
            "role": "tool",
            "tool_call_id": tc["id"],
            "content": result,
        })
        if fn["name"] == "send_whatsapp":
            sent_reply = True

    if sent_reply:
        break  # Agent has replied -- stop the loop

Each tool call maps to a Python function: search_restaurants queries Google Places API and caches results in SQLite; check_availability checks table inventory against existing reservations; make_reservation inserts a confirmed booking; send_whatsapp dispatches text, list, or button messages through the Telnyx WhatsApp API.

How to Send Interactive WhatsApp Messages with Telnyx

WhatsApp supports three interactive message types. The Telnyx API sends all three via POST /v2/messages/whatsapp:

List messages let customers pick from a scrollable set of options (restaurants, time slots):

def send_whatsapp_list(to, body_text, button_text, sections):
    payload = {
        "from": WHATSAPP_NUMBER,
        "to": to,
        "messaging_profile_id": WHATSAPP_MESSAGING_PROFILE_ID,
        "whatsapp_message": {
            "type": "interactive",
            "interactive": {
                "type": "list",
                "body": {"text": body_text},
                "action": {
                    "button": button_text,
                    "sections": sections,
                },
            },
        },
    }
    resp = requests.post(
        "https://api.telnyx.com/v2/messages/whatsapp",
        headers=_wa_headers(), json=payload, timeout=10,
    )
    resp.raise_for_status()

Button messages provide up to 3 quick-reply options (Confirm / Change / Cancel). The payload structure mirrors the list message but uses "type": "button" with a buttons array of reply objects. If interactive messages fail (the account may not be fully approved), the app automatically falls back to plain text.

How to Transcribe WhatsApp Voice Notes with Telnyx Speech-to-Text

When a customer sends an audio message, the webhook payload includes a media URL. The app downloads the OGG file and transcribes it with the Telnyx Speech-to-Text API (Whisper large-v3-turbo model):

def transcribe_audio(audio_url):
    # SSRF protection: only allow Telnyx-hosted media
    parsed = urlparse(audio_url)
    host = (parsed.hostname or "").lower()
    if not any(host.endswith(d) for d in ALLOWED_AUDIO_DOMAINS):
        return "[Voice note from untrusted source -- skipped]"

    # Download the audio file
    audio_resp = requests.get(
        audio_url,
        headers={"Authorization": f"Bearer {TELNYX_WHATSAPP_API_KEY}"},
        timeout=15,
    )
    audio_resp.raise_for_status()

    # Transcribe via Telnyx AI
    files = {"file": ("voice.ogg", audio_resp.content, "audio/ogg")}
    resp = requests.post(
        "https://api.telnyx.com/v2/ai/audio/transcriptions",
        headers={"Authorization": f"Bearer {TELNYX_API_KEY}"},
        files=files,
        data={"model": "openai/whisper-large-v3-turbo"},
        timeout=30,
    )
    resp.raise_for_status()
    return resp.json().get("text", "")

The transcribed text feeds into the same AI agent loop as a regular text message.

Step 4: Handle WhatsApp Webhooks for Inbound Messages

Configure your Telnyx WhatsApp messaging profile to send webhooks to your server:

https://your-server.example.com/webhooks/whatsapp

For local development, expose your server with ngrok:

ngrok http 5051

The webhook handler at POST /webhooks/whatsapp filters for message.received events with direction: inbound, extracts the message text (or interactive reply selection, or audio URL), and dispatches processing to a background thread. Returning 200 immediately prevents Telnyx from retrying the delivery while the AI agent loop runs.

Step 5: Run the Application and Test It

python app.py

The dashboard opens at http://localhost:5051. In a second terminal:

ngrok http 5051

Set the ngrok URL as your webhook in the Telnyx Portal, then send a WhatsApp message to your configured number. The three-panel dashboard shows the conversation in real time: restaurant browser on the left, WhatsApp phone mockup in the center, reservation board on the right.

Run the smoke tests (no API credentials required -- all HTTP calls are mocked):

python smoke_test.py

How the Real-Time Dashboard Works with Server-Sent Events

The dashboard subscribes to GET /stream, an SSE endpoint that pushes events as they happen:

EventTriggers when
channel.whatsappInbound or outbound WhatsApp message
reservation.newBooking confirmed
reservation.updateStatus changed (cancelled, completed)
agent.thinkingAI is processing
agent.tool_callAI invoked a tool
agent.doneAI finished responding

The SSE pattern uses a thread-safe subscriber list. Every connected browser client gets its own queue, and sse_publish() fans events out to all subscribers:

def sse_publish(event_type, data):
    payload = json.dumps({"type": event_type, **data})
    with _sse_lock:
        for q in _sse_subscribers:
            q.put_nowait(payload)

No WebSocket library, no polling. The browser's native EventSource API handles reconnection automatically.

Production Considerations for WhatsApp AI Agents

Three patterns in this codebase are worth adopting in any WhatsApp AI application:

1. Message deduplication. Telnyx may deliver the same webhook multiple times. The app tracks processed message IDs and enforces a per-phone cooldown (3 seconds) to prevent the AI agent from responding twice to the same message.

2. SSRF-hardened audio pipeline. Before downloading a voice note, the app validates that the URL points to a Telnyx domain. This prevents an attacker from crafting a webhook payload that tricks the server into making requests to internal services.

3. City-validated caching. Google Places API results are cached in SQLite, but the cache validates that a restaurant's address actually contains the requested city name. This prevents results from neighboring cities from being misfiled.

Why Telnyx AI Communications Infrastructure for WhatsApp Bots

This application uses four Telnyx products: WhatsApp Business API, AI Inference, Speech-to-Text, and Webhooks. All four share the same API key surface and the same billing account. There is no separate AI vendor to manage, no external STT service to provision, and no third-party orchestration framework. Telnyx AI Communications Infrastructure provides the messaging layer, the intelligence layer, and the real-time delivery layer as a single integrated platform.

The AI Inference API supports function calling natively, so the agent loop is 40 lines of Python -- no LangChain, no CrewAI, no abstraction layers. The model decides which tools to call, the app executes them, and the results feed back into the next inference call.

Related Examples

Frequently Asked Questions

How does the AI agent decide which tool to call during a WhatsApp conversation?

The application sends the full conversation history along with a JSON Schema definition of each available tool to the Telnyx AI Inference API (POST /v2/ai/chat/completions). The GLM3-flash model analyzes the conversation context and returns a tool_calls array in its response specifying which function to invoke and with what arguments. The app executes the tool, appends the result to the message history, and calls inference again. This loop continues until the model calls send_whatsapp to reply to the customer or produces a response with no further tool calls.

What WhatsApp message types does this bot support?

The bot supports three WhatsApp message types via the Telnyx API: plain text messages for general conversation, interactive list messages for presenting restaurant options or time slots in a scrollable picker, and interactive button messages (up to 3 buttons) for booking confirmation with Confirm / Change / Cancel options. All three types are sent via POST /v2/messages/whatsapp with the whatsapp_message.type set to either "text" or "interactive". If interactive messages fail, the bot automatically falls back to plain text.

Can I use this with a city other than Amsterdam?

Yes. Set the DEFAULT_CITY environment variable to any city. If a Google Places API key is configured, the app fetches live restaurant data for that city with ratings, photos, pricing, and hours. Without a Google Places key, the app ships with fallback data for Amsterdam, Rotterdam, The Hague, and Utrecht. The AI agent also supports cross-city searches within a single conversation -- a customer can ask about restaurants in multiple cities.

How does voice note booking work with Telnyx Speech-to-Text?

When a customer sends a WhatsApp audio message, the Telnyx webhook payload includes a media URL pointing to the audio file. The app downloads the OGG file (after SSRF validation), sends it to the Telnyx Speech-to-Text endpoint (POST /v2/ai/audio/transcriptions) using the Whisper large-v3-turbo model, and feeds the transcribed text into the same AI agentic loop as a regular text message. The customer experience is seamless -- they can say "book a table for 2 at Toscanini tomorrow at 7" and the agent handles it identically to a typed message.

Why does this example use two separate Telnyx API keys?

The Telnyx WhatsApp Business API requires a WABA-enabled account, which may be a separate Telnyx account from the one used for AI Inference and Speech-to-Text. The TELNYX_API_KEY handles AI model calls and transcription, while TELNYX_WHATSAPP_API_KEY handles sending and receiving WhatsApp messages. If both services are provisioned on a single account, both variables can use the same key.

Ready to build with low-latency voice AI?

Join developers building the future of real-time conversations