syousoufov/alexa-skills-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

syousoufov/alexa-skills-php

Composer 安装命令:

composer require syousoufov/alexa-skills-php

包简介

Utility package to handle the tedium of setting up a custom web service for Alexa skills.

README 文档

README

Utility package to handle the tedium of setting up a custom web service for Alexa skills.

Setup

  1. Require the package composer require syousoufov/alexa-skills-php

  2. Attach the service provider to config/app.php:

        // ..etc
        /*
         * Third Party Service Providers
         */
        AlexaPHP\Providers\AlexaServiceProvider::class,
        // ..etc
  3. Publish the alexa-skills-php config with php artisan vendor:publish --provider="AlexaPHP\Providers\AlexaServiceProvider" and change the required values. The defaults for everything except application_id should work just fine.

  4. Add the route middleware to the $routeMiddleware stack in app/Http/Kernel.php:

        /**
         * The application's route middleware.
         *
         * @var array
         */
        protected $routeMiddleware = [
            'alexa'    => \AlexaPHP\Middleware\AlexaRequestMiddleware::class,
        ];
  5. Routes can now be served through the middleware:

    $router->group(
        ['middleware' => ['alexa'],], function (Router $router) {
            $router->post('alexa', 'AlexaSkillsController@handleAlexaRequest');
    });

Handling requests and returning responses

Alexa requests can be routed to a single controller method which can act as a funnel to pass the request to appropriate handlers.

<?php

namespace App\Http\Controllers;

use AlexaPHP\Request\AlexaRequestInterface;
use AlexaPHP\Request\IntentRequest;
use AlexaPHP\Request\LaunchRequest;
use AlexaPHP\Request\SessionEndedRequest;
use AlexaPHP\Response\ResponseInterface;

class AlexaSkillsController extends Controller
{
    /**
     * Handle Alexa requests
     *
     * @param \AlexaPHP\Request\AlexaRequestInterface $alexa_request
     * @param \AlexaPHP\Response\ResponseInterface    $response
     * @return array
     */
    public function handleAlexaRequest(AlexaRequestInterface $alexa_request, ResponseInterface $response)
    {
        // Pass to its appropriate handler
        $method = camel_case($alexa_request->requestType());
    
        return $this->$method($alexa_request, $response);
    }

    /**
     * Handle a launch request
     *
     * @param \AlexaPHP\Request\LaunchRequest      $alexa_request
     * @param \AlexaPHP\Response\ResponseInterface $response
     * @return array
     */
    public function launchRequest(LaunchRequest $alexa_request, ResponseInterface $response)
    {
        return $response->say('LaunchRequest handled.');
    }

    /**
     * Handle an intent request
     *
     * @param \AlexaPHP\Request\IntentRequest      $alexa_request
     * @param \AlexaPHP\Response\ResponseInterface $response
     * @return array
     */
    public function intentRequest(IntentRequest $alexa_request, ResponseInterface $response)
    {
        $intent = $alexa_request->getIntent();

        return $response->say("$intent handled.");
    }

    /**
     * Handle a session end request
     *
     * @param \AlexaPHP\Request\SessionEndedRequest $alexa_request
     * @param \AlexaPHP\Response\ResponseInterface  $response
     * @return array
     */
    public function sessionEndedRequest(SessionEndedRequest $alexa_request, ResponseInterface $response)
    {
        return $response->endSession()->say('SessionEndedRequest handled.');
    }
}

Response types

Speech Response - return some speech to render to the user

    /**
     * Handle an intent request
     *
     * @param \AlexaPHP\Request\IntentRequest      $alexa_request
     * @param \AlexaPHP\Response\ResponseInterface $response
     * @return array
     */
    public function intentRequest(IntentRequest $alexa_request, ResponseInterface $response)
    {
        $intent = $alexa_request->getIntent();

        return $response->say("$intent handled.");
    }

Card - return a card to render to the Amazon Alexa app

	/**
	 * Handle an intent request
	 *
	 * @param \AlexaPHP\Request\IntentRequest      $alexa_request
	 * @param \AlexaPHP\Response\ResponseInterface $response
	 * @return array
	 */
	public function intentRequest(IntentRequest $alexa_request, ResponseInterface $response)
	{
		$intent = $alexa_request->getIntent();

		$card = new Card([
			'type' => 'Simple',
			'title' => 'SomeTitle',
			'content' => "Handled $intent!",
			'text' => 'SomeText',
			'image' => [
				'smallImageUrl' => 'http://someimg.com/url.jpg',
				'largeImageUrl' => 'http://someimg.com/urlx2.jpg',
			],
		]);

		$card->content; // Handled TestIntent!

		return $response->card($card);
	}

Reprompt - issue a reprompt

	/**
	 * Handle an intent request
	 *
	 * @param \AlexaPHP\Request\IntentRequest      $alexa_request
	 * @param \AlexaPHP\Response\ResponseInterface $response
	 * @return array
	 */
	public function intentRequest(IntentRequest $alexa_request, ResponseInterface $response)
	{
		$intent = $alexa_request->getIntent();

		return $response->reprompt("$intent handled.");
	}

Output Types

You can also specify PlainText or SSML speech output types for say and reprompt:

	/**
	 * Handle an intent request
	 *
	 * @param \AlexaPHP\Request\IntentRequest      $alexa_request
	 * @param \AlexaPHP\Response\ResponseInterface $response
	 * @return array
	 */
	public function intentRequest(IntentRequest $alexa_request, ResponseInterface $response)
	{
		return $response->reprompt("<speak>This output speech uses SSML.</speak>", ResponseInterface::TYPE_SSML);
	}

Extracting data from the request

This is not an exhaustive list of all available methods so please review the source to get a better understanding of what's available while this readme gets filled out.

	/**
	 * Handle Alexa requests
	 *
	 * @param \AlexaPHP\Request\AlexaRequestInterface $alexa_request
	 * @param \AlexaPHP\Response\ResponseInterface    $response
	 * @return array
	 */
	public function handleAlexaRequest(AlexaRequestInterface $alexa_request, ResponseInterface $response)
	{
		$request_type = $alexa_request->requestType();

		$session             = $alexa_request->getSession();
		$session_will_expire = $session->expiring();
		$user                = $session->user();
		$attributes          = $session->getAttribute('some.attribute');
		
		$verifier = $alexa_request->getVerifier();
		$lets_check_again = $verifier->verifyTimestamp();
		
		if (! $lets_check_again) {
			throw new AccessDeniedHttpException("Naughty, naughty");
		}

		return $response->say('I did stuff!');
	}

Testing

  1. Run composer install then phpunit.
  2. Code coverage: phpunit --coverage-html tests/coverage/ && open tests/coverage/index.html

TODO

  1. Complete documentation
  2. Open source the example web service
  3. Re-evaluate API decisions to get a more fluent syntax
  4. Get from 96% code coverage to 100%

syousoufov/alexa-skills-php 适用场景与选型建议

syousoufov/alexa-skills-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 syousoufov/alexa-skills-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 syousoufov/alexa-skills-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 syousoufov/alexa-skills-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 50
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-26