codecommerce/alexaapi

此包的最新版本(1.0.5)没有可用的许可信息。

Amazon Alexa 库

1.0.5 2018-12-07 23:33 UTC

This package is auto-updated.

Last update: 2024-09-25 21:03:05 UTC


README

composer require codecommerce/alexaapi 

创建目录和配置文件

Mac OS

mkdir -p Alexa/Config && cp vendor/codecommerce/alexaapi/Config/* Alexa/Config && mkdir Alexa/Intents

Windows

mkdir Alexa
cd Alexa
mkdir Config
mkdir Intents
xcopy ../vendor/codecommerce/alexaapi/Config ./Config /e /i /h

Projekt composer.json

在项目目录中创建一个 composer.json 文件。使用 require 请求 AlexaApi,并在 autoload -> psr-4 中写入你的命名空间。

{
    "require": {
        "codecommerce/alexaapi": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "AlexaSpielwiese\\Alexa\\": "./Alexa"
        }
    }
}

第一个 Intent

创建一个类并给出命名空间(参见项目 composer.json)。实现 'IntentsInterface' 接口。实现接口所需的方法。你可以在构造函数中将变量 $sytem 和 $request 设置为类变量,从而可以在任何地方使用它们。

<?php

namespace AlexaSpielwiese\Alexa\Intents;

use CodeCommerce\AlexaApi\Intents\IntentsInterface;
use CodeCommerce\AlexaApi\Model\Request;
use CodeCommerce\AlexaApi\Model\System;

class TestIntent implements IntentsInterface
{
    protected $request;
    protected $system;

    /**
     * IntentsInterface constructor.
     * @param Request $request
     * @param System  $system
     */
    public function __construct(Request $request, System $system)
    {
        $this->request = $request;
        $this->system = $system;
    }

    /**
     * @return mixed
     */
    public function runIntent()
    {
        // TODO: Implement runIntent() method.
    }
}

在 runIntent 方法中发生所有魔法。

路由

将你的新类添加到路由中。

Alexa/Config/routes.yml

Intent 必须与 Amazon 开发者控制台中的名称相同

routes.yml

TestIntent: AlexaSpielwiese\Alexa\Intents\TestIntent

API 将执行剩余操作并调用你预留的 Intent。

检查显示

你可以让 $system 返回 Display 输出设备,从而检查是否有显示支持。

$this->system->hasViewport()

端点配置

表示端点的文件必须获得以下调用

require __DIR__ . '/../vendor/autoload.php';
new CodeCommerce\AlexaApi\Controller\RequestHandler();

Hello World

要获取输出,请使用此方法

 /**
 * @return mixed
 */
public function runIntent()
{
    $outSpeech = new Outspeech('Hello World');
    $response = new Response($outSpeech);
    (new ResponseHandler())->send($response);
}

添加背景图片

$backgroundImage = new BackgroundImage();
$backgroundImage->setSources('DEINE_URL');

$response = new Response($outspeech);

if ($this->system->getDisplay()) {
    $template = new Template();
    $template->setBackgroundImage($backgroundImage)
        ->setPrimary('Text 1')
        ->setSecondary('Text 2')
        ->setTertiary('Text 3')
        ->setType($template::BODY_TEMPLATE_2_IMAGE_LIMITED_CENTERED_TEXT);

    $directive = new Directives();
    $directive->setTemplate($template);
    $response->setDirectives($directive);
}