right  Talk To Us!

simple play encrypted Sample

A simple application that plays an encrypted wav file (welcomeE.wav) to an answered inbound or outbound call.

This application requires the wav file welcomeE.wav to be available in the cloud media store.

Uses actions: Play

    • {
          "actions" :
          [
              {
                  "play" :
                  {
                      "play_list" :
                      [
                          {
                              "text_to_say" : "Welcome to the simple play encrypted sample."
                          },
                          {
                              "file_to_play" : "welcomeE.wav",
                              "decryption_cipher" :
                              {
                                  "type" : "aescbc",
                                  "key" : "BAA9386129662B3D80037440712632CA56493C67D956C19658500EC90DBECDD2",
                                  "initialisation_vector": "55B05FF59A28EDAE0F2CDFCEBB99E339"
                              }
                          }
                      ]
                  }
              }
          ],
          "token": "SimplePlayEncrypted - First page handler"
      }
      
    • {
          [
          ],
          "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 Play Encrypted:
        // A simple application that says some text and then plays a wav file to an answered inbound call.
        
        using System;
        using System.Collections.Generic;
        using Aculab.Cloud.RestAPIWrapper;
        
        public partial class SimplePlayEncrypted : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Unpack the request
                TelephonyRequest ourRequest = new TelephonyRequest(Request);
                String callFrom = ourRequest.InstanceInfo.ThisCall.CallFrom;
        
                // Setup the actions
                List<TelephonyAction> actions = new List<TelephonyAction>();
        
                Play playAction = Play.SayText("Welcome to the simple play encrypted sample.");
        
                byte[] key = new byte[] {
                            0xBA, 0xA9, 0x38, 0x61, 0x29, 0x66, 0x2B, 0x3D,
                            0x80, 0x03, 0x74, 0x40, 0x71, 0x26, 0x32, 0xCA,
                            0x56, 0x49, 0x3C, 0x67, 0xD9, 0x56, 0xC1, 0x96,
                            0x58, 0x50, 0x0E, 0xC9, 0x0D, 0xBE, 0xCD, 0xD2 };
                byte[] iv = new byte[] {
                            0x55, 0xB0, 0x5F, 0xF5, 0x9A, 0x28, 0xED, 0xAE,
                            0x0F, 0x2C, 0xDF, 0xCE, 0xBB, 0x99, 0xE3, 0x39 };
                AesCbcCipher cipher = new AesCbcCipher(key, iv);
                playAction.AddFile("welcomeE.wav", cipher);
                actions.Add(playAction);
        
                // Respond
                String token = String.Format("SimplePlayEncrypted - First page handler");
                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 Play:
        // The first page of a simple application that says some text and then plays a wav file to an answered inbound call.
        
        using System;
        using System.Collections.Generic;
        using Microsoft.AspNetCore.Mvc;
        using Aculab.Cloud.RestAPIWrapper;
        using System.Threading.Tasks;
        
        namespace Aculab.Cloud.RESTAPI.NETCoreCSharpSamples.Controllers
        {
            [Route("SimplePlay")]
            public class SimplePlayController : 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> SimplePlay()
                {
                    try
                    {
                        // Unpack the request
                        var telephonyRequest = await TelephonyRequest.UnpackRequestAsync(Request);
                        String callFrom = telephonyRequest.InstanceInfo.ThisCall.CallFrom;
        
                        // Setup the actions required
                        List<TelephonyAction> actions = new List<TelephonyAction>();
                        Play playAction = Play.SayText("Hello, good evening and welcome.");
                        playAction.AddFile("welcome.wav");
                        actions.Add(playAction);
        
                        // Create response
                        String token = String.Format("my instance id");
                        TelephonyResponse ourResponse = new TelephonyResponse(actions, token);
                        return new OkObjectResult(ourResponse.ToJson(this));
                    }
                    catch (ArgumentException)
                    {
                        return BadRequest();
                    }
                    catch (Exception e)
                    {
                        return StatusCode(500, 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 an ASP.Net Web App:

    • ' Visual Basic Wrapper sample for the Aculab Telephony REST API.
      '
      ' The first page for the Simple Play Encrypted sample:
      ' A simple application that says some text and then plays a wav file to an answered inbound call.
      Imports System.Collections.Generic
      Imports Aculab.Cloud.RestAPIWrapper
      
      Partial Class SimplePlay
          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 callFrom As String = ourRequest.InstanceInfo.ThisCall.CallFrom
      
              ' Setup the actions
              Dim actions As List(Of TelephonyAction) = New List(Of TelephonyAction)
      
              Dim playAction As Play = Play.SayText("Welcome to the simple play encrypted sample.")
      
              Dim key As Byte() = {
                          &HBA, &HA9, &H38, &H61, &H29, &H66, &H2B, &H3D,
                          &H80, &H3, &H74, &H40, &H71, &H26, &H32, &HCA,
                          &H56, &H49, &H3C, &H67, &HD9, &H56, &HC1, &H96,
                          &H58, &H50, &HE, &HC9, &HD, &HBE, &HCD, &HD2}
              Dim iv As Byte() = {
                          &H55, &HB0, &H5F, &HF5, &H9A, &H28, &HED, &HAE,
                          &HF, &H2C, &HDF, &HCE, &HBB, &H99, &HE3, &H39}
              Dim Cipher As AesCbcCipher = New AesCbcCipher(key, iv)
              playAction.AddFile("welcomeE.wav", Cipher)
              actions.Add(playAction)
      
              ' Respond
              Dim token As String = String.Format("SimplePlayEncrypted - First page handler")
              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 a Java Servlet:

    • // Java Servlet sample for the Aculab Telephony REST API.
      //
      // Simple Play Encrypted:
      // A simple application that says some text and then plays a wav file to an answered 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 SimplePlayEncrypted extends HttpServlet
      {
          private static final long serialVersionUID = -3906337692740787588L;
      
          @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);
      
              // Setup the actions
              List<TelephonyAction> actions = new ArrayList<TelephonyAction>();
      
              byte[] key = {
                          (byte)0xBA, (byte)0xA9, (byte)0x38, (byte)0x61, (byte)0x29, (byte)0x66, (byte)0x2B, (byte)0x3D,
                          (byte)0x80, (byte)0x03, (byte)0x74, (byte)0x40, (byte)0x71, (byte)0x26, (byte)0x32, (byte)0xCA,
                          (byte)0x56, (byte)0x49, (byte)0x3C, (byte)0x67, (byte)0xD9, (byte)0x56, (byte)0xC1, (byte)0x96,
                          (byte)0x58, (byte)0x50, (byte)0x0E, (byte)0xC9, (byte)0x0D, (byte)0xBE, (byte)0xCD, (byte)0xD2 };
              byte[] iv = {
                          (byte)0x55, (byte)0xB0, (byte)0x5F, (byte)0xF5, (byte)0x9A, (byte)0x28, (byte)0xED, (byte)0xAE,
                          (byte)0x0F, (byte)0x2C, (byte)0xDF, (byte)0xCE, (byte)0xBB, (byte)0x99, (byte)0xE3, (byte)0x39 };
              AesCbcCipher cipher = new AesCbcCipher(key, iv);
      
              Play playAction  = Play.sayText("Welcome to the simple play encrypted sample.");
              playAction.addFile("welcomeE.wav", cipher);
      
              actions.add(playAction);
      
              // Respond
              String token = String.format("SimplePlayEncrypted - First page handler");
              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('/SimplePlayEncrypted', methods=['GET','POST'])
      def handle_SimplePlayEncrypted():
      
          my_request = TelephonyRequest(request)
          app_inst_id = my_request.get_application_instance_id()
          print("SimplePlayEncrypted: app_inst_id='{}'".format(app_inst_id))
      
          # Cipher key is 32 bytes long
          key = "BAA9386129662B3D" + "80037440712632CA" + "56493C67D956C196" + "58500EC90DBECDD2"
      
          # Cipher initialisation vector is 16 bytes long
          initialisation_vector = "55B05FF59A28EDAE" + "0F2CDFCEBB99E339"
      
          cipher = AESCBCCipher(key, initialisation_vector)
      
          play_action = Play(text_to_say='Welcome to the simple play encrypted sample.')
          play_action.append_file('welcomeE.wav', cipher)
          
          list_of_actions = []
          list_of_actions.append(play_action)
          my_response = TelephonyResponse(list_of_actions, token='SimplePlayEncrypted - First page handler')
          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\Play;
      use Aculab\TelephonyRestAPI\Response;
      use Aculab\TelephonyRestAPI\AesCbcCipher;
      
      $response = new Response();
      $response->setToken('SimplePlayEncrypted - First page handler');
      
      $play = new Play();
      $play->addText('Welcome to the simple play encrypted sample.');
      
      $cipher = new AesCbcCipher('BAA9386129662B3D80037440712632CA56493C67D956C19658500EC90DBECDD2', '55B05FF59A28EDAE0F2CDFCEBB99E339');
      $play->addFile('welcomeE.wav', $cipher);
      
      $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);