scaletta / botman-driver-dialogflow

BotMan 的 Dialogflow 充分驱动程序,包含修复

1.0.0 2020-03-28 17:41 UTC

This package is auto-updated.

Last update: 2024-09-29 03:10:22 UTC


README

PHP Composer

BotMan Dialogflow 驱动程序,包含修复

BotMan 驱动程序,用于处理 Dialogflow 充分,基于 BotMan。原始仓库不再维护,pull requests 没有回答。因此,我基于修复创建了分支。当前的修复主要是能够向 DialogFlow 提供输出上下文。

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

安装 & 设置

首先,您需要拉取驱动程序。

composer require Scaletta/botman-driver-dialogflow

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

但是如果您不这样做,那么在创建 BotMan 实例之前加载驱动程序。

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

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

用法

接收消息

您可以通过基于消息的 Intent 使用 hears() 开始接收消息。

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

单条消息回复

回复传入消息的最简单方法是使用 BotMan 的 reply() 方法。

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

添加输出上下文

要使用 DialogFlow 中的参数,可以使用输出上下文。

$parameters = ["Parameter_1" => "Test1", "Parameter_2" => "Test2"];
$botman
       ->addMessage(["name" => "test", "parameters" => $parameters, "lifespan" => 5])
       ->addMessage("This message contains Outgoing Context")
       ->sendMessage();

多条消息回复

通常,当您想发送多条回复时,您会多次使用 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()
    ;