Build a Voice Agent or WhatsApp Bot using Twilio

View as Markdown

Overview

This guide shows you how to build a real-time conversational agent with Twilio and Sarvam AI, on either of two channels: a phone voice agent that answers inbound calls, or a WhatsApp bot that replies to text and voice messages. Both use Sarvam AI for speech-to-text, the LLM, and text-to-speech, and are a great starting point for IVR replacements, customer support lines, and conversational agents in Indian languages.

What Youโ€™ll Build

Pick the channel that matches your use case:


Part 1: Phone Call Agent (Telephony)

A voice agent that can:

  • Answer inbound phone calls on a Twilio number
  • Listen to callers speaking, in multiple Indian languages
  • Understand and process what they say
  • Respond back in a natural-sounding voice, over the phone

Quick Overview

  1. Get API keys (Twilio, Sarvam)
  2. Install packages: pip install "pipecat-ai[websocket,sarvam]" python-dotenv
  3. Create a .env file with your API keys
  4. Write about 80 lines of Python code
  5. Point a Twilio phone number at your agent using TwiML
  6. Call your Twilio number

Quick Start

1. Prerequisites

  • Python 3.9 or higher
  • ngrok, to expose your local server during development
  • Accounts and keys from:
    • Twilio: an Account SID, an Auth Token, and a phone number with voice capability
    • Sarvam AI: an API key from your dashboard

2. Install Dependencies

$pip install "pipecat-ai[websocket,sarvam]" python-dotenv loguru

3. Create Environment File

Create a file named .env in your project folder:

SARVAM_API_KEY=sk_xxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then replace each placeholder with your real credentials from the Sarvam dashboard and the Twilio Console.

TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN arenโ€™t optional here. Pipecatโ€™s Twilio transport uses them to authenticate with Twilioโ€™s REST API and to hang up the call cleanly when your agent ends the conversation.

4. Write Your Agent

Create agent.py:

1import os
2from dotenv import load_dotenv
3from loguru import logger
4from pipecat.frames.frames import LLMRunFrame
5from pipecat.pipeline.pipeline import Pipeline
6from pipecat.pipeline.runner import PipelineRunner
7from pipecat.pipeline.task import PipelineParams, PipelineTask
8from pipecat.processors.aggregators.llm_context import LLMContext
9from pipecat.processors.aggregators.llm_response_universal import (
10 LLMContextAggregatorPair,
11)
12from pipecat.runner.types import RunnerArguments
13from pipecat.runner.utils import create_transport
14from pipecat.services.sarvam.stt import SarvamSTTService
15from pipecat.services.sarvam.tts import SarvamTTSService
16from pipecat.services.sarvam.llm import SarvamLLMService
17from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams
18
19load_dotenv(override=True)
20
21async def bot(runner_args: RunnerArguments):
22 """Main bot entry point."""
23
24 # create_transport auto-detects Twilio's WebSocket handshake and builds
25 # the matching TwilioFrameSerializer, using TWILIO_ACCOUNT_SID and
26 # TWILIO_AUTH_TOKEN from the environment.
27 transport = await create_transport(
28 runner_args,
29 {
30 "twilio": lambda: FastAPIWebsocketParams(
31 audio_in_enabled=True, audio_out_enabled=True
32 ),
33 },
34 )
35
36 # Initialize AI services
37 stt = SarvamSTTService(api_key=os.getenv("SARVAM_API_KEY"))
38 tts = SarvamTTSService(api_key=os.getenv("SARVAM_API_KEY"))
39 llm = SarvamLLMService(
40 api_key=os.getenv("SARVAM_API_KEY"),
41 settings=SarvamLLMService.Settings(model="sarvam-105b"),
42 )
43
44 # Set up conversation context
45 messages = [
46 {
47 "role": "system",
48 "content": (
49 "You are Sarvam AI Agent, a multilingual voice assistant powered by Sarvam AI. "
50 "You can understand and respond fluently in English and 10+ Indian languages. "
51 "Keep your responses brief and conversational."
52 ),
53 },
54 ]
55 context = LLMContext(messages)
56 context_aggregator = LLMContextAggregatorPair(context)
57
58 # Build pipeline
59 pipeline = Pipeline(
60 [
61 transport.input(),
62 stt,
63 context_aggregator.user(),
64 llm,
65 tts,
66 transport.output(),
67 context_aggregator.assistant(),
68 ]
69 )
70
71 # Twilio Media Streams send and receive 8kHz mono audio
72 task = PipelineTask(
73 pipeline,
74 params=PipelineParams(
75 audio_in_sample_rate=8000,
76 audio_out_sample_rate=8000,
77 ),
78 )
79
80 @transport.event_handler("on_client_connected")
81 async def on_client_connected(transport, client):
82 logger.info("Caller connected")
83 messages.append(
84 {"role": "system", "content": "Greet the caller and briefly introduce yourself."}
85 )
86 await task.queue_frames([LLMRunFrame()])
87
88 @transport.event_handler("on_client_disconnected")
89 async def on_client_disconnected(transport, client):
90 logger.info("Caller disconnected")
91 await task.cancel()
92
93 runner = PipelineRunner(handle_sigint=runner_args.handle_sigint)
94 await runner.run(task)
95
96if __name__ == "__main__":
97 from pipecat.runner.run import main
98 main()

