Web Responder Examples
Complete Web Responder applications you can copy and adapt.
These examples show common Web Responder patterns. The server-side examples use PHP, but any language works: your application only needs to accept an HTTP request and return XML with Content-Type: text/xml.
Each example assumes Level365 has already connected the Web Responder to your phone system. See Requesting setup.
Hello world
The smallest useful Web Responder: greet the caller, then end the call.
<?php
header("Content-Type: text/xml");
?>
<Response>
<Say>Hello, and thanks for calling. Goodbye.</Say>
<Hangup/>
</Response>Add two numbers
A multi-step application. It asks for two single-digit numbers and says their sum, carrying the first number to the next step in the action URL.
<?php
header("Content-Type: text/xml");
$step = $_REQUEST["step"] ?? "start";
if ($step === "start") {
// First request: ask for the first number
echo "<Gather numDigits='1' action='add.php?step=second'>
<Say>Welcome to the math line. Enter the first number.</Say>
</Gather>";
} elseif ($step === "second") {
// Keep the first number in the next action URL
$first = (int) ($_REQUEST["Digits"] ?? 0);
echo "<Gather numDigits='1' action='add.php?step=result&first=$first'>
<Say>Enter the second number.</Say>
</Gather>";
} else {
$first = (int) ($_REQUEST["first"] ?? 0);
$second = (int) ($_REQUEST["Digits"] ?? 0);
$sum = $first + $second;
echo "<Response>
<Say>$first plus $second is $sum. Goodbye.</Say>
<Hangup/>
</Response>";
}Note the & in the action URL: an & inside an XML attribute has to be escaped.
Menu with keypad or speech
A menu that callers can answer by pressing a key or by speaking. The answer is routed with <Forward>.
<?php
header("Content-Type: text/xml");
$digits = $_REQUEST["Digits"] ?? "";
$speech = strtolower($_REQUEST["SpeechResult"] ?? "");
if ($digits === "" && $speech === "") {
// No answer yet: play the menu
echo "<Gather input='dtmf speech' numDigits='1' hints='sales, support' action='menu.php'>
<Say>For sales, press 1 or say sales. For support, press 2 or say support.</Say>
</Gather>";
} elseif ($digits === "1" || str_contains($speech, "sales")) {
echo "<Forward>2001</Forward>"; // Sales queue
} elseif ($digits === "2" || str_contains($speech, "support")) {
echo "<Forward>2002</Forward>"; // Support queue
} else {
echo "<Response>
<Say>Sorry, I didn't catch that.</Say>
<Gather input='dtmf speech' numDigits='1' hints='sales, support' action='menu.php'>
<Say>For sales, press 1 or say sales. For support, press 2 or say support.</Say>
</Gather>
</Response>";
}Replace 2001 and 2002 with the extensions or queues to route to.
Route by caller
Look up the caller and route them to their account manager. NmsAni is the caller's number.
<?php
header("Content-Type: text/xml");
$caller = preg_replace("/\D/", "", $_REQUEST["NmsAni"] ?? "");
// Replace with a lookup in your CRM or database
$accountManagers = [
"13175551234" => "1005",
];
if (isset($accountManagers[$caller])) {
echo "<Response>
<Say>Connecting you to your account manager.</Say>
<Forward>{$accountManagers[$caller]}</Forward>
</Response>";
} else {
echo "<Forward>2000</Forward>"; // Main line
}Speak another language
Ask a question in Brazilian Portuguese and listen for a name. hints improves accuracy for names you expect.
<Gather input="speech" language="pt-BR" hints="Thiago, Vicente" action="directory.php">
<Say voice="male" language="pt-BR">Olá, com quem você gostaria de falar?</Say>
</Gather>Microphone test
Let callers test their audio by hearing themselves for 15 seconds.
<Response>
<Say>After the pause, talk and you'll hear yourself for fifteen seconds.</Say>
<Wait timeout="2"/>
<Echo timeout="15"/>
<Say>Test complete. Goodbye.</Say>
<Hangup/>
</Response>Reject unwanted calls
Refuse calls from blocked numbers before they're answered.
<?php
header("Content-Type: text/xml");
$caller = preg_replace("/\D/", "", $_REQUEST["NmsAni"] ?? "");
$blocked = ["13175551234"];
if (in_array($caller, $blocked, true)) {
echo "<Response><Reject reason='rejected'/></Response>";
} else {
echo "<Forward>2000</Forward>";
}Outbound reminder call
Call someone with the API, then let the Web Responder read them a message. First, start the call. call-term-user is the system user that routes to your Web Responder:
curl -X POST https://api.365sip.com/ns-api/v2/domains/yourdomain/users/8080/calls \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"synchronous": "yes",
"call-id": "reminder-000123",
"call-orig-user": "13175551234",
"call-term-user": "8080"
}'When the person answers, your Web Responder runs:
<?php
header("Content-Type: text/xml");
?>
<Response>
<Say>This is a reminder of your appointment tomorrow at 10 AM.</Say>
<Gather numDigits="1" action="reminder-confirm.php">
<Say>Press 1 to confirm, or 2 to speak with someone.</Say>
</Gather>
</Response>Receive a media stream
A minimal Node.js WebSocket server that logs the messages from a media stream. It uses the ws package (npm install ws).
import { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
wss.on("connection", (socket) => {
socket.on("message", (data) => {
const msg = JSON.parse(data);
switch (msg.event) {
case "connected":
console.log("Stream connected, protocol", msg.version);
break;
case "start":
console.log("Stream started for call", msg.start.call_id, msg.start.customParameters);
break;
case "media": {
const audio = Buffer.from(msg.media.payload, "base64"); // format is in start.mediaFormat
// Send `audio` to your transcription or analysis service here
break;
}
case "stop":
console.log("Stream stopped", msg.stop.tracks);
break;
}
});
});Point a <Stream> at it from your Web Responder:
<Response>
<Start>
<Stream name="transcribe" url="wss://stream.example.com/audio" track="inbound">
<Parameter name="customerID" value="12345"/>
</Stream>
</Start>
<Say>This call may be transcribed for quality purposes.</Say>
<Gather input="dtmf speech" numDigits="1" hints="sales, support" action="menu.php">
<Say>For sales, press 1 or say sales. For support, press 2 or say support.</Say>
</Gather>
</Response>Updated 11 days ago
