voice broadcast Sample

An outbound application that handles answered outbound calls that have been classified.

Live speakers are played a notification message and then asked to press a digit to confirm they have received and understood the message. Answering machines are played a different notification message.

Uses actions: Play, Run Menu

    • {
          "actions" :
          [
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "Hello."
                          }
                      ]
                  }
              },
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "This is a recorded notification message to let you know that Saint Custards Preparatory School will open for the spring term on 5th September next year. Please call <say-as interpret-as='digits'>443069990123</say-as> to let us know that you have received this message."
                          }
                      ]
                  }
              }
          ],
          "token" : "my voice broadcast instance id",
          "api_version": "2.0"
      }
      
    • {
          "actions" :
          [
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "Hello."
                          }
                      ]
                  }
              },
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "This is a notification to let you know that Saint Custards Preparatory School will open for the spring term on 5th September next year."
                          }
                      ]
                  }
              },
              {
                  "run_menu" :
                  {
                      "prompt" :
                      {
                          "play" :
                          {
                              "play_list" :
                              [
                                  {
                                      "text_to_say" : "Please press 1 to confirm that you have received and understood this message, or press 2 to hear the message again."
                                  }
                              ]
                          }
                      },
                      "menu_options" :
                      [
                          {
                              "digit" : "1",
                              "next_page" :
                              {
                                  "url" : "ConfirmMessage"
                              }
                          },
                          {
                              "digit" : "2",
                              "next_page" :
                              {
                                  "url" : "VoiceBroadcast"
                              }
                          }
                      ],
                      "on_digit_timeout_messages":
                      [
                          {
                              "play" :
                              {
                                  "play_list" :
                                  [
                                      {
                                          "text_to_say" : "I didn't catch your entry."
                                      }
                                  ]
                              }
                          }
                      ],
                      "on_invalid_digit_messages" :
                      [
                          {
                              "play" :
                              {
                                  "play_list" :
                                  [
                                      {
                                          "text_to_say" : "That wasn't one of the options. Please try again."
                                      }
                                  ]
                              }
                          }
                      ]
                  }
              }
          ],
          "token" : "my voice broadcast instance id",
          "api_version": "2.0"
      }
      
    • {
          "actions" :
          [
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "Thanks for your confirmation. Goodbye."
                          }
                      ]
                  }
              }
          ],
          "token" : "my voice broadcast instance id",
          "api_version": "2.0"
      }
      
    • {
          [
          ],
          "token" : "Error for Action: xxxx  ActionIndex: xxxx  Result: xxxx"
      }
      
    • {
      }
      
    • Implementing this sample as an ASP.Net Web Form application:

      • // CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Voice Broadcast:
        // An application that handles answered outbound calls that have been classified.
        // Live speakers are played a notification message and then asked to press a 
        // digit to confirm they have received and understood the message. Answering 
        // machines are played a different notification message.
        
        using System;
        using System.Collections.Generic;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class VoiceBroadcast : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Unpack the request
                TelephonyRequest ourRequest = new TelephonyRequest(Request);
                String farEndType = ourRequest.InstanceInfo.ThisCall.FarEndType;
        
                // Setup the actions
                List<TelephonyAction> actions = new List<TelephonyAction>
                {
                    Play.SayText("Hello.")
                };
        
                if (farEndType.CompareTo("answering_machine") == 0)
                {
                    // Simply leave a message on the answering machine
                    actions.Add(Play.SayText("This is a recorded notification message to let you know that Saint " +
                                             "Custards Preparatory School will open for the spring term on 5th " + 
                                             "September next year. Please call " +
                                             "<say-as interpret-as='digits'>443069990123</say-as> " +
                                             "to let us know that you have received this message."));
                }
                else
                {
                    // Play the notification message
                    actions.Add(Play.SayText("This is a notification to let you know that Saint Custards Preparatory " + 
                                             "School will open for the spring term on 5th September next year."));
                    
                    // Set up a run menu action to obtain an acknowledgement or repeat the notification message
                    Play prompt = Play.SayText("Please press 1 to confirm that you have received and understood " +
                                               "this message, or press 2 to hear the message again.");
                    List<MenuOption> menuOptions = new List<MenuOption>
                    {
                        new MenuOption('1', new WebPageRequest("ConfirmMessage.aspx")),
                        new MenuOption('2', new WebPageRequest("VoiceBroadcast.aspx"))
                    };
                    RunMenu runMenuAction = new RunMenu(menuOptions, prompt);
                    runMenuAction.OnDigitTimeoutMessages = new List<Play>
                    {
                        Play.SayText("I didn't catch your entry.")
                    };
                    runMenuAction.OnInvalidDigitMessages = new List<Play>
                    {
                        Play.SayText("That wasn't one of the options. Please try again.")
                    };
                    actions.Add(runMenuAction);        
                }
        
                // Respond
                TelephonyResponse ourResponse = new TelephonyResponse(actions, "my voice broadcast instance id");
                ourResponse.ToHttpResponse(Response);
            }
        }
        
        
      • // CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Voice Broadcast:
        // ConfirmMessage page
        
        using System;
        using System.Collections.Generic;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class ConfirmMessage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Unpack the request
                TelephonyRequest ourRequest = new TelephonyRequest(Request);
                String token = ourRequest.InstanceInfo.Token;
        
                // Setup the actions
                List<TelephonyAction> actions = new List<TelephonyAction>();
                actions.Add(Play.SayText("Thanks for your confirmation. Goodbye."));
        
                // Respond
                TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
                ourResponse.ToHttpResponse(Response);
            }
        }
        
        
      • using System;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class ErrorPage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Unpack the request
                TelephonyRequest ourRequest = new TelephonyRequest(Request);
                ErrorResult result = ourRequest.InstanceInfo.ErrorResult;
        
                String token = String.Format("Action: {0}\nActionIndex: {1}\nResult: {2}",
                    result.Action, result.ActionIndex, result.Result);
        
                // Respond
                TelephonyResponse ourResponse = new TelephonyResponse(null, token);
                ourResponse.ToHttpResponse(Response);
            }
        }
        
        
      • using System;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class FinalPage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Unpack the request
                TelephonyRequest ourRequest = new TelephonyRequest(Request);
            }
        }
        
        
    • Implementing this sample as an ASP.Net Mvc application:

      • // ASP.NET Core CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Voice Broadcast:
        // An application that handles answered outbound calls that have been classified.
        // Live speakers are played a notification message and then asked to press a 
        // digit to confirm they have received and understood the message. Answering 
        // machines are played a different notification message.
        
        using System;
        using System.Net;
        using System.Collections.Generic;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            [Route("VoiceBroadcast")]
            public class VoiceBroadcastController : Controller
            {
                // Process the GET or POST request, set up the actions and construct the json response.
                [Route("FirstPage")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public IActionResult VoiceBroadcast()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = new TelephonyRequest(Request);
                        String farEndType = telephonyRequest.InstanceInfo.ThisCall.FarEndType;
        
                        // Setup the actions required
                        List<TelephonyAction> actions = new List<TelephonyAction>();
                        actions.Add(Play.SayText("Hello."));
        
                        if (farEndType.CompareTo("answering_machine") == 0)
                        {
                            // Simply leave a message on the answering machine
                            actions.Add(Play.SayText("This is a recorded notification message to let you know that Saint " +
                                                     "Custards Preparatory School will open for the spring term on 5th " +
                                                     "September next year. Please call " +
                                                     "<say-as interpret-as='digits'>443069990123</say-as> " +
                                                     "to let us know that you have received this message."));
                        }
                        else
                        {
                            // Play the notification message
                            actions.Add(Play.SayText("This is a notification to let you know that Saint Custards Preparatory " +
                                                     "School will open for the spring term on 5th September next year."));
        
                            // Set up a run menu action to obtain an acknowledgement or repeat the notification message
                            Play prompt = Play.SayText("Please press 1 to confirm that you have received and understood " +
                                                       "this message, or press 2 to hear the message again.");
                            List<MenuOption> menuOptions = new List<MenuOption>()
                            {
                                new MenuOption('1', new WebPageRequest("VoiceBroadcast/ConfirmMessage")),
                                new MenuOption('2', new WebPageRequest("VoiceBroadcast/FirstPage"))
                            };
                            RunMenu runMenuAction = new RunMenu(menuOptions, prompt);
                            runMenuAction.OnDigitTimeoutMessages = new List<Play>()
                            {
                                Play.SayText("I didn't catch your entry.")
                            };
                            runMenuAction.OnInvalidDigitMessages = new List<Play>()
                            {
                                Play.SayText("That wasn't one of the options. " +
                                             "Please try again.")
                            };
                            actions.Add(runMenuAction);
                        }
        
                        // Create response
                        TelephonyResponse ourResponse = new TelephonyResponse(actions, "my voice broadcast instance id");
                        return ourResponse.ToJson(this);
                    }
                    catch (ArgumentException)
                    {
                        return BadRequest();
                    }
                    catch (Exception e)
                    {
                        return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
                    }
                }
        
                // Process the GET or POST request, set up the actions and construct the json response.
                [Route("ConfirmMessage")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public IActionResult ConfirmMessage()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = new TelephonyRequest(Request);
                        String token = telephonyRequest.InstanceInfo.Token;
        
                        // Setup the actions required
                        List<TelephonyAction> actions = new List<TelephonyAction>()
                        {
                            Play.SayText("Thanks for your confirmation. Goodbye.")
                        };
        
                        // Create response
                        TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
                        return ourResponse.ToJson(this);
                    }
                    catch (ArgumentException)
                    {
                        return BadRequest();
                    }
                    catch (Exception e)
                    {
                        return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
                    }
                }
            }
        }
        
      • // ASP.NET Core CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Voice Broadcast:
        // An application that handles answered outbound calls that have been classified.
        // Live speakers are played a notification message and then asked to press a 
        // digit to confirm they have received and understood the message. Answering 
        // machines are played a different notification message.
        
        using System;
        using System.Net;
        using System.Collections.Generic;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        }
        
      • // ASP.NET Core CSharp Wrapper sample for the Aculab Telephony REST API.
        
        using System;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        using System.Net;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            public class RESTSampleController : Controller
            {
                // Process the GET or POST request for the Error condition
                [Route("ErrorPage")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public IActionResult ErrorPage()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = new TelephonyRequest(Request);
                        ErrorResult result = telephonyRequest.InstanceInfo.ErrorResult;
        
                        String token = String.Format("Action: {0}\nActionIndex: {1}\nResult: {2}",
                            result.Action, result.ActionIndex, result.Result);
        
                        // Create response
                        TelephonyResponse ourResponse = new TelephonyResponse(null, token);
                        return ourResponse.ToJson(this);
                    }
                    catch (ArgumentException)
                    {
                        return BadRequest();
                    }
                    catch (Exception e)
                    {
                        return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
                    }
                }
            }
        }
        
      • // ASP.NET Core CSharp Wrapper sample for the Aculab Telephony REST API.
        
        using System;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        using System.Net;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            public class RESTSampleController : Controller
            {
                // Process the GET or POST request for the Final Page
                [Route("FinalPage")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public IActionResult FinalPage()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = new TelephonyRequest(Request);
                        String token = telephonyRequest.InstanceInfo.Token;
        
                        // Create response
                        // Only very limited actions can be returned here
                        TelephonyResponse ourResponse = new TelephonyResponse(null, token);
                        return ourResponse.ToJson(this);
                    }
                    catch (ArgumentException)
                    {
                        return BadRequest();
                    }
                    catch (Exception e)
                    {
                        return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
                    }
                }
            }
        }
        
  • Implemented as ASP.Net Web Forms:

    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' The first page for the Voice Broadcast sample:
      ' An application that handles answered outbound calls that have been classified.
      ' Live speakers are played a notification message and then asked to press a 
      ' digit to confirm they have received and understood the message. Answering 
      ' machines are played a different notification message.
      Imports System.Collections.Generic
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class VoiceBroadcast
          Inherits System.Web.UI.Page
      
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      
              ' Unpack the request
              Dim ourRequest As TelephonyRequest = New TelephonyRequest(Request)
      
              Dim farEndType As String = ourRequest.InstanceInfo.ThisCall.FarEndType
      
              ' Setup the actions
              Dim actions As List(Of TelephonyAction) = New List(Of TelephonyAction)
      
              actions.Add(Play.SayText("Hello."))
      
              If farEndType.CompareTo("answering_machine") = 0 Then
                  ' Simply leave a message on the answering machine
                  actions.Add(Play.SayText("This is a recorded notification message to let you know that Saint " +
                                           "Custards Preparatory School will open for the spring term on 5th " +
                                           "September next year. Please call " +
                                           "<say-as interpret-as='digits'>443069990123</say-as> " +
                                           "to let us know that you have received this message."))
              Else
                  ' Play the notification message
                  actions.Add(Play.SayText("This is a notification to let you know that Saint Custards Preparatory " + _
                                           "School will open for the spring term on 5th September next year."))
      
                  ' Set up a run menu action to obtain an acknowledgement or repeat the notification message
                  Dim prompt As Play = Play.SayText("Please press 1 to confirm that you have received and understood " +
                                             "this message, or press 2 to hear the message again.")
                  Dim menuOptions As List(Of MenuOption) = New List(Of MenuOption)
                  menuOptions.Add(New MenuOption("1", New WebPageRequest("ConfirmMessage.aspx")))
                  menuOptions.Add(New MenuOption("2", New WebPageRequest("VoiceBroadcast.aspx")))
                  Dim runMenuAction As RunMenu = New RunMenu(menuOptions, prompt)
                  runMenuAction.OnDigitTimeoutMessages = New List(Of Play) From {
                      Play.SayText("I didn't catch your entry.")
                  }
                  runMenuAction.OnInvalidDigitMessages = New List(Of Play) From {
                      Play.SayText("That wasn't one of the options. Please try again.")
                  }
                  actions.Add(runMenuAction)
              End If
      
              ' Respond
              Dim ourResponse As TelephonyResponse = New TelephonyResponse(actions, "my voice broadcast instance id")
              ourResponse.ToHttpResponse(Response)
          End Sub
      End Class
      
      
    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' A page from the Voice Broadcast sample:
      ' This plays a confirmation message.
      Imports System.Collections.Generic
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class ConfirmMessage
          Inherits System.Web.UI.Page
      
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      
              ' Unpack the request
              Dim ourRequest As TelephonyRequest = New TelephonyRequest(Request)
              Dim token As String = ourRequest.InstanceInfo.Token
      
              ' Setup the actions
              Dim actions As List(Of TelephonyAction) = New List(Of TelephonyAction)
              actions.Add(Play.SayText("Thanks for your confirmation. Goodbye."))
      
              ' Respond
              Dim ourResponse As TelephonyResponse = New TelephonyResponse(actions, token)
              ourResponse.ToHttpResponse(Response)
          End Sub
      End Class
      
      
    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' A generic error page for all the samples.
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class ErrorPage
          Inherits System.Web.UI.Page
      
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      
              ' Unpack the request
              Dim ourRequest As TelephonyRequest = New TelephonyRequest(Request)
              Dim result As ErrorResult = ourRequest.InstanceInfo.ErrorResult
      
              Dim token As String = String.Format("Action: {0}\nActionIndex: {1}\nResult: {2}", _
                  result.Action, result.ActionIndex, result.Result)
      
              ' Respond
              Dim ourResponse As TelephonyResponse = New TelephonyResponse(token)
              ourResponse.ToHttpResponse(Response)
          End Sub
      End Class
      
      
    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' A generic final page for all the samples:
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class FinalPage
          Inherits System.Web.UI.Page
      
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      
              ' Unpack the request
              Dim ourRequest As TelephonyRequest = New TelephonyRequest(Request)
      
              ' Do application tidying up
              ' ...
          End Sub
      End Class
      
      
  • Implemented as Java Servlets:

    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      // Voice Broadcast:
      // An application that handles answered outbound calls that have been classified.
      // Live speakers are played a notification message and then asked to press a 
      // digit to confirm they have received and understood the message. Answering 
      // machines are played a different notification message.
      
      package com.aculab.telephonyrestapi.samples;
      
      import javax.servlet.http.*;
      import javax.servlet.ServletException;
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.List;
      
      import com.aculab.telephonyrestapi.*;
      
      public class VoiceBroadcast extends HttpServlet
      {
          private static final long serialVersionUID = -3689455870958870956L;
      
          @Override
          public void doGet(HttpServletRequest request,
                            HttpServletResponse response)
              throws IOException, ServletException
          {
             	handleRequest(request, response);
          }
          
          @Override
          public void doPost(HttpServletRequest request,
                  HttpServletResponse response)
                          throws IOException, ServletException
          {
          	handleRequest(request, response);
          }
          
          private void handleRequest(HttpServletRequest request,
                                     HttpServletResponse response) throws IOException
          {
              // Unpack the request
              TelephonyRequest ourRequest = new TelephonyRequest(request);
      
              String farEndType = ourRequest.getInstanceInfo().getThisCall().getFarEndType();
      
              // Set up the actions
              List<TelephonyAction> actions = new ArrayList<TelephonyAction>();
      
              actions.add(Play.sayText("Hello."));
      
              if (farEndType.compareTo("answering_machine") == 0)
              {
                  // Simply leave a message on the answering machine
                  actions.add(Play.sayText("This is a recorded notification message to let you know that Saint " +
                                           "Custards Preparatory School will open for the spring term on 5th " + 
                                           "September next year. Please call " +
                                           "<say-as interpret-as='digits'>443069990123</say-as> " +
                                           "to let us know that you have received this message."));
              }
              else
              {
                  // Play the notification message
                  actions.add(Play.sayText("This is a notification to let you know that Saint Custards Preparatory " + 
                                           "School will open for the spring term on 5th September next year."));
                  
                  // Set up a run menu action to obtain an acknowledgement or repeat the notification message
                  Play prompt = Play.sayText("Please press 1 to confirm that you have received and understood " +
                                             "this message, or press 2 to hear the message again.");
                  List<MenuOption> menuOptions = new ArrayList<MenuOption>();
                  menuOptions.add(new MenuOption('1', new WebPageRequest("ConfirmMessage")));
                  menuOptions.add(new MenuOption('2', new WebPageRequest("VoiceBroadcast")));
                  RunMenu runMenuAction = new RunMenu(prompt, menuOptions);
                  List<Play> timeoutMsgs = new ArrayList<Play>();
                  timeoutMsgs.add(Play.sayText("I didn't catch your entry.", "Paul"));
                  runMenuAction.setOnDigitTimeoutMessages(timeoutMsgs);
                  List<Play> invalidMsgs = new ArrayList<Play>();
                  invalidMsgs.add(Play.sayText("That wasn't one of the options. Please try again.", "Paul"));
                  runMenuAction.setOnInvalidDigitMessages(invalidMsgs);
      
                  actions.add(runMenuAction);        
              }
      
              // Respond
              TelephonyResponse ourResponse = new TelephonyResponse(actions, "my voice broadcast instance id");
              ourResponse.setHttpServletResponse(response);
          }
      }
      
      
    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      
      package com.aculab.telephonyrestapi.samples;
      
      import javax.servlet.http.*;
      import javax.servlet.ServletException;
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.List;
      import com.aculab.telephonyrestapi.*;
      
      public class ConfirmMessage extends HttpServlet
      {
      	private static final long serialVersionUID = 1052633605054507967L;
      
          @Override
          public void doGet(HttpServletRequest request,
                            HttpServletResponse response)
              throws IOException, ServletException
          {
             	handleRequest(request, response);
          }
          
          @Override
          public void doPost(HttpServletRequest request,
                  HttpServletResponse response)
                          throws IOException, ServletException
          {
          	handleRequest(request, response);
          }
          
          private void handleRequest(HttpServletRequest request,
                                     HttpServletResponse response) throws IOException
          {
              // Unpack the request
              TelephonyRequest ourRequest = new TelephonyRequest(request);
      
              String token = ourRequest.getInstanceInfo().getToken();
      
              // Set up the actions
              List<TelephonyAction> actions = new ArrayList<TelephonyAction>();
              actions.add(Play.sayText("Thanks for your confirmation. Goodbye.", "Paul"));
      
              // Respond
              TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
              ourResponse.setHttpServletResponse(response);
          }
      }
      
      
    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      
      package com.aculab.telephonyrestapi.samples;
      
      import javax.servlet.http.*;
      import javax.servlet.ServletException;
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.List;
      
      import com.aculab.telephonyrestapi.*;
      
      public class ErrorPage extends HttpServlet
      {
          private static final long serialVersionUID = -4842873371047361437L;
      
          @Override
          public void doGet(HttpServletRequest request,
                            HttpServletResponse response)
              throws IOException, ServletException
          {
             	handleRequest(request, response);
          }
          
          @Override
          public void doPost(HttpServletRequest request,
                  HttpServletResponse response)
                          throws IOException, ServletException
          {
          	handleRequest(request, response);
          }
          
          private void handleRequest(HttpServletRequest request,
                                     HttpServletResponse response) throws IOException
          {
              // Unpack the request
              TelephonyRequest ourRequest = new TelephonyRequest(request);
      
              ErrorResult result = ourRequest.getInstanceInfo().getErrorResult();
      
              String token = String.format("Action: %s\nActionIndex: %d\nResult: %s", result.getAction(), result.getActionIndex(), result.getResult());
              
              // Respond
              List<TelephonyAction> actions = new ArrayList<TelephonyAction>();
              TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
              ourResponse.setHttpServletResponse(response);
          }
      }
      
      
    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      
      package com.aculab.telephonyrestapi.samples;
      
      import javax.servlet.http.*;
      import javax.servlet.ServletException;
      import java.io.IOException;
      import com.aculab.telephonyrestapi.*;
      
      public class FinalPage extends HttpServlet
      {
          private static final long serialVersionUID = 5940620014313056844L;
      
          @Override
          public void doGet(HttpServletRequest request,
                            HttpServletResponse response)
              throws IOException, ServletException
          {
             	handleRequest(request, response);
          }
          
          @Override
          public void doPost(HttpServletRequest request,
                  HttpServletResponse response)
                          throws IOException, ServletException
          {
          	handleRequest(request, response);
          }
          
          private void handleRequest(HttpServletRequest request,
                                     HttpServletResponse response) throws IOException
          {
              // Unpack the request
              TelephonyRequest ourRequest = new TelephonyRequest(request);
          }
      }
      
      
  • Implemented as a Flask web application:

    • @app.route('/VoiceBroadcast', methods=['GET','POST'])
      def handle_VoiceBroadcast():
      
          my_request = TelephonyRequest(request)
          app_inst_id = my_request.get_application_instance_id()
          print("VoiceBroadcast: app_inst_id='{}'".format(app_inst_id))
          
          # get the call classification (machine / non-machine)
          this_call = my_request.get_this_call()
          
          list_of_actions = []
      
          list_of_actions.append(Play(text_to_say="Hello."))
      
          # the far end type will only be available if classify is enabled
          far_end_type = this_call.get('far_end_type', 'unknown')
      
          if far_end_type.find('answering_machine') != -1:
              play_action = Play(text_to_say=("This is a recorded notification message to let you know that Saint "
                                              "Custards Preparatory School will open for the spring term on 5th "
                                              "September next year. Please call "
                                              "<say-as interpret-as='digits'>443069990123</say-as> "
                                              "to let us know that you have received this message."))
              list_of_actions.append(play_action)
          else:
              play_action = Play(text_to_say=("This is a notification to let you know that Saint Custards Preparatory "
                                              "School will open for the spring term on 5th September next year."))
              list_of_actions.append(play_action)
      
              menu_options = []
              menu_options.append(MenuOption('1',WebPage(url='ConfirmMessage')))
              menu_options.append(MenuOption('2',WebPage(url='VoiceBroadcast')))
      
              # set up a run menu action to obtain an acknowledgement or repeat the notification message
              my_menu = RunMenu(menu_options)
              my_menu.set_prompt(Play(text_to_say=("Please press 1 to confirm that you have received and understood "
                                                       "this message, or press 2 to hear the message again.")))
              my_menu.set_on_digit_timeout_messages([Play(text_to_say="I didn't catch your entry.")])
              my_menu.set_on_invalid_digit_messages([Play(text_to_say="That wasn't one of the options. Please try again.")])
              list_of_actions.append(my_menu)
      
          my_response = TelephonyResponse(list_of_actions, token='my voice broadcast instance id')
          return my_response.get_json()
      
      
    • @app.route('/ConfirmMessage', methods=['GET','POST'])
      def handle_ConfirmMessage():
      
          my_request = TelephonyRequest(request)
          my_token = my_request.get_token()
      
          list_of_actions = []
          list_of_actions.append(Play(text_to_say="Thanks for your confirmation. Goodbye."))
          my_response = TelephonyResponse(list_of_actions, my_token)
          return my_response.get_json()
      
    • @app.route('/ErrorPage', methods=['GET','POST'])
      def handle_ErrorPage():
          
          my_request = TelephonyRequest(request)
          token = my_request.get_token()
          app_inst_id = my_request.get_application_instance_id()
          error_result_dict = my_request.get_error_result()
          action_string = error_result_dict.get('action', "?")
          result_string = error_result_dict.get('result', "?")
          print("ErrorPage: app_inst_id='{}' token='{}' action='{}' result='{}'".format(app_inst_id, token, action_string, result_string))
          
          my_response = TelephonyResponse([Play(text_to_say='An error has occurred')])
          return my_response.get_json()
      
      
    • @app.route('/FinalPage', methods=['GET','POST'])
      def handle_FinalPage():
          # The FinalPage handler follows the guidelines on:
          # https://www.aculab.com/cloud/voice-and-fax-apis/rest-api/rest-api-version-2/rest-api-version-2/writing-a-rest-application
          # The guidelines are:
          #   "Your final page should return an empty response, a 204 is preferable, but empty JSON is acceptable."
          my_request = TelephonyRequest(request)
          token = my_request.get_token()
          app_inst_id = my_request.get_application_instance_id()
          print("FinalPage: app_inst_id='{}' token='{}'".format(app_inst_id, token))
          empty_json = '{}'.encode('utf-8')
          return empty_json
          
      
    • <?php
      header("Content-Type: application/json; charset=UTF-8");
      
      require __DIR__ . "/../../autoload.php";
      
      // set headers to prevent the page being cached by intermediaries
      header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
      header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
      
      header("Content-Type: application/json; charset=UTF-8");
      
      use \Aculab\TelephonyRestAPI\InstanceInfo;
      use \Aculab\TelephonyRestAPI\Response;
      use \Aculab\TelephonyRestAPI\Play;
      use \Aculab\TelephonyRestAPI\RunMenu;
      use \Aculab\TelephonyRestAPI\MessageList;
      
      $info = InstanceInfo::getInstanceInfo();
      $endType = '';
      $response = new Response();
      if ($info != null) {
          $response->setToken($info->getToken());
      
          $callInfo = $info->getThisCallInfo();
          if ($callInfo != null) {
              $endType = $callInfo->getFarEndType();
          }
      }
      
      $response->addAction(Play::sayText("Hello."));
      
      if ($endType == 'human') {
          $response->addAction(
              Play::sayText(
                  "This is a notification to let you know that Saint Custards Preparatory " .
                  "School will open for the spring term on 5th September next year."
              )
          );
          // Create the menu action
          $menuPrompt = Play::sayText(
              "Please press 1 to confirm that you have " .
              "received and understood this message, " . 
              "or press 2 to hear the message again."
          );
          $menu = new RunMenu();
          $menu->setPrompt($menuPrompt);
          $menu->addMenuOption('1', 'ConfirmMessage.php');
          $menu->addMenuOption('2', 'First.php');
      
          // Set up some new info messages for digit timeout and invalid digit
          $onDigitTimeoutMessages = new MessageList();
          $onDigitTimeoutMessages->addMessage(
              Play::sayText(
                  "I didn't catch your entry."
              )
          );
          $menu->setOnDigitTimeoutMessages($onDigitTimeoutMessages);
      
          $onInvalidDigitMessages = new MessageList();
          $onInvalidDigitMessages->addMessage(
              Play::sayText(
                  "That wasn't one of the options. Please try again."
              )
          );
          $menu->setOnInvalidDigitMessages($onInvalidDigitMessages);
      
          $response->addAction($menu);
      } elseif ($endType == 'answering_machine') {
          $response->addAction(
              Play::sayText(
                  "This is a recorded notification message to let you know that Saint " .
                  "Custards Preparatory School will open for the spring term on 5th " .
                  "September next year. Please call " .
                  "<say-as interpret-as='digits'>443069990123</say-as> " .
                  "to let us know that you have received this message."
              )
          );
      } // else no actions, so the call will be disconnected
      print $response;
      
      
    • <?php
      header("Content-Type: application/json; charset=UTF-8");
      
      require __DIR__ . "/../../autoload.php";
      
      use \Aculab\TelephonyRestAPI\Play;
      use \Aculab\TelephonyRestAPI\Response;
      use \Aculab\TelephonyRestAPI\InstanceInfo;
      
      $info = InstanceInfo::getInstanceInfo();
      
      $response = new Response();
      $response->setToken($info->getToken());
      
      $play = new Play();
      $play->addText('Thanks for your confirmation. Goodbye.');
      $response->addAction($play);
      
      print $response;
      
      
    • <?php
      header("Content-Type: application/json; charset=UTF-8");
      
      require __DIR__ . "/../../autoload.php";
      
      use Aculab\TelephonyRestAPI\Play;
      use Aculab\TelephonyRestAPI\Response;
      use Aculab\TelephonyRestAPI\InstanceInfo;
      
      $info = InstanceInfo::getInstanceInfo();
      
      $error = $info->getErrorResult();
      $action = $error->getAction();
      $desc = $error->getResult();
      if (!is_null($action)) {
          error_log("Error from action \"$action\" with result:" . PHP_EOL . "$desc" . PHP_EOL);
      } else {
          error_log("Error result:" . PHP_EOL . "$desc" . PHP_EOL);
      }
      
      $response = new Response();
      $response->setToken('Error');
      
      $play = new Play();
      $play->addText('An error has occurred.');
      $response->addAction($play);
      
      print $response;
      
      
    • <?php
      require __DIR__ . "/../../autoload.php";
      
      http_response_code(204);
      header("Content-Type: application/json; charset=UTF-8");
      
      use Aculab\TelephonyRestAPI\InstanceInfo;
      
      $info = InstanceInfo::getInstanceInfo();
      $call = $info->getThisCallInfo();
      $callid = $call->getCallId();
      $duration = $call->getSecondsCallDuration();
      error_log("This call id: $callid" . PHP_EOL . "This call duration: $duration" . PHP_EOL);