Thereโ€™s no manual try/except around stt, tts, or llm here on purpose. Those lines just construct pipeline processors; the actual Sarvam API calls happen later, streamed frame-by-frame while the pipeline runs, and Pipecat already catches failures at that layer and surfaces them as ErrorFrames (logged via loguru) instead of raising into your code. Wrapping the setup code in try/except wouldnโ€™t protect anything a real call could fail on.

5. Configure Twilio to Reach Your Agent

Twilio needs a public wss:// URL to stream call audio to. For local development, expose your agent with ngrok.

Start ngrok:

$ngrok http 7860

ngrok prints a forwarding URL that looks like https://xxxx.ngrok-free.app. Youโ€™ll turn this into a wss:// URL in the next step.

The domain suffix ngrok assigns varies per account โ€” you may see ngrok-free.app, ngrok-free.dev, or the older ngrok.io, regardless of whether youโ€™re on macOS, Windows, or Linux. Always use whatever URL ngrok actually prints, not the placeholder shown here.

Free ngrok URLs change every time you restart ngrok. If your agent stops receiving calls, check whether the URL in your TwiML Bin still matches the one ngrok is currently printing.

Create a TwiML Bin:

TwiML Bins arenโ€™t in the Twilio Consoleโ€™s left sidebar by default. The fastest way to reach them is to click the search bar at the top of the console and type โ€œTwiML Binsโ€.

  1. Open the Twilio Console and search for TwiML Bins using the search bar at the top (or find it under Developer Tools in the sidebar)
  2. Click Create new TwiML Bin, give it a friendly name, and paste the following, replacing the domain with your own ngrok URL:
1<?xml version="1.0" encoding="UTF-8"?>
2<Response>
3 <Connect>
4 <Stream url="wss://xxxx.ngrok-free.app/ws" />
5 </Connect>
6</Response>
  1. Click Create to save the bin

Assign it to your phone number:

  1. Go to Numbers & Senders, then Phone Numbers, and select your number
  2. Open its configuration/voice settings tab
  3. Find the setting for what happens when a call comes in, and set it to use your TwiML Bin (Twilio has labeled this option TwiML, TwiML Bin, or similar depending on your console version)
  4. Save

Twilio has redesigned this pageโ€™s tabs and section names more than once (you may see Configuration, Configure, or Configuration Details; Voice & Fax, Voice Configuration, or Voice and emergency calling). The setting you want is always the one controlling what happens when a call comes in. If you canโ€™t find it, Twilioโ€™s own Getting Started with TwiML Bins guide has current screenshots.

6. Run Your Agent

