Skip to content

DX-2467 added modifyCallBxml function #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Voice/Bxml/Bxml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Bxml.php
*
* Class that represents the BXML response. Is built by adding verbs to it
*
* * @copyright Bandwidth INC
*/

namespace BandwidthLib\Voice\Bxml;

use DOMDocument;

class Bxml {

/**
* Creates the Bxml class with an empty list of verbs
*/
public function __construct() {
$this->verbs = array();
}

/**
* Adds the verb to the verbs
*
* @param Verb $verb The verb to add to the list
*/
public function addVerb($verb) {
array_push($this->verbs, $verb);
}

/**
* Converts the Response class into its BXML representation
*
* @return string The xml representation of the class
*/
public function toBxml() {
$ssmlRegex = '/&lt;([a-zA-Z\/\/].*?)&gt;/';
$doc = new DOMDocument('1.0', 'UTF-8');
$bxmlElement = $doc->createElement("Bxml");

foreach ($this->verbs as $verb) {
$bxmlElement->appendChild($verb->toBxml($doc));
}

$doc->appendChild($bxmlElement);
return str_replace("\n", '', preg_replace($ssmlRegex, "<$1>", $doc->saveXML()));
}
}
108 changes: 108 additions & 0 deletions src/Voice/Controllers/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,114 @@ public function getCall(
return new ApiResponse($response->code, $response->headers, $deserializedResponse);
}

/**
* Replaces the bxml for an active call
*
* @param string $accountId TODO: type description here
* @param string $callId TODO: type description here
* @param string $body Valid BXML string
* @return ApiResponse response from the API call
* @throws APIException Thrown if API call fails
*/
public function modifyCallBxml(
$accountId,
$callId,
$body
) {

//prepare query string for API call
$_queryBuilder = '/api/v2/accounts/{accountId}/calls/{callId}';

//process optional query parameters
$_queryBuilder = APIHelper::appendUrlWithTemplateParameters($_queryBuilder, array (
'accountId' => $accountId,
'callId' => $callId,
));

//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl($this->config->getBaseUri(Servers::VOICEDEFAULT) . $_queryBuilder);

//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'content-type' => 'application/xml; charset=utf-8'
);

//set HTTP basic auth parameters
Request::auth($this->config->getVoiceBasicAuthUserName(), $this->config->getVoiceBasicAuthPassword());

$_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);

//call on-before Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}
// Set request timeout
Request::timeout($this->config->getTimeout());

// and invoke the API call request to fetch the response
$response = Request::post($_queryUrl, $_headers, $body);

$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);

//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}

//Error handling using HTTP status codes
if ($response->code == 400) {
throw new Exceptions\ApiErrorException(
'Something\'s not quite right... Your request is invalid. Please fix it before trying again.',
$_httpContext
);
}

if ($response->code == 401) {
throw new APIException(
'Your credentials are invalid. Please use your Bandwidth dashboard credentials to authenticate to ' .
'the API.',
$_httpContext
);
}

if ($response->code == 403) {
throw new Exceptions\ApiErrorException('User unauthorized to perform this action.', $_httpContext);
}

if ($response->code == 404) {
throw new Exceptions\ApiErrorException(
'The resource specified cannot be found or does not belong to you.',
$_httpContext
);
}

if ($response->code == 415) {
throw new Exceptions\ApiErrorException(
'We don\'t support that media type for this endpoint. If a request body is required, please send it to us as ' .
'`application/xml`.',
$_httpContext
);
}

if ($response->code == 429) {
throw new Exceptions\ApiErrorException(
'You\'re sending requests to this endpoint too frequently. Please slow your request rate down and ' .
'try again.',
$_httpContext
);
}

if ($response->code == 500) {
throw new Exceptions\ApiErrorException('Something unexpected happened. Please try again.', $_httpContext);
}

//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);
return new ApiResponse($response->code, $response->headers, null);
}

/**
* Interrupts and replaces an active call's BXML document.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/BxmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@

final class BxmlTest extends TestCase
{
public function testBxml() {
$speakSentence = new BandwidthLib\Voice\Bxml\SpeakSentence("Test");
$pause = new BandwidthLib\Voice\Bxml\Pause();
$pause->duration("3");
$speakSentence->voice("susan");
$speakSentence->locale("en_US");
$speakSentence->gender("female");
$bxml = new BandwidthLib\Voice\Bxml\Bxml();
$bxml->addVerb($speakSentence, $pause);
$bxml->addVerb($pause);
$expectedXml = '<?xml version="1.0" encoding="UTF-8"?><Bxml><SpeakSentence locale="en_US" gender="female" voice="susan">Test</SpeakSentence><Pause duration="3"/></Bxml>';
$responseXml = $bxml->toBxml();
$this->assertEquals($expectedXml, $responseXml);
}

public function testForward() {
$forward = new BandwidthLib\Voice\Bxml\Forward();
$forward->to("+18888888888");
Expand Down