minicodemonkey / amazon-alexa-php
PHP的Amazon Alexa接口
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.6
README
此库提供了一个方便的接口,用于开发针对您的PHP应用程序的Amazon Alexa技能。
用法
通过composer安装: composer require minicodemonkey/amazon-alexa-php
。
请求
当Amazon Alexa触发您的技能时,会向您为应用程序指定的URL发送HTTP请求。
您可以根据以下方式解析请求的JSON
正文:
$jsonDataAsArray = $request->json()->all(); // This is how you would retrieve this with Laravel $alexaRequest = \Alexa\Request\Request::fromData($jsonDataAsArray);
您可以使用instanceof
确定请求的类型,例如:
if ($alexaRequest instanceof IntentRequest) { // Handle intent here }
响应
您可以使用Response
类构建多个Alexa响应。您还可以选择设置卡片或重新提示。
以下是一些示例。
简单文本响应
$response = new \Alexa\Response\Response; $response->respond('I\'m your response message');
带重新提示
$response = new \Alexa\Response\Response; $response->reprompt('What is your favourite color?');
卡片
有关卡片的详细信息,请参阅以下链接: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/providing-home-cards-for-the-amazon-alexa-app#creating-a-home-card-to-display-text-and-an-image
SingleCard
$response = new \Alexa\Response\Response; $response->respond('Cooool. I\'ll lower the temperature a bit for you!') ->withCard('Temperature decreased by 2 degrees');
带有图片的StandardCard
您也可以在卡片中显示图片
请注意有关图片大小和托管的一些注意事项: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/providing-home-cards-for-the-amazon-alexa-app#image_size
$response = new \Alexa\Response\Response; $response->respond('Cooool. I\'ll lower the temperature a bit and show you an image!') ->withImageCardCard('My title', 'My caption text for the image...', 'https://url.to/small-image.jpg', 'https://url.to/large-image.jpg');
LinkAccountCard
LinkAccountCard用于已启用帐户链接的技能,并将显示到您配置的帐户链接URL的链接。由于标题、文本等都是自动设置的,因此无法设置随机文本。
$response = new \Alexa\Response\Response; $response->respond('To link the skill with your account, click the linkAccount shown in your alexa app.') ->withLinkAccountCard();
输出响应
要输出响应,请简单地使用->render()
函数,例如在Laravel中,您将创建响应如下:
return response()->json($response->render());
纯PHP
header('Content-Type: application/json'); echo json_encode($response->render()); exit;
待办事项
- 自动验证请求时间戳完整性