$python agent.py --transport twilio

This starts a local FastAPI server, via Pipecatโ€™s development runner, that accepts Twilioโ€™s Media Streams WebSocket connection at /ws.

7. Test Your Agent

Call your Twilio phone number from any phone. Your voice agent will pick up and start the conversation.


Customization Examples

Example 1: Hindi Voice Agent

1stt = SarvamSTTService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 settings=SarvamSTTService.Settings(
4 model="saaras:v3",
5 language="hi-IN", # Hindi
6 ),
7 mode="transcribe",
8)
9
10tts = SarvamTTSService(
11 api_key=os.getenv("SARVAM_API_KEY"),
12 settings=SarvamTTSService.Settings(
13 model="bulbul:v3",
14 voice="simran", # Or: priya, ishita, kavya, aditya, anand, rohan
15 language_code="hi-IN",
16 ),
17)
18
19llm = SarvamLLMService(
20 api_key=os.getenv("SARVAM_API_KEY"),
21 settings=SarvamLLMService.Settings(model="sarvam-105b"),
22)

language, voice, and language_code are Settings fields, not constructor keyword arguments. Passing them directly to SarvamSTTService(...) or SarvamTTSService(...) raises a TypeError on current versions of pipecat-ai. Only mode (STT) and api_key stay outside settings.

Example 2: Tamil Voice Agent

1stt = SarvamSTTService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 settings=SarvamSTTService.Settings(
4 model="saaras:v3",
5 language="ta-IN",
6 ),
7 mode="transcribe",
8)
9
10tts = SarvamTTSService(
11 api_key=os.getenv("SARVAM_API_KEY"),
12 settings=SarvamTTSService.Settings(
13 model="bulbul:v3",
14 voice="shubh",
15 language_code="ta-IN",
16 ),
17)
18
19llm = SarvamLLMService(
20 api_key=os.getenv("SARVAM_API_KEY"),
21 settings=SarvamLLMService.Settings(model="sarvam-105b"),
22)

Example 3: Multilingual Agent (Auto-detect)

1# Auto-detect the caller's language
2stt = SarvamSTTService(
3 api_key=os.getenv("SARVAM_API_KEY"),
4 settings=SarvamSTTService.Settings(
5 model="saaras:v3",
6 language="unknown", # Auto-detects language
7 ),
8 mode="transcribe",
9)
10
11tts = SarvamTTSService(
12 api_key=os.getenv("SARVAM_API_KEY"),
13 settings=SarvamTTSService.Settings(
14 model="bulbul:v3",
15 voice="anand",
16 language_code="en-IN",
17 ),
18)
19
20llm = SarvamLLMService(
21 api_key=os.getenv("SARVAM_API_KEY"),
22 settings=SarvamLLMService.Settings(model="sarvam-105b"),
23)

Use language="unknown" for support lines where callers might speak any of several languages. Sarvam auto-detects the spoken language per utterance, so the same agent can handle a Hindi caller followed by a Tamil caller without any code changes.

Example 4: Speech-to-English Agent (Saaras)

Saarika transcribes speech to text in the same language. Saaras translates speech directly to English text. Use Saaras when the caller speaks an Indian language but you want to process and respond in English.

1# Caller speaks Hindi, Saaras converts it to English, the LLM processes it,
2# and the agent responds in English.
3
4stt = SarvamSTTService(
5 api_key=os.getenv("SARVAM_API_KEY"),
6 settings=SarvamSTTService.Settings(model="saaras:v3"),
7 mode="translate", # Speech-to-English translation
8)
9
10tts = SarvamTTSService(
11 api_key=os.getenv("SARVAM_API_KEY"),
12 settings=SarvamTTSService.Settings(
13 model="bulbul:v3",
14 voice="aditya",
15 language_code="en-IN",
16 ),
17)
18
19llm = SarvamLLMService(
20 api_key=os.getenv("SARVAM_API_KEY"),
21 settings=SarvamLLMService.Settings(model="sarvam-105b"),
22)

