diff --git a/app/Http/Controllers/Api/Web/AlexaController.php b/app/Http/Controllers/Api/Web/AlexaController.php new file mode 100644 index 00000000..cab5f158 --- /dev/null +++ b/app/Http/Controllers/Api/Web/AlexaController.php @@ -0,0 +1,165 @@ +json('request.type'); + $intent = $request->json('request.intent.name'); + + $logger->debug('Incoming Alexa Request', [ + 'type' => $type, + 'intent' => $intent + ]); + + $logger->debug('Incoming Alexa Full Request', $request->json()->all()); + + switch ($type) { + case 'LaunchRequest': + return $this->launch(); + case 'PlayAudio'; + return $this->play(); +// case 'AudioPlayer.PlaybackNearlyFinished': +// return $this->queueNextTrack(); + case 'IntentRequest': + return $this->handleIntent($request); + default: + return response()->make('', 204); + } + } + + public function handleIntent(Request $request) + { + $intent = $request->json('request.intent.name'); + + switch ($intent) { + case 'AMAZON.PauseIntent': + return $this->stop(); + case 'AMAZON.ResumeIntent': + return $this->play(); + case 'Author': + return $this->author(); + default: + return response()->make('', 204); + } + } + + public function launch() + { + return [ + 'version' => '1.0', + 'sessionAttributes' => (object)[], + 'response' => [ + "outputSpeech" => [ + "type" => "SSML", + "ssml" => "If you want to play music, say 'Alexa, ask pony fm to play'" + ], + 'shouldEndSession' => true, + ], + ]; + } + + public function unknown() + { + return [ + 'version' => '1.0', + 'sessionAttributes' => (object)[], + 'response' => [ + "outputSpeech" => [ + "type" => "SSML", + "ssml" => "Sorry, I don't recognise that command." + ], + 'shouldEndSession' => true, + ], + ]; + } + + public function author() + { + return [ + 'version' => '1.0', + 'sessionAttributes' => (object)[], + 'response' => [ + "outputSpeech" => [ + "type" => "SSML", + "ssml" => " + + Pony.fm was built by Pixel Wavelength for Viola to keep all her music in one place. + + " + ], + 'shouldEndSession' => true, + ], + ]; + } + + public function play() + { + return [ + 'version' => '1.0', + 'sessionAttributes' => (object)[], + 'response' => [ + 'directives' => [ + [ + 'type' => 'AudioPlayer.Play', + 'playBehavior' => 'REPLACE_ALL', + 'audioItem' => [ + 'stream' => [ + 'url' => 'https://pony.fm/t13840/stream.mp3', + 'token' => 't13840', + 'offsetInMilliseconds' => 0, + ], + ], + ], + ], + 'shouldEndSession' => true, + ], + ]; + } + + public function queueNextTrack() + { + return [ + 'version' => '1.0', + 'sessionAttributes' => (object)[], + 'response' => [ + 'directives' => [ + [ + 'type' => 'AudioPlayer.Play', + 'playBehavior' => 'ENQUEUE', + 'audioItem' => [ + 'stream' => [ + 'url' => 'https://pony.fm/t13840/stream.mp3', + 'token' => 't13840', + 'expectedPreviousToken' => 'fma', + 'offsetInMilliseconds' => 0, + ], + ], + ], + ] + ], + ]; + } + + public function stop() + { + return [ + 'version' => '1.0', + 'sessionAttributes' => (object)[], + 'response' => [ + 'directives' => [ + [ + 'type' => 'AudioPlayer.Stop' + ], + ], + 'shouldEndSession' => true, + ], + ]; + } +} diff --git a/routes/web.php b/routes/web.php index 960b7034..284d0b9e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -87,6 +87,8 @@ Route::group(['prefix' => 'api/v1', 'middleware' => 'json-exceptions'], function Route::group(['prefix' => 'api/web'], function () { + Route::post('/alexa', 'Api\Web\AlexaController@handle'); + Route::get('/taxonomies/all', 'Api\Web\TaxonomiesController@getAll'); Route::get('/search', 'Api\Web\SearchController@getSearch');