Skip to content
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

Add play audio support #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/AmazonAlexaDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,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
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
94 changes: 94 additions & 0 deletions src/Extensions/Directives.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace BotMan\Drivers\AmazonAlexa\Extensions;

use BotMan\BotMan\Messages\Attachments\Attachment;

class Directives extends Attachment
{
const DEFAULT_PLAY_TYPE = 'AudioPlayer.Play';
const PLAY_PLAY_TYPE = 'AudioPlayer.Play';
const STOP_PLAY_TYPE = 'AudioPlayer.Stop';
const CLEAR_PLAY_TYPE = 'AudioPlayer.Clear-Queue';

const DEFAULT_PLAY_BEHAVIOUR_TYPE = 'ENQUEUE';
const ENQUEUE_PLAY_BEHAVIOUR_TYPE = 'ENQUEUE';
const REPLACE_PLAY_BEHAVIOUR_TYPE = 'REPLACE_ALL';
const REPLACE_ENQUEUE_BEHAVIOUR_TYPE = 'REPLACE_ENQUEUED';

protected $type = self::DEFAULT_PLAY_TYPE;
protected $playBehaviour = self::DEFAULT_PLAY_BEHAVIOUR_TYPE;
protected $audioItem;

public static function create()
{
return new self();
}

public function __construct()
{
$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 token($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(),
],
],
]);
}
}
71 changes: 71 additions & 0 deletions src/Extensions/Stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace BotMan\Drivers\AmazonAlexa\Extensions;

class Stream
{
const DEFAULT_OFFSET_IN_MILLISECONDS = 0;

protected $url;
protected $token;
protected $expectedPreviousToken;
protected $offsetInMilliseconds = self::DEFAULT_OFFSET_IN_MILLISECONDS;

public function url($url)
{
$this->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,
];
}
}