Saaras auto-detects the source language (Hindi, Tamil, and so on) and translates spoken content directly to English text, which makes Indian-language speech usable by English-based LLMs.


Available Options

Language Codes

LanguageCode
English (India)en-IN
Hindihi-IN
Bengalibn-IN
Tamilta-IN
Telugute-IN
Gujaratigu-IN
Kannadakn-IN
Malayalamml-IN
Marathimr-IN
Punjabipa-IN
Odiaod-IN
Auto-detectunknown

Speaker Voices (Bulbul v3)

Male (23): Shubh (default), Aditya, Rahul, Rohan, Amit, Dev, Ratan, Varun, Manan, Sumit, Kabir, Aayan, Ashutosh, Advait, Anand, Tarun, Sunny, Mani, Gokul, Vijay, Mohit, Rehan, Soham

Female (14): Ritu, Priya, Neha, Pooja, Simran, Kavya, Ishita, Shreya, Roopa, Tanya, Shruti, Suhani, Kavitha, Rupali

TTS Additional Parameters

You can customize the TTS service with additional parameters:

1tts = SarvamTTSService(
2 api_key=os.getenv("SARVAM_API_KEY"),
3 settings=SarvamTTSService.Settings(
4 model="bulbul:v3",
5 voice="shubh",
6 language_code="en-IN",
7 pace=1.0, # Range: 0.5 to 2.0
8 ),
9)

Twilio Media Streams always carry 8kHz mono audio. You donโ€™t need to touch sample_rate on SarvamTTSService for this: Pipecat resamples the TTS output to match the audio_out_sample_rate set in PipelineParams automatically.


Understanding the Call Flow

  1. Caller dials your Twilio number. Twilio answers using the TwiML Bin and opens a WebSocket (Media Stream) to your agent.
  2. Transport Input: Your Pipecat transport receives the callerโ€™s audio over the WebSocket, and hands it to STT.
  3. STT (Speech-to-Text): Converts audio to text using Sarvamโ€™s Saarika or Saaras, and the context aggregator adds it to the conversation context.
  4. LLM: Generates a response using Sarvam.
  5. TTS (Text-to-Speech): Converts the response to audio using Sarvamโ€™s Bulbul.
  6. Transport Output: Streams the audio back over the WebSocket, and Twilio plays it to the caller, while the context aggregator saves the assistantโ€™s response to context.

Pro Tips

  • Use language="unknown" to automatically detect the callerโ€™s language. This works well for multilingual support lines, but transcription accuracy is slightly lower than when you specify the exact language code, so prefer an explicit code whenever you already know which language the caller will use.
  • Sarvamโ€™s models understand code-mixing, so your agent can naturally handle Hinglish, Tanglish, and other mixed languages, which is common on real support calls.
  • Use sarvam-105b for the LLM step.

Log the transcript from context_aggregator if you want post-call analytics. Writing it to a file or database is cheap and wonโ€™t slow down the live pipeline.


Troubleshooting

Call connects but thereโ€™s no audio. Confirm the TwiML <Stream url> uses wss://, not wss:/ or https://, and that it points at your current ngrok URL. Free ngrok URLs change on every restart.

Call drops immediately. Check that TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN are set correctly in .env. The Twilio serializer needs both to manage the call.

API key errors. Make sure all keys are in your .env file and that the file sits in the same directory as agent.py.

Module not found. Re-run the install command for your operating system (see Step 2 above).

Poor transcription. Try language="unknown" for auto-detection, or set the exact language code (en-IN, hi-IN, and so on) if you already know it.

Choppy or robotic audio. Make sure audio_in_sample_rate and audio_out_sample_rate are both set to 8000 in PipelineParams. Twilio Media Streams donโ€™t support other rates, and a mismatch causes distorted playback.


Part 2: WhatsApp Bot

