From 4d75f15782cd68427501c7a877dbf61b0916045d Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 12 Apr 2018 18:30:59 +0800 Subject: [PATCH 1/2] Add directives and stream to Alexa driver --- src/AmazonAlexaDriver.php | 5 ++ src/Extensions/Directives.php | 96 +++++++++++++++++++++++++++++++++++ src/Extensions/Stream.php | 68 +++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 src/Extensions/Directives.php create mode 100644 src/Extensions/Stream.php diff --git a/src/AmazonAlexaDriver.php b/src/AmazonAlexaDriver.php index 78aa347..b2aeef7 100644 --- a/src/AmazonAlexaDriver.php +++ b/src/AmazonAlexaDriver.php @@ -3,6 +3,7 @@ namespace BotMan\Drivers\AmazonAlexa; use BotMan\BotMan\Users\User; +use BotMan\Drivers\AmazonAlexa\Extensions\Directives; use Illuminate\Support\Collection; use BotMan\BotMan\Drivers\HttpDriver; use Techworker\Ssml\ContainerElement; @@ -125,6 +126,9 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam if ($attachment instanceof Card) { $parameters['card'] = $attachment; } + if ($attachment instanceof Directives) { + $parameters['directives'] = $attachment; + } } else { $text = $message; } @@ -147,6 +151,7 @@ public function sendPayload($payload) $response->respondSsml($payload['text']); } $response->card = $payload['card'] ?? null; + $response->directives = $payload['directives'] ?? null; $response->shouldEndSession = $payload['shouldEndSession'] ?? false; return Response::create(json_encode($response->render()))->send(); diff --git a/src/Extensions/Directives.php b/src/Extensions/Directives.php new file mode 100644 index 0000000..f0fdc94 --- /dev/null +++ b/src/Extensions/Directives.php @@ -0,0 +1,96 @@ +type = $type; + $this->playBehaviour = $playBehaviour; + $this->audioItem = new Stream; + } + + public function type($type) + { + $this->type = $type; + + return $this; + } + + public function getType() + { + return $this->type; + } + + public function playBehaviour($playBehaviour) + { + $this->playBehaviour = $playBehaviour; + + return $this; + } + + public function getPlayBehaviour() + { + return $this->playBehaviour; + } + + public function url($url) + { + $this->audioItem->url($url); + } + + public function setToken($token) + { + $this->audioItem->token($token); + } + + public function expectedPreviousToken($expectedPreviousToken) + { + $this->audioItem->expectedPreviousToken($expectedPreviousToken); + } + + public function offsetInMilliseconds($offsetInMilliseconds) + { + $this->audioItem->offsetInMilliseconds($offsetInMilliseconds); + } + + public function toWebDriver() + { + return []; + } + + public function render() + { + return array_filter([ + [ + 'type' => $this->type, + 'playBehavior' => $this->playBehaviour, + 'audioItem' => [ + 'stream' => $this->audioItem->renderStream() + ] + ] + ]); + } +} \ No newline at end of file diff --git a/src/Extensions/Stream.php b/src/Extensions/Stream.php new file mode 100644 index 0000000..7b5fbc7 --- /dev/null +++ b/src/Extensions/Stream.php @@ -0,0 +1,68 @@ +url = $url; + + return $this; + } + + public function getUrl() { + + return $this->url; + } + + public function token($token) + { + $this->token = $token; + + return $this; + } + + public function getToken() + { + return $this->token; + } + + public function expectedPreviousToken($expectedPreviousToken) + { + $this->expectedPreviousToken = $expectedPreviousToken; + + return $this; + } + + public function getExpectedPreviousToken() + { + return $this->expectedPreviousToken; + } + + public function offsetInMilliseconds($offsetInMilliseconds) + { + $this->offsetInMilliseconds = $offsetInMilliseconds; + + return $this; + } + + public function getOffsetInMilliseconds() + { + return $this->offsetInMilliseconds; + } + + public function renderStream() + { + return [ + 'url' => $this->url, + 'token' => $this->token, + 'expectedPreviousToken' => $this->expectedPreviousToken, + 'offsetInMilliseconds' => $this->offsetInMilliseconds + ]; + } +} \ No newline at end of file From d3ddd387c6b212108cde4ce2d1bcb08f7e046538 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 30 Apr 2018 09:18:26 +0800 Subject: [PATCH 2/2] Fixed syntax error --- src/AmazonAlexaDriver.php | 2 +- src/Extensions/Directives.php | 16 +++++++--------- src/Extensions/Stream.php | 13 ++++++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/AmazonAlexaDriver.php b/src/AmazonAlexaDriver.php index b2aeef7..344156c 100644 --- a/src/AmazonAlexaDriver.php +++ b/src/AmazonAlexaDriver.php @@ -3,7 +3,6 @@ namespace BotMan\Drivers\AmazonAlexa; use BotMan\BotMan\Users\User; -use BotMan\Drivers\AmazonAlexa\Extensions\Directives; use Illuminate\Support\Collection; use BotMan\BotMan\Drivers\HttpDriver; use Techworker\Ssml\ContainerElement; @@ -16,6 +15,7 @@ use BotMan\BotMan\Interfaces\DriverEventInterface; use BotMan\BotMan\Messages\Incoming\IncomingMessage; use BotMan\BotMan\Messages\Outgoing\OutgoingMessage; +use BotMan\Drivers\AmazonAlexa\Extensions\Directives; class AmazonAlexaDriver extends HttpDriver { diff --git a/src/Extensions/Directives.php b/src/Extensions/Directives.php index f0fdc94..4e28b97 100644 --- a/src/Extensions/Directives.php +++ b/src/Extensions/Directives.php @@ -20,15 +20,13 @@ class Directives extends Attachment protected $playBehaviour = self::DEFAULT_PLAY_BEHAVIOUR_TYPE; protected $audioItem; - public static function create($type, $playBehaviour) + public static function create() { - return new self($type, $playBehaviour); + return new self(); } - public function __construct($type, $playBehaviour) + public function __construct() { - $this->type = $type; - $this->playBehaviour = $playBehaviour; $this->audioItem = new Stream; } @@ -61,7 +59,7 @@ public function url($url) $this->audioItem->url($url); } - public function setToken($token) + public function token($token) { $this->audioItem->token($token); } @@ -88,9 +86,9 @@ public function render() 'type' => $this->type, 'playBehavior' => $this->playBehaviour, 'audioItem' => [ - 'stream' => $this->audioItem->renderStream() - ] - ] + 'stream' => $this->audioItem->renderStream(), + ], + ], ]); } } \ No newline at end of file diff --git a/src/Extensions/Stream.php b/src/Extensions/Stream.php index 7b5fbc7..e1a94d2 100644 --- a/src/Extensions/Stream.php +++ b/src/Extensions/Stream.php @@ -4,19 +4,22 @@ class Stream { + const DEFAULT_OFFSET_IN_MILLISECONDS = 0; + protected $url; protected $token; protected $expectedPreviousToken; - protected $offsetInMilliseconds; + protected $offsetInMilliseconds = self::DEFAULT_OFFSET_IN_MILLISECONDS; - public function url($url) { + public function url($url) + { $this->url = $url; return $this; } - public function getUrl() { - + public function getUrl() + { return $this->url; } @@ -62,7 +65,7 @@ public function renderStream() 'url' => $this->url, 'token' => $this->token, 'expectedPreviousToken' => $this->expectedPreviousToken, - 'offsetInMilliseconds' => $this->offsetInMilliseconds + 'offsetInMilliseconds' => $this->offsetInMilliseconds, ]; } } \ No newline at end of file