b00gizm/alexa-skill

针对亚马逊Alexa技能JSON接口的对象封装

v1.0.0 2017-01-05 14:59 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:58:19 UTC


README

Build Status Latest Stable Version

针对亚马逊 Alexa技能JSON接口的对象封装。

通过Composer安装

composer require b00gizm/alexa-skill

示例

快速入门

用不超过10行代码创建您的第一个Alexa应用!

$factory = new Alexa\EventFactory();
$alexa = new Alexa\Alexa($factory);
$alexa->onLaunchRequest(function(Alexa\Alexa $alexa, Alexa\LaunchRequest $request, Alexa\Session $session) {
    return $alexa->getResponse()->addPlainTextOutputSpeech("Welcome to my awesome app!");
});

return new JsonResponse($alexa->process($requestJson));

Alexa\Alexa类包含多个回调处理程序,用于处理不同的Alexa请求类型

$alexa->onLaunchRequest(function(Alexa\Alexa $alexa, Alexa\LaunchRequest $request, Alexa\Session $session) { ... });
$alexa->onIntentRequest(function(Alexa\Alexa $alexa, Alexa\Intent $intent, Alexa\LaunchRequest $request, Alexa\Session $session) { ... });
$alexa->onSessionEndedRequest(function(Alexa\Alexa $alexa, Alexa\LaunchRequest $request, Alexa\Session $session) { ... });

您的回调应该返回一个有效的Alexa\ResponseNULL,如果不知道如何处理请求(稍后会有更多介绍)。

编写自己的处理程序

提供Alexa请求处理程序的一种更复杂的方式是编写自己的处理程序类,实现以下接口之一(或多个)

  • Alexa\Handler\LaunchRequestHandler
  • Alexa\Handler\IntentRequestHandler
  • Alexa\Handler\SessionEndedHandler

自己的请求处理程序必须注册到Alexa\EventProcessor实例

$factory = new Alexa\EventFactory();

$processor = new Alexa\EventProcessor();
$processor->setLaunchHandler(new MyLaunchHandler());
$processor->setIntentHandlers([
    new MyFooIntentHandler(),
    new MyBarIntentHandler(),
]);
$processor->setSessionEndedHandler(new MySessionEndedHandler());

$alexa = new Alexa\Alexa($factory, $processor);

您可能已经注意到有一个意图处理程序数组。在编写具有大量意图的大型应用时,为每个意图编写自定义处理程序可能是组织代码库和保持可测试性的最佳解决方案。

意图处理程序按接收顺序处理。如果处理程序无法处理某个意图请求,则应简单地返回NULL以通知处理器尝试下一个意图处理程序。如果可以成功处理意图请求,则应返回一个有效的Alexa\Response对象,并通知处理器停止。

请注意,Alexa\Alexa的回调处理程序将始终优先,并且如果在某些原因下同时使用,将覆盖您已注册的自定义处理程序。

自动加载

此库使用schmittjoh/serializer进行序列化和反序列化,它本身依赖于doctrine/common进行注解支持。如果您遇到奇怪的序列化器错误,请将其添加到您的autolad.php中,作为当前的工作解决方案。

\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');

维护者

Pascal Cremer

许可

MIT许可证(MIT)

版权所有 (c) 2016-2017 Pascal Cremer

特此授予任何获得此软件及其相关文档文件(以下简称“软件”)副本的任何人免费处理软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许软件的接受者为此目的进行上述操作,前提是符合以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论此类责任是基于合同、侵权或其他原因,源于、因或与软件或软件的使用或其他方式有关。