maxbeckers / amazon-alexa-php
Php library for amazon echo (alexa) skill development.
Installs: 35 093
Dependents: 3
Suggesters: 0
Security: 0
Stars: 113
Watchers: 13
Forks: 30
Open Issues: 3
Requires
- php: >=7.0
- guzzlehttp/guzzle: >=6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: >=2.8
- phpunit/phpunit: >=6.0
- dev-master
- 1.13.0
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.x-dev
- 1.5.0
- 1.4.x-dev
- 1.4.0
- 1.3.x-dev
- 1.3.0
- 1.2.x-dev
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- dev-feature/playback
- dev-feature/version_2.0
This package is auto-updated.
Last update: 2024-12-10 11:10:31 UTC
README
Amazon alexa php library
This library is a helper for amazon echo (alexa) skills with php. With this library it's very simple to handle alexa requests in your php application. You only create some handlers for the requests of your alexa skill and add them to a registry.
Install via composer
Require the package with composer:
composer require maxbeckers/amazon-alexa-php
Usage
Handle the request:
- map request data to request object
- validate request
- handle request data
- create response
- send response
Map request data to request object
Map needed request headers and request body to Request
.
use MaxBeckers\AmazonAlexa\Request\Request; ... $requestBody = file_get_contents('php://input'); $alexaRequest = Request::fromAmazonRequest($requestBody, $_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE']);
Validate request
The RequestValidator
will handle the amazon request validation.
use MaxBeckers\AmazonAlexa\Validation\RequestValidator; ... $validator = new RequestValidator(); $validator->validate($alexaRequest);
Register request handlers
For different requests it's helpful to create different RequestHandlers.
use MaxBeckers\AmazonAlexa\RequestHandler\RequestHandlerRegistry; ... $requestHandlerRegistry = new RequestHandlerRegistry(); $requestHandlerRegistry->addHandler($myRequestHandler);
Use registry to handle request
use MaxBeckers\AmazonAlexa\RequestHandler\RequestHandlerRegistry; ... $requestHandler = $requestHandlerRegistry->getSupportingHandler($alexaRequest); $response = $requestHandler->handleRequest($alexaRequest);
Render response
header('Content-Type: application/json'); echo json_encode($response); exit();
Create a new request handler
The new request handler must extend AbstractRequestHandler
.
In constructor set the supportedApplicationIds
to your skill Ids.
public function __construct() { $this->supportedApplicationIds = ['my_amazon_skill_id']; }
Then implement the abstract supportsRequest
-method.
public function supportsRequest(Request $request): bool { return $request->request instanceOf MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest && 'MyTestIntent' === $request->request->intent->name; }
Then implement the handleRequest
-method. For simple responses there is a ResponseHelper
.
use MaxBeckers\AmazonAlexa\Helper\ResponseHelper; ... public function handleRequest(Request $request): Response { return $this->responseHelper->respond('Success :)'); }
Check device address information
To get either "Full Address" or "Country & Postal Code" from the customer you need the permissions for user api call. More informations for the call see device-address-api.
$helper = new DeviceAddressInformationHelper(); $fullAddress = $helper->getAddress($request); $countryAndPostalCode = $helper->getCountryAndPostalCode($request);
Generate SSML
For SSML output you can use the SsmlGenerator
. With the helper will generate valid SSML for alexa. All types of alexa known SSML tags have a function in the SsmlGeneator
.
You can add all SSML you need to the generator and call getSsml
to get the full string.
$ssmlGenerator = new SsmlGenerator(); $ssmlGenerator->say('one'); $ssmlGenerator->pauseStrength(SsmlGenerator::BREAK_STRENGTH_MEDIUM); $ssmlGenerator->say('two'); $ssml = $ssmlGenerator->getSsml(); // $ssml === '<speak>one <break strength="medium" /> two</speak>'
Symfony Integration
There is also a symfony bundle on maxbeckers/amazon-alexa-bundle.