Skip to content

Commit

Permalink
Allow SSML usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Feb 12, 2018
1 parent 649e1ef commit d42673d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.0",
"botman/botman": "~2.0",
"minicodemonkey/amazon-alexa-php": "^0.1.5"
"minicodemonkey/amazon-alexa-php": "^0.1.5",
"techworker/ssml": "^1.0"
},
"require-dev": {
"botman/studio-addons": "~1.0",
Expand Down
24 changes: 24 additions & 0 deletions src/AlexaOutputSpeech.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace BotMan\Drivers\AmazonAlexa;

use Illuminate\Support\Collection;

class AlexaOutputSpeech {
const TYPE_SSML = 'SSML';
const TYPE_PLAIN_TEXT = 'PlainText';

public $type = self::TYPE_PLAIN_TEXT;

public $text = null;

public $ssml = null;

public function render() {
return Collection::make([
'type' => $this->type,
'text' => $this->text,
'ssml' => $this->ssml,
])->filter()->toArray();
}
}
32 changes: 32 additions & 0 deletions src/AlexaResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace BotMan\Drivers\AmazonAlexa;

use Alexa\Response\Response;

class AlexaResponse extends Response
{

const TYPE_PLAIN_TEXT = 'PlainText';

const TYPE_SSML = 'SSML';

public function respondText(string $text)
{
$this->outputSpeech = new AlexaOutputSpeech;
$this->outputSpeech->type = self::TYPE_PLAIN_TEXT;
$this->outputSpeech->text = $text;

return $this;
}

public function respondSsml(string $ssml)
{
$this->outputSpeech = new AlexaOutputSpeech;
$this->outputSpeech->type = self::TYPE_SSML;
$this->outputSpeech->ssml = $ssml;

return $this;
}

}
8 changes: 6 additions & 2 deletions src/AmazonAlexaDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use BotMan\BotMan\Users\User;
use Illuminate\Support\Collection;
use BotMan\BotMan\Drivers\HttpDriver;
use Techworker\Ssml\ContainerElement;
use BotMan\BotMan\Messages\Incoming\Answer;
use Alexa\Response\Response as AlexaResponse;
use BotMan\BotMan\Messages\Outgoing\Question;
use Symfony\Component\HttpFoundation\Request;
use BotMan\BotMan\Drivers\Events\GenericEvent;
Expand Down Expand Up @@ -141,7 +141,11 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
public function sendPayload($payload)
{
$response = new AlexaResponse();
$response->respond($payload['text']);
if (is_string($payload['text'])) {
$response->respondText($payload['text']);
} elseif ($payload['text'] instanceof ContainerElement) {
$response->respondSsml($payload['text']);
}
$response->card = $payload['card'] ?? null;
$response->shouldEndSession = $payload['shouldEndSession'] ?? false;

Expand Down
25 changes: 25 additions & 0 deletions tests/AmazonAlexaDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use BotMan\Drivers\AmazonAlexa\AmazonAlexaDriver;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
use BotMan\BotMan\Messages\Outgoing\OutgoingMessage;
use Techworker\Ssml\Element\Audio;
use Techworker\Ssml\SsmlBuilder;

class AmazonAlexaDriverTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -178,13 +180,36 @@ public function it_can_build_payload()
$this->assertSame([
'text' => 'question object',
], $payload);

$message = SsmlBuilder::factory()->text('This is SSML!');
$payload = $driver->buildServicePayload($message, $incomingMessage);

$this->assertSame([
'text' => $message
], $payload);
}

/** @test */
public function it_can_send_payload()
{
$driver = $this->getValidDriver();

$ssml = SsmlBuilder::factory();
$ssml->text('This is SSML!')->audio('foo')->up()->text('more Text');
$payload = [
'text' => $ssml,
];

/** @var Response $response */
$response = $driver->sendPayload($payload);
$this->assertSame('{"version":"1.0","sessionAttributes":[],"response":{"outputSpeech":{"type":"SSML","ssml":"<speak>This is SSML!<audio src=\"foo\"\/>more Text<\/speak>"},"card":null,"reprompt":null,"shouldEndSession":false}}', $response->getContent());
}

/** @test */
public function it_can_send_ssml_payload()
{
$driver = $this->getValidDriver();

$payload = [
'text' => 'string',
];
Expand Down

0 comments on commit d42673d

Please sign in to comment.