A WhatsApp bot that can:

  • Receive inbound WhatsApp messages, text or voice notes, on a Twilio WhatsApp number
  • Transcribe voice notes in multiple Indian languages
  • Understand and process what the user said or typed
  • Reply with text, or optionally, a spoken voice note

Unlike the phone agent, this path doesnโ€™t use Pipecat. WhatsApp messages arrive as one-off HTTP webhook requests rather than a continuous audio stream, so a small web server (Flask) that calls Sarvamโ€™s APIs directly is all you need.

WhatsApp Bot: Quick Overview

  1. Get API keys (Twilio, Sarvam)
  2. Install packages: pip install flask twilio sarvamai python-dotenv requests
  3. Create a .env file with your API keys
  4. Write about 50 lines of Python code
  5. From WhatsApp, send the Sandboxโ€™s join code to opt your number in
  6. Point the Sandboxโ€™s webhook at your agent
  7. Message your Twilio WhatsApp number

WhatsApp Bot: Quick Start

Step 1: Prerequisites

  • Python 3.9 or higher
  • ngrok, to expose your local server during development
  • Accounts and keys from:
    • Twilio: an Account SID and an Auth Token
    • Sarvam AI: an API key from your dashboard

This guide uses the Twilio Sandbox for WhatsApp, a free, instant way to test WhatsApp messaging in development. Moving to production requires registering a WhatsApp Sender, which involves Meta Business verification and can take a few days. See Twilioโ€™s WhatsApp docs when youโ€™re ready to go live.

Step 2: Install Dependencies

$pip install flask twilio sarvamai python-dotenv requests

Step 3: Create Environment File

Create a file named .env in your project folder:

SARVAM_API_KEY=sk_xxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then replace each placeholder with your real credentials from the Sarvam dashboard and the Twilio Console.

TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN are used to authenticate when downloading voice-note media from Twilio, which requires HTTP Basic Auth with these credentials.

Step 4: Write Your Agent

Create whatsapp_agent.py:

1import logging
2import os
3from pathlib import Path
4
5import requests
6from dotenv import load_dotenv
7from flask import Flask, Response, request
8from sarvamai import SarvamAI
9from twilio.twiml.messaging_response import MessagingResponse
10
11load_dotenv(override=True)
12
13logging.basicConfig(level=logging.INFO)
14logger = logging.getLogger(__name__)
15
16app = Flask(__name__)
17sarvam = SarvamAI(api_subscription_key=os.getenv("SARVAM_API_KEY"))
18
19SYSTEM_PROMPT = (
20 "You are Sarvam AI Agent, a multilingual assistant powered by Sarvam AI, "
21 "chatting over WhatsApp. You can understand and respond fluently in "
22 "English and 10+ Indian languages. Keep replies short (2-3 sentences) "
23 "and conversational."
24)
25
26
27@app.route("/whatsapp", methods=["POST"])
28def whatsapp_webhook():
29 num_media = int(request.form.get("NumMedia", 0))
30 twiml = MessagingResponse()
31
32 try:
33 if num_media > 0 and "audio" in request.form.get("MediaContentType0", ""):
34 # Sender sent a voice note - download and transcribe it.
35 # Twilio media URLs require your account credentials to fetch.
36 media_url = request.form["MediaUrl0"]
37 media_response = requests.get(
38 media_url,
39 auth=(os.getenv("TWILIO_ACCOUNT_SID"), os.getenv("TWILIO_AUTH_TOKEN")),
40 timeout=10,
41 )
42 media_response.raise_for_status()
43
44 audio_file = Path("/tmp/voice_note.ogg")
45 audio_file.write_bytes(media_response.content)
46
47 transcript = sarvam.speech_to_text.transcribe(
48 file=open(audio_file, "rb"),
49 model="saaras:v3",
50 language_code="unknown", # auto-detects the sender's language
51 )
52 user_text = transcript.transcript
53 else:
54 user_text = request.form.get("Body", "")
55
56 completion = sarvam.chat.completions(
57 model="sarvam-105b",
58 messages=[
59 {"role": "system", "content": SYSTEM_PROMPT},
60 {"role": "user", "content": user_text},
61 ],
62 )
63 reply_text = completion.choices[0].message.content
64 except Exception:
65 # Network hiccups, a failed download, or an API error shouldn't crash
66 # the webhook - Twilio would just get a 500 and the sender gets silence.
67 logger.exception("Failed to process incoming WhatsApp message")
68 reply_text = "Sorry, I couldn't process that. Please try again in a moment."
69
70 twiml.message(reply_text)
71 return Response(str(twiml), mimetype="application/xml")
72
73
74if __name__ == "__main__":
75 app.run(port=7861)

