simple connect Sample

A simple inbound or outbound application that connects an answered inbound or outbound call to a new outbound call.
Some audio (holdmusic.wav) is played to the original call until the new call is answered. When answered it is played a message and then connected to the original call.

This sample requires one Extra Channel. Please configure your service accordingly.
It also requires the wav file holdmusic.wav to be available in the cloud media store.

Uses actions: Connect, Play

    • {
          "actions" :
          [
              {
                  "connect":
                  {
                      "destinations" :
                      [
                          "443069990123"
                      ],
                      "call_from" : "443069999876",
                      "hold_media":
                      {
                          "play" :
                          {
                              "play_list" :
                              [
                                  {
                                      "file_to_play" : "holdmusic.wav"
                                  }
                              ]
                          }
                      },
                      "secondary_call" :
                      {
                          "first_page" :
                          {
                              "url" : "ConnectFirst"
                          }
                      },
                      "next_page" :
                      {
                          "url" : "ConnectNext"
                      }
                  }
              },
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "Sorry, I could not connect your call. Goodbye."
                          }
                      ]
                  }
              }
          ],
          "token" : "my connect instance id",
          "api_version": "2.0"
      }
      
    • {
          "actions" :
          [
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "Connecting you now."
                          }
                      ]
                  }
              }
          ],
          "token" : "my connect instance id",
          "api_version": "2.0"
      }
      
    • {
          "actions" :
          [
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "The outbound call has disconnected. Goodbye."
                          }
                      ]
                  }
              }
          ],
          "token" : "my connect instance id",
          "api_version": "2.0"
      }
      
    • {
          [
          ],
          "token" : "Error for Action: xxxx  ActionIndex: xxxx  Result: xxxx"
      }
      
    • {
      }
      
    • Implementing this sample as an ASP.Net Web application:

      • // CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Simple Connect:
        // A simple inbound application that connects an answered inbound call to a new outbound call.
        // The outbound call made by the application is played a message when that call is 
        // answered. It is then connected to the original inbound call.
        
        using System;
        using System.Collections.Generic;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class SimpleConnect : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Unpack the request
                TelephonyRequest ourRequest = new TelephonyRequest(Request);
        
                // Setup the actions
                List<TelephonyAction> actions = new List<TelephonyAction>();
        
                // Create the connect action
                Connect connectAction = new Connect("443069990123")
                {
                    CallFrom = "443069999876",
                    HoldMedia = Play.PlayFile("holdmusic.wav"),
                    SecondaryCall = new SecondaryCall(new WebPageRequest("ConnectFirst.aspx")),
                    NextPage = new WebPageRequest("ConnectNext.aspx")
                };
                actions.Add(connectAction);
        
                // Add a message to play if the connect doesn't succeed
                Play notConnectedMsgAction = Play.SayText("Sorry, I could not connect your call. Goodbye.");
                actions.Add(notConnectedMsgAction);
        
                // Respond
                TelephonyResponse ourResponse = new TelephonyResponse(actions, "my connect instance id");
                ourResponse.ToHttpResponse(Response);
            }
        }
        
        
      • // CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Simple Connect:
        // ConnectFirst page
        
        using System;
        using System.Collections.Generic;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class ConnectFirst : 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("Connecting you now."));
        
                // Respond
                TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
                ourResponse.ToHttpResponse(Response);
            }
        }
        
        
      • // CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Simple Connect:
        // ConnectNext page
        
        using System;
        using System.Collections.Generic;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class ConnectNext : 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("The outbound call has disconnected. 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 Core Web application:

      • // ASP.NET Core CSharp Wrapper sample for the Aculab Telephony REST API.
        //
        // Simple Connect:
        // A simple inbound application that connects an answered inbound call to a new outbound call.
        // The outbound call made by the application is played a message when that call is 
        // answered. It is then connected to the original inbound call.
        
        using System;
        using System.Net;
        using System.Collections.Generic;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        using System.Threading.Tasks;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            [Route("SimpleConnect")]
            public class SimpleConnectController : ControllerBase
            {
                // 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 async Task<IActionResult> SimpleConnect()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = await TelephonyRequest.UnpackRequestAsync(Request);
        
                        // Setup the actions required
                        List<TelephonyAction> actions = new List<TelephonyAction>();
                        // Create the connect action
                        Connect connectAction = new Connect("443069990123");
                        connectAction.CallFrom = "443069999876";
                        connectAction.HoldMedia = Play.PlayFile("holdmusic.wav");
                        connectAction.SecondaryCall = new SecondaryCall(new WebPageRequest("SimpleConnect/ConnectFirst"));
                        connectAction.NextPage = new WebPageRequest("SimpleConnect/ConnectNext");
                        actions.Add(connectAction);
        
                        // Add a message to play if the connect doesn't succeed
                        Play notConnectedMsgAction = Play.SayText("Sorry, I could not connect your call. Goodbye.");
                        actions.Add(notConnectedMsgAction);
        
                        // Create response
                        TelephonyResponse ourResponse = new TelephonyResponse(actions, "my connect instance id");
                        return new OkObjectResult(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.
        //
        // Simple Connect:
        // A simple inbound application that connects an answered inbound call to a new outbound call.
        // The outbound call made by the application is played a message when that call is 
        // answered. It is then connected to the original inbound call.
        
        using System;
        using System.Net;
        using System.Collections.Generic;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        using System.Threading.Tasks;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            [Route("SimpleConnect")]
            public class SimpleConnectController : ControllerBase
            {
                // Process the GET or POST request, set up the actions and construct the json response.
                [Route("ConnectFirst")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public async Task<IActionResult> ConnectFirst()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = await TelephonyRequest.UnpackRequestAsync(Request);
                        String token = telephonyRequest.InstanceInfo.Token;
        
                        // Setup the actions required
                        List<TelephonyAction> actions = new List<TelephonyAction>();
                        actions.Add(Play.SayText("Connecting you now."));
        
                        // Create response
                        TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
                        return new OkObjectResult(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.
        //
        // Simple Connect:
        // A simple inbound application that connects an answered inbound call to a new outbound call.
        // The outbound call made by the application is played a message when that call is 
        // answered. It is then connected to the original inbound call.
        
        using System;
        using System.Net;
        using System.Collections.Generic;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        using System.Threading.Tasks;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            [Route("SimpleConnect")]
            public class SimpleConnectController : ControllerBase
            {
                // Process the GET or POST request, set up the actions and construct the json response.
                [Route("ConnectNext")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public async Task<IActionResult> ConnectNext()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = await TelephonyRequest.UnpackRequestAsync(Request);
                        String token = telephonyRequest.InstanceInfo.Token;
        
                        // Setup the actions required
                        List<TelephonyAction> actions = new List<TelephonyAction>();
                        actions.Add(Play.SayText("The outbound call has disconnected. Goodbye."));
        
                        // Create response
                        TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
                        return new OkObjectResult(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;
        using System.Threading.Tasks;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            public class RESTSampleController : ControllerBase
            {
                // Process the GET or POST request for the Error condition
                [Route("ErrorPage")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public async Task<IActionResult> ErrorPage()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = await TelephonyRequest.UnpackRequestAsync(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 new OkObjectResult(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;
        using System.Threading.Tasks;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            public class RESTSampleController : ControllerBase
            {
                // Process the GET or POST request for the Final Page
                [Route("FinalPage")]
                [HttpGet]
                [HttpPost]
                [ProducesResponseType(200)]
                [ProducesResponseType(400)]
                [ProducesResponseType(500)]
                public async Task<IActionResult> FinalPage()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = await TelephonyRequest.UnpackRequestAsync(Request);
                        String token = telephonyRequest.InstanceInfo.Token;
        
                        // Create response
                        // Only very limited actions can be returned here
                        TelephonyResponse ourResponse = new TelephonyResponse(null, token);
                        return new OkObjectResult(ourResponse.ToJson(this));
                    }
                    catch (ArgumentException)
                    {
                        return BadRequest();
                    }
                    catch (Exception e)
                    {
                        return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
                    }
                }
            }
        }
        
  • Implemented as ASP.Net Web App:

    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' The first page for the Simple Connect sample:
      ' A simple inbound application that connects an answered inbound call to a new outbound call.
      ' The outbound call made by the application is played a message when that call is 
      ' answered. It is then connected to the original inbound call.
      Imports System.Collections.Generic
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class SimpleConnect
          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)
      
              ' Setup the actions
              Dim actions As List(Of TelephonyAction) = New List(Of TelephonyAction)
      
              ' Create the connect action
              Dim connectAction As Connect = New Connect("443069990123") With {
                  .CallFrom = "443069999876",
                  .HoldMedia = Play.PlayFile("holdmusic.wav"),
                  .SecondaryCall = New SecondaryCall(New WebPageRequest("ConnectFirst.aspx")),
                  .NextPage = New WebPageRequest("ConnectNext.aspx")
              }
              actions.Add(connectAction)
      
              ' Add a message to play if the connect doesn't succeed
              Dim notConnectedMsgAction As Play = Play.SayText("Sorry, I could not connect your call. Goodbye.")
              actions.Add(notConnectedMsgAction)
      
              ' Respond
              Dim ourResponse As TelephonyResponse = New TelephonyResponse(actions, "my connect instance id")
              ourResponse.ToHttpResponse(Response)
          End Sub
      End Class
      
      
    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' A page from the Simple Connect sample:
      ' This is called when the outgoing call is answered. It plays a message
      ' to the outbound call.
      Imports System.Collections.Generic
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class ConnectFirst
          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("Connecting you now."))
      
              ' 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 page from the Simple Connect sample:
      ' This is called when the connection is broken. It plays a message
      ' to the original inbound call.
      Imports System.Collections.Generic
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class ConnectNext
          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("The outbound call has disconnected. 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.
      //
      // Simple Connect:
      // A simple application that connects an outbound call to an answered inbound call. 
      // The outbound call created by the application is played a message when that call 
      // is answered. It is then connected to the original inbound call.
      
      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 SimpleConnect extends HttpServlet
      {
          private static final long serialVersionUID = 3759820747632941625L;
      
          @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);
      
              // Set up the actions
              List<TelephonyAction> actions = new ArrayList<TelephonyAction>();
      
              // Create the connect action
              Connect connectAction = new Connect("443069990123");
              connectAction.setCallFrom("443069999876");
              connectAction.setHoldMedia(Play.playFile("holdmusic.wav"));
              SecondaryCall secondaryCall = new SecondaryCall(new WebPageRequest("ConnectFirst"));
              connectAction.setSecondaryCall(secondaryCall);
              connectAction.setNextPage(new WebPageRequest("ConnectNext"));
              actions.add(connectAction);
      
              // Add a message to play if the connect doesn't succeed
              Play notConnectedMsgAction = Play.sayText("Sorry, I could not connect your call. Goodbye."); 
              actions.add(notConnectedMsgAction);
      
              // Respond
              TelephonyResponse ourResponse = new TelephonyResponse(actions, "my connect instance id");
              ourResponse.setHttpServletResponse(response);
          }
      }
      
      
    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      // Simple Connect Sample
      
      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 ConnectFirst extends HttpServlet
      {
          private static final long serialVersionUID = 6208947052478728738L;
      
          @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("Connecting you now."));
      
              // Respond
              TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
              ourResponse.setHttpServletResponse(response);
          }
      }
      
      
    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      // Simple Connect Sample
      
      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 ConnectNext extends HttpServlet
      {
          private static final long serialVersionUID = 5194315347255670212L;
      
          @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("The outbound call has disconnected. Goodbye."));
      
              // 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('/SimpleConnect', methods=['GET','POST'])
      def handle_SimpleConnect():
      
          my_request = TelephonyRequest(request)
          app_inst_id = my_request.get_application_instance_id()
          print("SimpleConnect: app_inst_id='{}'".format(app_inst_id))
      
          my_connect = Connect()
          # When phoning telephone numbers, this must be a full international phone number, without the +
          my_connect.append_destination('443069990123')
      
          # When phoning telephone numbers, this must be a validated phone number. 
          # Please see the online documentation for details
          my_connect.set_call_from('443069999876')
          my_connect.set_hold_media(Play(file_to_play='holdmusic.wav'))
          my_connect.set_secondary_call(SecondaryCall(first_page=WebPage(url='ConnectFirst'),
                                                      final_page=WebPage(url='FinalPage'),
                                                      error_page=WebPage(url='ErrorPage')))
          my_connect.set_next_page(WebPage('ConnectNext'))
      
          list_of_actions = []
          list_of_actions.append(my_connect)
          list_of_actions.append(Play(text_to_say='Sorry, I could not connect your call. Goodbye.'))
      
          my_response = TelephonyResponse(list_of_actions, token='my connect instance id')
          return my_response.get_json()
      
      
    • @app.route('/ConnectFirst', methods=['GET','POST'])
      def handle_ConnectFirst():
          my_request = TelephonyRequest(request)
          my_token = my_request.get_token()
      
          list_of_actions = []
          list_of_actions.append(Play(text_to_say='Connecting you now.'))
          my_response = TelephonyResponse(list_of_actions, my_token)
          return my_response.get_json()
      
      
    • @app.route('/ConnectNext', methods=['GET','POST'])
      def handle_ConnectNext():
          my_request = TelephonyRequest(request)
          my_token = my_request.get_token()
      
          list_of_actions = []
          list_of_actions.append(Play(text_to_say='The outbound call has disconnected. 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";
      
      use \Aculab\TelephonyRestAPI\Response;
      use \Aculab\TelephonyRestAPI\Play;
      use \Aculab\TelephonyRestAPI\Connect;
      use \Aculab\TelephonyRestAPI\SecondaryCallConfiguration;
      
      $response = new Response();
      $response->setToken('my connect instance id');
      
      // Create the connect action
      $connect = new Connect();
      $connect->addDestination('443069990123');
      $connect->setCallFrom('443069999876');
      $connect->setHoldMedia(Play::playFile('holdmusic.wav'));
      
      $call_config = new SecondaryCallConfiguration();
      $call_config->setFirstPage('ConnectFirst.php');
      $connect->setSecondaryCallConfiguration($call_config);
      
      $connect->setNextPage('ConnectNext.php');
      $response->addAction($connect);
      
      // Add a message to play if the connect doesn't succeed
      $play = new Play();
      $play->addText('Sorry, I could not connect your call. Goodbye.');
      $response->addAction($play);
      
      print $response;
      
      
    • <?php
      header("Content-Type: application/json; charset=UTF-8");
      
      require __DIR__ . "/../../autoload.php";
      
      use \Aculab\TelephonyRestAPI\InstanceInfo;
      use \Aculab\TelephonyRestAPI\Response;
      use \Aculab\TelephonyRestAPI\Play;
      
      $info = InstanceInfo::getInstanceInfo();
      
      $response = new Response();
      $response->setToken($info->getToken());
      
      $play = new Play();
      $play->addText('Connecting you now.');
      $response->addAction($play);
      
      print $response;
      ?>
      
    • <?php
      header("Content-Type: application/json; charset=UTF-8");
      
      require __DIR__ . "/../../autoload.php";
      
      use \Aculab\TelephonyRestAPI\InstanceInfo;
      use \Aculab\TelephonyRestAPI\Response;
      use \Aculab\TelephonyRestAPI\Play;
      
      $info = InstanceInfo::getInstanceInfo();
      
      $response = new Response();
      $response->setToken($info->getToken());
      
      $play = new Play();
      $play->addText('The outbound call has disconnected. 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);