1. Go to this page and download the library: Download maxbeckers/amazon-alexa-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\RequestHandler\AbstractRequestHandler;
use MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest;
class WelcomeIntentHandler extends AbstractRequestHandler
{
public function __construct(
private readonly array $supportedApplicationIds = ['amzn1.ask.skill.your-skill-id']
) {
parent::__construct();
}
public function supportsRequest(Request $request): bool
{
return $request->request instanceof IntentRequest &&
$request->request->intent->name === 'WelcomeIntent';
}
public function handleRequest(Request $request): Response
{
return $this->responseHelper->respond(
outputSpeech: 'Welcome to our amazing skill!',
shouldEndSession: false
);
}
}
use MaxBeckers\AmazonAlexa\Request\Request;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\RequestHandler\AbstractRequestHandler;
use MaxBeckers\AmazonAlexa\Request\Request\Standard\IntentRequest;
class UserInfoHandler extends AbstractRequestHandler
{
public function __construct(
private readonly array $supportedApplicationIds = ['amzn1.ask.skill.your-skill-id']
) {
parent::__construct();
}
public function supportsRequest(Request $request): bool
{
return $request->request instanceof IntentRequest &&
$request->request->intent->name === 'GetUserInfoIntent';
}
public function handleRequest(Request $request): Response
{
$intentRequest = $request->request;
$userName = $intentRequest->intent->slots['userName']->value ?? 'friend';
$message = match($userName) {
'admin' => 'Hello administrator! You have special privileges.',
'guest' => 'Welcome, guest user. Limited features available.',
default => "Nice to meet you, {$userName}!"
};
return $this->responseHelper->respond($message);
}
}
use MaxBeckers\AmazonAlexa\Helper\SsmlGenerator;
$ssmlGenerator = new SsmlGenerator();
$ssmlGenerator
->say('Welcome to our cooking skill!')
->pauseStrength(SsmlGenerator::BREAK_STRENGTH_MEDIUM)
->say('Today we will learn about')
->emphasis('amazing', SsmlGenerator::EMPHASIS_STRONG)
->say('pasta recipes.')
->pauseTime('2s')
->say('Let\'s get started!');
$ssml = $ssmlGenerator->getSsml();
// Result: <speak>Welcome to our cooking skill! <break strength="medium" /> Today we will learn about <emphasis level="strong">amazing</emphasis> pasta recipes. <break time="2s" /> Let's get started!</speak>
use MaxBeckers\AmazonAlexa\Response\Directives\APL\RenderDocumentDirective;
use MaxBeckers\AmazonAlexa\Response\Response;
use MaxBeckers\AmazonAlexa\ResponseHelper;
// Inside a request handler:
$document = [
'type' => 'APL',
'version' => '1.8',
'mainTemplate' => [
'items' => [
[
'type' => 'Text',
'text' => '${payload.data.message}',
'style' => 'textStylePrimary1'
]
]
],
];
$dataSources = [
'data' => [
'message' => 'Hello from APL!'
],
];
$aplDirective = new RenderDocumentDirective(
token: 'mainScreen',
document: $document,
datasources: $dataSources
);
// Using the response helper (if available in your handler base)
$response = $this->responseHelper->respond(
outputSpeech: 'Showing a visual response.',
directives: [$aplDirective],
shouldEndSession: false
);
// Or manually:
$alexaResponse = new Response();
$alexaResponse->response->directives[] = $aplDirective;
use MaxBeckers\AmazonAlexa\Helper\DeviceAddressInformationHelper;
$addressHelper = new DeviceAddressInformationHelper();
try {
// Get full address (al_code" permission)
$countryAndPostalCode = $addressHelper->getCountryAndPostalCode($request);
$response = $this->responseHelper->respond(
"I found your location in {$fullAddress->city}, {$fullAddress->stateOrRegion}"
);
} catch (Exception $e) {
$response = $this->responseHelper->respond(
'I need permission to access your address. Please check your Alexa app.'
);
}