skollro / alexa-php-sdk
用于在PHP中开发Amazon Alexa技能的表达式SDK。
Requires
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2024-09-22 14:55:59 UTC
README
此包提供了一个基于 https://github.com/maxbeckers/amazon-alexa-php 的、与框架无关的表达式SDK,用于在PHP中开发Alexa技能。
use Skollro\Alexa\Alexa; use MaxBeckers\AmazonAlexa\Request\Request; $request = Request::fromAmazonRequest(file_get_contents('php://input'), $_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE']); Alexa::skill('amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX') ->intent('HelloIntent', function ($request, $response) { $response->say('Hello'); }) ->handle($request, function ($response) { header('Content-Type: application/json'); echo json_encode($response); });
免责声明
此包不是官方的Amazon Alexa SDK。
到目前为止,此包不支持编写Alexa技能可能需要的所有操作,但是一个坚实的基础。因此,请随意提交缺少功能的PR。
安装
您可以通过composer安装此包
composer require skollro/alexa-php-sdk
用法
创建技能并处理请求
以下代码可作为您自己的技能的起点。首先,您必须从POST请求的数据中创建一个 Request
对象。然后,使用 Alexa::skill($applicationId)
创建一个新的技能,传递您的应用程序ID。最后,定义您的请求处理器并调用 handle($request)
,该处理器处理请求并创建响应。
use MaxBeckers\AmazonAlexa\Request\Request; $request = Request::fromAmazonRequest(file_get_contents('php://input'), $_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE']); $alexa = Alexa::skill('amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'); // define middlewares and request handlers... $response = $alexa->handle($request); // send response header('Content-Type: application/json'); echo json_encode($response);
中间件
所有请求在调用请求处理器之前都会通过几个中间件。请记住返回 $next($request, $response)
来调用链中的下一个中间件。如果从中间件返回一个 $response
,则不会调用其他处理器,并且立即返回 $response
。
自动附加的中间件
当使用 Alexa::skill($applicationId)
创建技能时,我们自动附加以下两个中间件。
- Skollro\Alexa\VerifyRequest: 验证请求是否来自Amazon Echo API,并检查签名。
- Skollro\Alexa\VerifyApplicationId: 验证请求是否针对您的应用程序。
中间件之前
检查访问令牌是中间件在请求处理器之前运行的典型用例。
$alexa->middleware(function ($next, $request, $response) { if (! $request->context->system->user->accessToken) { return $response->say('Link your account')->linkAccount(); } return $next($request, $response); });
中间件之后
要运行请求处理器之后的中间件,将 $next($request, $response)
的结果存储在一个临时变量中,稍后返回响应。
$alexa->middleware(function ($next, $request, $response) { $response = $next($request, $response); // modify $response here... return $response; });
请求处理器
Amazon Echo API向您的应用程序发送不同类型的请求。使用这些回调来实现您的技能逻辑。您可以使用可调用的类来更好地封装。
class BarIntent { public function __invoke($request, $response) { $response->say('Use an invokable class as request handler'); } } $alexa->launch(function ($request, $response) { $response->say('Welcome to your skill'); }); $alexa->intent('HelloIntent', function ($request, $response) { $response->say('Hello world'); }); $alexa->intent('BarIntent', new BarIntent);
响应
您可以使用流畅且自然的语法来创建响应。
$alexa->intent('HelloIntent', function ($request, $response) { // Basic response $response->say('Hello world'); // Use cards for showing information in a user's Alexa App $response->simple($title, $content); $response->standard($title, $content, $smallImageUrl, $largeImageUrl); $response->linkAccount(); // Use fluent syntax $response->say('Link your account')->linkAccount(); });
异常
如果您在请求处理器中抛出异常,我们将调用此回调,以便您可以在那里处理异常。
$alexa->exception(function ($e, $request, $response) { if ($e instanceof MyException) { return $response->say('An error occurred'); } throw $e; });
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。