Integrate Laravel web service with custom alexa skill

|
| By Webner

Steps for integrating Laravel web service with custom alexa skill:

Step 1: Your web service should be on secure HTTP connection and should have SSL/TLS. Alexa strictly enforces for this to protect the confidentiality and integrity of your data.

Step 2: Open your amazon developer account
Go to your skill
Open “Endpoint” page
Select “HTTPS” radio option
Fill your web service url with your alexa route like “https://your-domain-name/your-app-uri”. Your all custom skill request will sent there.
Select “My development endpoint has a certificate from a trusted certificate authority” from the below drop down.

Step 3: Install “AlexaApp” package in your laravel project. Follow all the installation steps which are mentioned in AlexaApp package documentation.

Link: https://github.com/develpr/alexa-app

Step 4: Create new Controller with name “AlexaController”.

php artisan make:controller AlexaController
Open controller file and write the below code in it.

AlexaController.php

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use Develpr\AlexaApp\Request\AlexaRequest;
use Develpr\AlexaApp\Facades\Alexa;
use Develpr\AlexaApp\Response\AlexaResponse;

class AlexaController extends Controller {
	
	/**
	 * Alexa launch request.
	 * @var alexa request data
	 * @return json
	 */
	function alexalaunch(AlexaRequest $alexarequest) {
		return Alexa::ask ( "Hello, welcome to support center. You can ask say my color is red" )
		->endSession ( 'false' );
	}
	/**
	 * Alexa intent request.
	 * @var alexa request data
	 * @return json
	 */
	function alexaGetColor(AlexaRequest $alexarequest) {
		$color = $alexarequest->slot ( "color" );
		$session = Alexa::session ( 'color', $color );
		return Alexa::say ( "Great, your color is " . $color . ". Now you can ask me what's my color." )->endSession ( 'false' );
	}
	
	/**
	 * Alexa intent request.
	 * @var alexa request data
	 * @return json
	 */
	function alexaTellColor() {
		$color = Alexa::session( 'color' );
		return Alexa::say ( 'I remember your color is ' . $color . ". Good Bye." )->endSession (‘true’);
	}
	
	/**
	 * Alexa termination intent request.
	 * @var alexa request data
	 * @return json
	 */
	function alexaTerminate(AlexaRequest $alexarequest) {
		return Alexa::ask ( "Goodbye, have a nice day." )->endSession ();
	}
}

AlexaRequest: To handle the alexa request, we have to use “AlexaRequest $alexarequest” instance.

endSession(): used to maintain the alexa skill session. By default session end value is true and it means that session will end. If you don’t want to end session then set “false”.

Slot(): is use to get slot value. Just pass slot name in slot function and you will get slot value which user will say.

Session(): to save slot value during the skill session. Just pass session variable name to session function and it will give you value which stored in it.

Alexa::ask :send outputSpeech as a response to your skill

Step 5: After installing laravel AlexaApp package open your api.php from route folder. Add the below code to it.

Api.php

<?php 

use Illuminate\Http\Request;
use Develpr\AlexaApp\Facades\AlexaRouter;

AlexaRouter::launch ( '/alexa', 'App\Http\Controllers\API\AlexaController@alexalaunch' );
AlexaRouter::intent ( '/alexa', 'GetColorIntent', 'App\Http\Controllers\API\AlexaController@alexaGetColor' );
AlexaRouter::intent ( '/alexa', 'TellColorIntent', 'App\Http\Controllers\API\AlexaController@alexaTellColor' );
AlexaRoute::sessionEnded ( '/alexa', function () {
	return '{"version":"1.0","response":{"shouldEndSession":true}}';
} );

Add “use Develpr\AlexaApp\Facades\AlexaRouter;” this line before any route.

for alexa launch request.

AlexaRouter::launch ( '/Route_Name', 'Your_Controller_Name@Your_Function_Name' );

-> Route_Name: a route name which you have mentioned in your alexa skill endpoints as “your-app-uri”.
-> Your_Controller_Name: your controller name with path.
-> Your_Function_Name: the function name which you have written in your above mentioned controller.

for alexa intent request.

AlexaRouter::intent('/Route_Name','Your_Intent_Name','Your_Controller_Name@Your_Function_Name');

-> Your_Intent_Name: mention the intent request which you have created in your skill .

Step 5: Open your skill test page from amazon developer account

To start your skill invoke skill by your skill invocation name (Like Ask Support Center). After that you can request for intent request. For example, see below screenshot.

Leave a Reply

Your email address will not be published. Required fields are marked *