WhatsApp voice notes arrive as OGG/Opus audio, and Sarvamโ€™s speech-to-text REST API accepts that format directly, no transcoding needed. Voice notes longer than 30 seconds will fail on this endpoint; for longer audio, switch to the Batch STT API.

The try/except around the webhook body exists because this endpoint is a system boundary โ€” it calls two external services (Twilioโ€™s media download, Sarvamโ€™s API) over the network, either of which can fail independently of your code. Replying with a short apology keeps the conversation alive instead of leaving the sender with no response at all.

This example trusts every request to /whatsapp. Before going to production, validate the X-Twilio-Signature header on each request (using twilio.request_validator.RequestValidator) so that requests canโ€™t be spoofed as coming from Twilio.

Step 5: Configure Twilio to Reach Your Agent

Start ngrok:

$ngrok http 7861

ngrok prints a forwarding URL that looks like https://xxxx.ngrok-free.app.

5a. Register your WhatsApp number with the Sandbox (required first)

The Sandbox is a shared Twilio number (+1 XXX XXX XXXX, shown on the console page below), used by every developer testing WhatsApp. Twilio only forwards messages from phone numbers that have explicitly opted in to your sandbox โ€” nothing reaches your webhook until this step is done, no matter how correctly your code or webhook URL is set up.

  1. Open the Twilio Console and go to Messaging โ†’ Try it out โ†’ Send a WhatsApp message (or search for โ€œWhatsApp sandboxโ€ in the console search bar). This page shows the sandbox number and your accountโ€™s unique join code, for example join happy-elephant.
  2. From the WhatsApp app on your phone, send that exact join <your-code> message as a normal WhatsApp text to the sandbox number shown on that page (or scan the QR code Twilio shows, which pre-fills this message for you).
  3. Twilio replies confirming youโ€™re connected. Your number is now opted in to your sandbox.

This opt-in is per phone number, not per account. Anyone else who wants to message your bot (a teammate, a demo user) must send the same join <code> message from their own WhatsApp number first โ€” thereโ€™s no way around this for the Sandbox.

The opt-in expires 3 days after joining (or after the last message exchanged). If your bot suddenly stops receiving messages from a number that worked before, re-send the join <code> message from that WhatsApp number.

