pangodream/alexa-skill

将Alexa技能集成到自托管Web服务的库

dev-master 2019-01-27 19:21 UTC

This package is auto-updated.

Last update: 2024-09-28 07:46:44 UTC


README

PHP的自托管Web服务实现Alexa技能

此组件允许您解析和验证来自您创建的任何Alexa技能的传入请求。解析过程涵盖了基本功能,以知道何时调用了Intent以及如何向Alexa回复基本指令。请求验证检查了所有这些Amazon要求(除了最后一个)

  • 请求时间戳:150秒容差
  • 证书链URL(协议、端口、主机和URI)
  • 证书链验证(主题替代名称和FromTime-ToTime)
  • 使用证书公钥解密签名以匹配请求体的SHA1哈希
  • 证书链根证书颁发机构对Amazon有效(仍待处理)

尽管最后一个要求仍待处理,但在“认证”选项卡下的“功能测试”获得了“未发现错误”。

功能测试,至少在我的情况下,已进行了7次请求,并且该组件成功解决了所有请求

  • 请求格式正确 --> 响应OK
  • 请求格式正确 --> 响应OK
  • 请求格式正确 --> 响应OK
  • 证书链在指定的URL中不可用 --> 响应400错误请求
  • 没有签名 --> 响应400错误请求
  • 证书链在指定的URL中不可用 --> 响应400错误请求
  • 签名与计算出的SHA1哈希不匹配 --> 响应400错误请求

所有这些要求在托管自定义技能作为Web服务中描述

如何

如何使用此组件

Composer安装

composer require pangodream/alexa-skill

Laravel中的使用示例

这是一个在Laravel中制作的示例,尽管该组件没有外部依赖(除了PHP 7.0)

让我们在api.php文件中创建一个API入口点。

api.php

<?php

use Illuminate\Http\Request;
use App\EntradaLista;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

//Alexa
Route::post('/alexa', 'AlexaController@request');

此条目将为Alexa技能端点配置定义端点URL。在我的情况下

https://iot.mydomain.net/api/alexa

现在,创建控制器和接收http请求并处理的方法 AlexaController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Log;
use Alexa\Skill;
use Alexa\Response;

class AlexaController extends Controller
{
    public function request(Request $httpRequest){
        $skill = new Skill();
        $skill->apiRequest->processHttpRequest($httpRequest);
        $request = $skill->apiRequest->request;
        if ($request->type == "IntentRequest"){
            // Aply here the logic you want to perform based on the Alexa request intent
            // $applicationId = $skill->apiRequest->session->application->applicationId;
            // $sessionId = $skill->apiRequest->session->sessionId;
            // $userId = $skill->apiRequest->session->user->userId;
            // $intentId = $skill->apiRequest->request->intent->name;
            // $slotsName = $skill->apiRequest->request->intent->slotsName;
            // $mySlotValue = $skill->apiRequest->request->intent->slots['elemento']->value;
            // ...
            // ...
            // ...
            // And then compose the response you want to send back
            $response = new Response();
            $response->setOutputSpeech("You asked me to switch off ".$request->intent->slots['elemento']->value);
        }else{
            $response = $request->routeRequest();
        }
        return $skill->apiResponse->doReply($response);
    }
}

这就完成了!

以下是我在测试时使用的Alexa技能的JSON定义

技能JSON

{
    "interactionModel": {
        "languageModel": {
            "invocationName": "sebastian",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": [
                        "Adios"
                    ]
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                },
                {
                    "name": "Apagar",
                    "slots": [
                        {
                            "name": "elemento",
                            "type": "AMAZON.Room"
                        }
                    ],
                    "samples": [
                        "to switch off {elemento}",
                        "to turn off {elemento}",
                        "to disable {elemento}"
                    ]
                }
            ],
            "types": []
        },
        "dialog": {
            "intents": [
                {
                    "name": "Apagar",
                    "confirmationRequired": false,
                    "prompts": {},
                    "slots": [
                        {
                            "name": "elemento",
                            "type": "AMAZON.Room",
                            "confirmationRequired": false,
                            "elicitationRequired": true,
                            "prompts": {
                                "elicitation": "Elicit.Slot.777962330825.371179603765"
                            }
                        }
                    ]
                }
            ],
            "delegationStrategy": "ALWAYS"
        },
        "prompts": [
            {
                "id": "Elicit.Slot.777962330825.371179603765",
                "variations": [
                    {
                        "type": "PlainText",
                        "value": "What?"
                    }
                ]
            }
        ]
    }
}

创建并编译技能后,可以对其进行测试

你: "Alexa,让sebastian关掉客厅的扇子"

如果一切顺利,Alexa将回答

Alexa: "你让我关掉客厅的扇子"

欢迎提出建议或提问。

Alberto Iriberri

邮箱: development@pangodream.com Pangodream

许可证

此组件是开源软件,许可协议为MIT许可证