eristemena/botman-driver-dialogflow

BotMan 的 Dialogflow 满足驱动器

0.0.2 2018-05-13 23:21 UTC

This package is not auto-updated.

Last update: 2024-09-15 05:08:06 UTC


README

BotMan 驱动器用于处理与 BotMan 的 Dialogflow 满足。

Build Status codecov StyleCI

它使用 eristemena/dialog-fulfillment-webhook-php 库,因此支持 Dialogflow 请求 的 v1 和 v2 版本。

安装 & 设置

首先您需要获取驱动器。

composer require eristemena/botman-driver-dialogflow

如果您使用 BotMan Studio,那么基本上就是这样。

但是如果您不使用,那么在创建 BotMan 实例之前加载驱动器

DriverManager::loadDriver(\BotMan\Drivers\Dialogflow\DialogflowDriver::class);

// Create BotMan instance
BotManFactory::create([]);

用法

听到消息

您可以使用 hears() 开始接收消息,基于消息的 意图

$botman->hears('Intent Name', function ($botman) {
    // replies here
});

单条消息回复

回复接收到的最简单方法是使用 BotMan 自己的 reply() 方法

$botman->hears('Default Welcome Intent', function ($botman) {
    $botman->reply('Hi, welcome!');
});

多条消息回复

通常,当您想要发送多条回复时,您会多次使用 reply()。不幸的是,这对于 Dialogflow 驱动器不起作用,因为消息应该在一个单一的 响应 负载中。

为此,您必须使用此驱动器的特定方法,即 addMessage()sendMessage(),如下所示,

$botman->hears('Default Welcome Intent', function ($botman) {
    $botman->addMessage('Good morning');
    $botman->addMessage('How may i help you?');
    $botman->sendMessage();
});

富媒体消息

文本

使用 Dialogflow\RichMessage\Text

    $text = Text::create()
        ->text('Hello')
        ->ssml('
            <speak>
                Hello!
                <audio src="https://actions.google.com/sounds/v1/cartoon/clang_and_wobble.ogg"></audio>
            </speak>
        ')
    ;

    $botman->reply($text);

图像

使用 Dialogflow\RichMessage\Image

    $image = Image::create('https://picsum.photos/200/300');
    $botman
        ->addMessage('This is an image')
        ->addMessage($image)
        ->sendMessage()
    ;

卡片

使用 Dialogflow\RichMessage\Card

    $card = Card::create()
        ->title('This is title')
        ->text('This is text body, you can put whatever here.')
        ->image('https://picsum.photos/200/300')
        ->button('This is a button', 'https://docs.dialogflow.com/')
    ;

    $botman
        ->addMessage('This is a card')
        ->addMessage($card)
        ->sendMessage()
    ;

快速回复

使用 Dialogflow\RichMessage\Suggestion

    $suggestion = Suggestion::create(['Tell me a joke', 'Tell me about yourself']);

    $botman
        ->addMessage('Hi, how can i help you with?')
        ->addMessage($suggestion)
        ->sendMessage()
    ;

自定义负载

使用 Dialogflow\RichMessage\Payload

    $payload = Payload::create([
        'expectUserResponse' => false
    ]);

    $botman
        ->addMessage('Have a good day')
        ->addMessage($payload)
        ->sendMessage()
    ;