5b. Point the Sandbox at your webhook

  1. Still on the Sandbox settings page, find When a message comes in
  2. Set it to your ngrok URL plus /whatsapp (for example, https://xxxx.ngrok-free.app/whatsapp), with the method set to HTTP POST
  3. Save

Free ngrok URLs change every time you restart ngrok. If your bot stops responding, check whether the webhook URL in the sandbox settings still matches the one ngrok is currently printing.

Step 6: Run and Test Your Agent

$python whatsapp_agent.py

Send a text message, or a voice note, to your Twilio WhatsApp number from any phone. Your bot will transcribe and reply within a few seconds.


Reply with a Voice Note (Optional)

To have your bot speak its reply back as a voice note instead of (or alongside) text, synthesize the reply with Sarvamโ€™s text-to-speech API, serve the audio file from your own Flask app, and attach it to the TwiML response as media:

1import uuid
2
3from flask import send_from_directory
4from sarvamai.play import save
5
6AUDIO_DIR = Path("audio_replies")
7AUDIO_DIR.mkdir(exist_ok=True)
8
9
10@app.route("/audio/<filename>")
11def serve_audio(filename):
12 return send_from_directory(AUDIO_DIR, filename, mimetype="audio/wav")
13
14
15# Inside whatsapp_webhook(), replace the final `twiml.message(reply_text)` with:
16message = twiml.message(reply_text)
17
18try:
19 audio = sarvam.text_to_speech.convert(
20 text=reply_text,
21 language_code="en-IN",
22 model="bulbul:v3",
23 speaker="shubh",
24 )
25 filename = f"{uuid.uuid4().hex}.wav"
26 save(audio, AUDIO_DIR / filename)
27 message.media(f"{request.host_url}audio/{filename}")
28except Exception:
29 # If TTS fails, the sender still gets the text reply above.
30 logger.exception("Text-to-speech failed, sending text-only reply")
31
32return Response(str(twiml), mimetype="application/xml")

request.host_url resolves to your current ngrok URL automatically, since ngrok forwards the original Host header, so you donโ€™t need to hardcode it here.

Synthesizing the voice note is wrapped in its own try/except, separate from the outer one, so a TTS failure only drops the audio attachment โ€” the sender still gets the text reply instead of the generic apology.

WhatsApp only renders a native, waveform voice-note bubble for OGG/Opus audio. A .wav reply still plays back fine, just as a regular audio attachment rather than a voice-note bubble.

You can use the same language codes and speaker voices listed above for the phone agent when customizing STT and TTS for your WhatsApp bot.


Understanding the Message Flow

  1. User sends a message. Text or a voice note, to your Twilio WhatsApp number. Twilio POSTs it to your /whatsapp webhook.
  2. STT (Speech-to-Text): If the message is a voice note, your agent downloads it and converts it to text using Sarvamโ€™s Saaras.
  3. LLM: Generates a reply using Sarvam.
  4. TTS (Text-to-Speech, optional): Converts the reply to audio using Sarvamโ€™s Bulbul, if youโ€™ve added voice-note replies.
  5. Response: Your agent returns TwiML, and Twilio delivers the reply, text and/or audio, back to the user on WhatsApp.

WhatsApp Bot: Pro Tips

  • Use language_code="unknown" in STT to auto-detect whichever language the sender types or speaks in.
  • Keep system prompts explicit about reply length (as in the example) โ€” WhatsApp messages are capped at 1600 characters by Twilio, and short replies also read better in a chat window.
  • For production traffic, move off the Sandbox to a verified WhatsApp Sender, and validate Twilioโ€™s request signature on every webhook call.

WhatsAppโ€™s Business Platform only allows free-form replies within 24 hours of the userโ€™s last message. Outside that window, you need a pre-approved template message โ€” this is a WhatsApp policy that neither Sarvam nor Twilio can bypass.

WhatsApp Bot: Troubleshooting

Webhook never gets called. First confirm the sending number actually joined the sandbox (Step 5a) โ€” Twilio silently drops messages from numbers that havenโ€™t opted in, and this is the most common reason nothing reaches your server. Then check that the sandboxโ€™s โ€œWhen a message comes inโ€ URL matches your current ngrok URL exactly, ends in /whatsapp, and uses HTTPS.

Works for a few days, then stops. The sandbox opt-in expires 3 days after joining (or after the last message exchanged). Re-send the join <code> message from that WhatsApp number to reconnect.

No response outside a fresh conversation. WhatsAppโ€™s 24-hour session-messaging window has closed; youโ€™ll need a pre-approved template message to reach the user again.

Voice note transcription fails or comes back empty. Confirm MediaContentType0 starts with audio/, and that youโ€™re passing your Twilio Account SID and Auth Token as Basic Auth when downloading MediaUrl0 (Twilio media URLs return 401 without it).

Voice notes over 30 seconds fail. The synchronous speech-to-text endpoint caps input at 30 seconds. Switch to the Batch STT API for longer voice notes.

Module not found. Re-run the install command from Step 2 above.


Additional Resources


Need Help?


Happy Building!