Is it possible to implement a Whatsapp Chatbot usi...
# ask-ai
c
Is it possible to implement a Whatsapp Chatbot using LIvekit. I don't need to use voice. If yes, give me a snippet in python
t
Thanks for reaching out, Yousri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can use LiveKit for real-time data messaging between participants, but LiveKit does not provide a built-in WhatsApp integration. If your goal is a WhatsApp chatbot (text-only), the typical approach is to use WhatsApp Business API (or a provider like Twilio) to receive/send WhatsApp messages, and build a bridge that forwards messages between WhatsApp and LiveKit data channels or your backend chatbot logic. A few quick questions so I give the right snippet: 1. Do you want LiveKit to participate in the conversation (e.g., send/receive messages inside a LiveKit room) or do you only want LiveKit as an optional channel for other realtime features? 2. Are you using WhatsApp Business API directly or a provider (Twilio, Vonage, etc.)? 3. Do you want the chatbot logic on your server (recommended) or inside a LiveKit client? If you confirm those, I’ll provide a concise Python example showing: • webhook handling for WhatsApp messages (provider-agnostic) • forwarding inbound messages to a LiveKit room via a data message • sending replies back to WhatsApp Which of the three options above matches your use case? Sources: Data packets | LiveKit docs | Realtime data | LiveKit Docs
c
1. I want LiveKit to participate in the conversation (e.g., send/receive messages inside a LiveKit room) 2. I'm using a Twiliio not WhatsApp Business API 3. I have an Agent class handling the chatbot implemented
t
Thanks — I’ll show a concise, practical Python pattern that ties Twilio WhatsApp webhooks to a LiveKit room data channel and your existing Agent class. This makes LiveKit participate in the conversation: inbound WhatsApp → LiveKit room → your Agent → LiveKit room (and optionally back to WhatsApp via Twilio). Prerequisites • A running LiveKit server and server-side SDK access with credentials. • A LiveKit room and a server API client able to call
publish_data
. • Twilio configured to POST incoming WhatsApp messages to your webhook. • Your existing Agent class can accept text input and return a text reply. 1) Webhook handler — receive Twilio WhatsApp message and publish to LiveKit room
Copy code
from flask import Flask, request, jsonify
from twilio.rest import Client as TwilioClient
from livekit import Client as LiveKitClient  # server SDK client
import os

app = Flask(__name__)

# env/config
TWILIO_SID = os.environ['TWILIO_SID']
TWILIO_TOKEN = os.environ['TWILIO_TOKEN']
TWILIO_WHATSAPP_NUMBER = 'whatsapp:+1234567890'
LIVEKIT_URL = os.environ['LIVEKIT_URL']
LIVEKIT_API_KEY = os.environ['LIVEKIT_API_KEY']
LIVEKIT_API_SECRET = os.environ['LIVEKIT_API_SECRET']
ROOM_NAME = 'whatsapp_room'
TOPIC = 'whatsapp_messages'

twilio = TwilioClient(TWILIO_SID, TWILIO_TOKEN)
lk = LiveKitClient(url=LIVEKIT_URL, api_key=LIVEKIT_API_KEY, api_secret=LIVEKIT_API_SECRET)

@app.route('/twilio-webhook', methods=['POST'])
def twilio_webhook():
    sender = request.form.get('From')             # e.g. 'whatsapp:+1425...'
    body = request.form.get('Body', '')
    if not body:
        return ('', 200)

    # Format message payload you want visible in the LiveKit room
    payload = f"{sender}: {body}"

    # Publish to LiveKit room via server API (data channel). Use topic to filter.
    lk.room_publish_data(room=ROOM_NAME, data=payload.encode('utf-8'), topic=TOPIC, reliable=True)

    return ('', 200)
Notes: • Replace
LiveKitClient
call with the server SDK method your environment uses. The important action is
publish_data
to the room with a topic (here
whatsapp_messages
). 2) Agent integration — listen to LiveKit room data messages and respond
Copy code
# Example Agent wrapper that registers a data callback on the LiveKit room
# This assumes you have a running process connected to the LiveKit room as a participant
# and have access to the room object with data callbacks.

class LiveKitWhatsAppBridge:
    def __init__(self, room, agent, twilio_client):
        self.room = room                # livekit room object for the agent participant
        self.agent = agent              # your existing Agent instance with `generate_reply(text)`
        self.twilio = twilio_client

        # Register callback for messages published on TOPIC
        self.room.register_data_callback(self.on_data_message, topic=TOPIC)

    async def on_data_message(self, data_packet):
        # data_packet.data is bytes; decode
        text = data_packet.data.decode('utf-8')
        # Expect format: "whatsapp:+1425...: message" or "whatsapp:+1425...: message"
        # Extract sender if you included it in payload
        if ':' in text:
            sender, user_text = text.split(':', 1)
            user_text = user_text.strip()
        else:
            sender = None
            user_text = text

        # Avoid processing messages sent by this agent participant
        if data_packet.participant and data_packet.participant.identity == self.room.local_participant.identity:
            return

        # Get reply from your Agent (synchronous or async)
        response = await self.agent.generate_reply(user_text) if callable(getattr(self.agent, 'generate_reply', None)) else self.agent.generate_reply(user_text)

        # Publish reply into LiveKit room so other participants (and UI) see it
        await self.room.local_participant.publish_data(response.encode('utf-8'), topic=TOPIC, reliability=True)

        # Also send reply back to WhatsApp via Twilio if we have sender
        if sender:
            self.send_whatsapp(sender, response)

    def send_whatsapp(self, to_whatsapp_number, body):
        # Twilio expects 'whatsapp:+number' format
        self.twilio.messages.create(from_=TWILIO_WHATSAPP_NUMBER, to=to_whatsapp_number, body=body)
3) Architecture summary • Twilio webhook receives incoming WhatsApp messages and calls
/twilio-webhook
. • Webhook publishes the message into the LiveKit room on topic
whatsapp_messages
. • Your LiveKit-connected Agent (as a room participant) receives the data packet via
register_data_callback
, generates a reply, and publishes it back to the room. • Bridge sends the agent reply back to the original WhatsApp sender via Twilio SMS API. Would you like: • A version tailored to your Agent class signature (share the method name and sync vs async), or • A minimal runnable example showing the LiveKit server SDK method names you use (I can adapt the
lk.room_publish_data
call to your SDK)? Sources: Data packets | LiveKit docs | Agents telephony integration | LiveKit docs | Call forwarding | LiveKit docs
a
and what would be the pricing?
t
Regarding pricing for this integration — costs come from two places: LiveKit (agent session minutes) and Twilio (WhatsApp messages). • LiveKit agent sessions: $0.01 per agent session minute when running agents on LiveKit Cloud. Paid plans include monthly minute allotments before overages. • Twilio WhatsApp messages: charged per message by destination country (example: ~ $0.0048 per outbound message to the US). Session/conversation pricing rules apply for 24‑hour windows. Example estimate: • One 5‑minute conversation with ~10 messages: ◦ LiveKit: 5 minutes × $0.01 = $0.05 ◦ Twilio: 10 messages × $0.0048 ≈ $0.048 ◦ Total ≈ $0.098 per conversation If you want, tell me: • expected avg messages per conversation, • avg conversation length (minutes), • primary destination country for messages —I’ll produce a tailored monthly cost estimate. Sources: Deploy and scale agents on LiveKit Cloud | LiveKit Pricing
a
but if i have a 24h session?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
a
my agent will only produce text