eristemena / dialogflow-fulfillment-webhook-php
Dialogflow Fulfillment Webhook v1 & v2
Requires
- php: >=5.4
- nesbot/carbon: 1.* || 2.*
Requires (Dev)
- clean/phpdoc-md: ^0.10.0
- phpunit/phpunit: ^4.8
README
此库受到dialogflow/dialogflow-fulfillment-nodejs的启发。
它支持Dialogflow的fulfillment webhook v1和v2代理的JSON请求和响应。
有关完整类参考,请参阅文档。
安装
通过composer安装: composer require eristemena/dialogflow-fulfillment-webhook-php
。
使用方法
启动代理
要启动代理,请使用带有来自Dialogflow的请求数组的\Dialogflow\WebhookClient
构造函数。
在纯PHP中,可以这样操作,
use Dialogflow\WebhookClient; $agent = new WebhookClient(json_decode(file_get_contents('php://input'),true)); // or $agent = WebhookClient::fromData($_POST);
或者如果您正在使用Laravel,
$agent = \Dialogflow\WebhookClient::fromData($request->json()->all());
获取请求信息
$intent = $agent->getIntent();
$action = $agent->getAction();
$query = $agent->getQuery();
$parameters = $agent->getParameters();
$session = $agent->getSession();
$contexts = $agent->getContexts();
$language = $agent->getLocale();
- 请求来源(例如:
google
、facebook
、slack
等)
$originalRequest = $agent->getRequestSource();
- 原始请求,平台特定有效载荷
$originalRequest = $agent->getOriginalRequest();
- 代理版本(1或2)
$agentVersion = $agent->getAgentVersion();
发送回复
要发送回复,请使用reply()
方法。
$agent->reply('Hi, how can I help?');
然后使用render()
获取数组形式的响应。您只需将数组作为JSON打印即可,
header('Content-type: application/json'); echo json_encode($agent->render());
或者在Laravel中,
return response()->json($agent->render());
响应有效载荷将自动根据请求的代理版本进行格式化。
丰富消息
文本
$text = \Dialogflow\RichMessage\Text::create() ->text('This is text') ->ssml('<speak>This is <say-as interpret-as="characters">ssml</say-as></speak>') ; $agent->reply($text);
图片
$image = \Dialogflow\RichMessage\Image::create('https://www.example.com/image.png'); $agent->reply($image);
卡片
$card = \Dialogflow\RichMessage\Card::create() ->title('This is title') ->text('this is text body') ->image('https://www.example.com/image.png') ->button('This is a button', 'https://docs.dialogflow.com/') ; $agent->reply($card);
建议
$suggestion = \Dialogflow\RichMessage\Suggestion::create(['Suggestion one', 'Suggestion two']); $agent->reply($suggestion);
自定义有效载荷
if ($agent->getRequestSource()=='google') { $agent->reply(\Dialogflow\RichMessage\Payload::create([ 'expectUserResponse' => false ])); }
谷歌动作
这个库也支持谷歌动作特定的功能。它仍在开发中,因此未来将添加更多功能。
要使用谷歌动作 Dialogflow 对话对象,您首先需要确保requestSource
来自谷歌助手,
if ($agent->getRequestSource()=='google') { $conv = $agent->getActionConversation(); // here you can use the rest of Actions on Google responses and helpers $agent->reply($conv); }
或者您可以直接调用getActionConversation()
方法,如果请求不是来自谷歌助手,它将返回null
。
$conv = $agent->getActionConversation(); if ($conv) { // here you can use the rest of Actions on Google responses and helpers } else { // the request does not come from Google Assistant }
发送回复
使用 Dialogflow 对话对象,您可以通过两种方式发送回复,
- 发送回复并关闭对话
$conv->close('Have a nice day!');
- 发送回复并等待用户响应
$conv->ask('Hi, how can I help?');
响应
简单响应
请参阅此处的文档。
use Dialogflow\Action\Responses\SimpleResponse; $conv->ask(SimpleResponse::create() ->displayText('Hello, how can i help?') ->ssml('<speak>Hello,<break time="0.5s"/> <prosody rate="slow">how can i help?</prosody></speak>') );
图片
use Dialogflow\Action\Responses\Image; $conv->close(Image::create('https://picsum.photos/400/300'));
基本卡片
请参阅此处的文档。
use Dialogflow\Action\Responses\BasicCard; $conv->close(BasicCard::create() ->title('This is a title') ->formattedText('This is a subtitle') ->image('https://picsum.photos/400/300') ->button('This is a button', 'https://docs.dialogflow.com/') );
列表
请参阅此处的文档。
单选列表向用户展示一个垂直的多个项目列表,并允许用户选择一个。从列表中选择一个项目将生成一个包含列表项目标题的用户查询(聊天泡泡)。
use Dialogflow\Action\Questions\ListCard; use Dialogflow\Action\Questions\ListCard\Option; $conv->ask('Please choose below'); $conv->ask(ListCard::create() ->title('This is a title') ->addOption(Option::create() ->key('OPTION_1') ->title('Option 1') ->synonyms(['option one','one']) ->description('Select option 1') ->image('https://picsum.photos/48/48') ) ->addOption(Option::create() ->key('OPTION_2') ->title('Option 2') ->synonyms(['option two','two']) ->description('Select option 2') ->image('https://picsum.photos/48/48') ) );
要捕获用户选择的选项,创建一个带有actions_intent_OPTION
事件的 Dialogflow 意图。假设您将意图命名为Get Option
,您可以按以下方式获取参数,
if ('Get Option'==$agent->getIntent()) { $conv = $agent->getActionConversation(); $option = $conv->getArguments()->get('OPTION'); switch ($option) { case 'OPTION_1': $conv->close('You choose option 1'); break; case 'OPTION_2': $conv->close('You choose option 2'); break; default: $conv->close('Sorry, i do not understand'); break; } }
轮播图
请参阅此处的文档。
轮播图水平滚动并允许选择一个项目。与列表选择器相比,它具有更大的瓷砖,允许更丰富的内容。构成轮播图的瓷砖类似于带图片的基本卡片。从轮播图中选择一个项目将简单地生成一个聊天泡泡作为响应,就像列表选择器一样。
use Dialogflow\Action\Questions\Carousel; use Dialogflow\Action\Questions\Carousel\Option; $conv->ask('Please choose below'); $conv->ask( Carousel::create() ->Option( Option::create() ->key('OPTION_1') ->title('Option 1') ->synonyms(['option one', 'one']) ->description('Select option 1') ->image('https://picsum.photos/300/300') ) ->Option( Option::create() ->key('OPTION_2') ->title('Option 2') ->synonyms(['option two', 'two']) ->description('Select option 2') ->image('https://picsum.photos/300/300') ) ->Option( Option::create() ->key('OPTION_3') ->title('Option 3') ->synonyms(['option three', 'three']) ->description('Select option 3') ->image('https://picsum.photos/300/300') ) ->Option( Option::create() ->key('OPTION_4') ->title('Option 4') ->synonyms(['option four', 'four']) ->description('Select option 4') ->image('https://picsum.photos/300/300') ) );
要检查用户是否授予您信息然后访问数据,创建一个带有actions_intent_OPTION
事件的 Dialogflow 意图。假设您将意图命名为Get Option
,您可以按以下方式获取参数,
if ('Get Option'==$agent->getIntent()) { $conv = $agent->getActionConversation(); $option = $conv->getArguments()->get('OPTION'); switch ($option) { case 'OPTION_1': $conv->close('You choose option 1'); break; case 'OPTION_2': $conv->close('You choose option 2'); break; case 'OPTION_3': $conv->close('You choose option 3'); break; case 'OPTION_4': $conv->close('You choose option 4'); break; default: $conv->close('Sorry, i do not understand'); break; } }
浏览轮播图
请参阅此处的文档。
浏览轮播图是一种丰富响应,类似于轮播图响应,因为它水平滚动并允许用户选择一个瓷砖。浏览轮播图专门设计用于网页内容,通过在网页浏览器(或所有瓷砖都启用了 AMP 的情况下在 AMP 浏览器)中打开所选的瓷砖。浏览轮播图还将持久保存在用户的助手界面上供以后浏览。
use Dialogflow\Action\Responses\BrowseCarousel; use Dialogflow\Action\Responses\BrowseCarousel\Option; $conv->ask('Please choose below'); $conv->ask( BrowseCarousel::create() ->imageDisplayOptions('CROPPED') ->addOption( Option::create() ->title('Title of item 1') ->description('Description of item 1') ->footer('Item 1 footer') ->url('http://www.example.com') ->image('https://picsum.photos/300/300') ) ->addOption( Option::create() ->title('Title of item 2') ->description('Description of item 2') ->footer('Item 2 footer') ->url('http://www.example.com') ->image('https://picsum.photos/300/300') ) ->addOption( Option::create() ->title('Title of item 3') ->description('Description of item 3') ->footer('Item 3 footer') ->url('http://www.example.com') ->image('https://picsum.photos/300/300') ) ->addOption( Option::create() ->title('Title of item 4') ->description('Description of item 4') ->footer('Item 4 footer') ->url('http://www.example.com') ->image('https://picsum.photos/300/300') ) );
用户与浏览轮播图项的交互不需要后续满足,因为轮播图处理浏览器传递。
建议芯片
请参阅以下文档:这里。
use Dialogflow\Action\Responses\LinkOutSuggestion; use Dialogflow\Action\Responses\Suggestions; $conv->ask('Please choose'); $conv->ask(new Suggestions(['Suggestion 1', 'Suggestion 2'])); $conv->ask(new Suggestions('Suggestion 3')); $conv->ask(new LinkOutSuggestion('Website', 'http://www.example.com'));
媒体响应
请参阅以下文档:这里。
媒体响应允许您的动作播放超过SSML 120秒限制的音频内容。媒体响应的主要组件是单轨卡片。
use Dialogflow\Action\Responses\MediaObject; use Dialogflow\Action\Responses\MediaResponse; use Dialogflow\Action\Responses\Suggestions; $conv->ask('Here you go'); $conv->ask( new MediaResponse( MediaObject::create('http://storage.googleapis.com/automotive-media/Jazz_In_Paris.mp3') ->name('Jazz in Paris') ->description('A funky Jazz tune') ->icon('http://storage.googleapis.com/automotive-media/album_art.jpg') ->image('http://storage.googleapis.com/automotive-media/album_art.jpg') ) ); $conv->ask(new Suggestions(['Pause', 'Stop', 'Start over']));
辅助工具
用户信息
请参阅以下文档:这里。
use Dialogflow\Action\Questions\Permission; $conv->ask(Permission::create('To address you by name and know your location', ['NAME', 'DEVICE_PRECISE_LOCATION']));
要检查用户是否授予了您信息并访问数据,请创建一个带有actions_intent_PERMISSION
事件的Dialogflow意图。假设您将意图命名为Get Permission
,您可以这样获取信息:
if ('Get Permission'==$agent->getIntent()) { $conv = $agent->getActionConversation(); $approved = $conv->getArguments()->get('PERMISSION'); if ($approved) { $name = $conv->getUser()->getName()->getDisplay(); $latlng = $conv->getDevice()->getLocation()->getCoordinates(); $lat = $latlng->getLatitude(); $lng = $latlng->getLongitude(); $conv->close('Got it, your name is ' . $name . ' and your coordinates are ' . $lat . ', ' . $lng); } else { $conv->close('Never mind then'); } }
日期和时间
请参阅以下文档:这里。
use Dialogflow\Action\Questions\DateTime; $conv->ask(new DateTime('When do you want to come in?', 'What is the best date to schedule your appointment?', 'What time of day works best for you?'));
要检查用户是否授予访问权限并访问数据,请创建一个带有actions_intent_DATETIME
事件的Dialogflow意图。假设您将意图命名为Get Date Time
,您可以按照以下方式获取信息:
if ('Get Date Time'==$agent->getIntent()) { $conv = $agent->getActionConversation(); $date = $conv->getArguments()->get('DATETIME'); if ($date) { $conv->close('Ok, got it, i will see you at ' . $date->format('r')); } else { $conv->close('Never mind then'); } }
地点和位置
请参阅以下文档:这里。
use Dialogflow\Action\Questions\Place; $conv->ask(new Place('Where do you want to have lunch?', 'To find lunch locations'));
要检查用户是否授予访问权限并访问数据,请创建一个带有actions_intent_PLACE
事件的Dialogflow意图。假设您将意图命名为Get Place
,您可以按照以下方式获取信息:
if ('Get Place'==$agent->getIntent()) { $conv = $agent->getActionConversation(); $place = $conv->getArguments()->get('PLACE'); if ($place) { $conv->close('Ok, got it, we\'ll meet at ' . $place->getFormattedAddress()); } else { $conv->close('Never mind then'); } }
确认
请参阅以下文档:这里。
use Dialogflow\Action\Questions\Confirmation; $conv->ask(new Confirmation('Can you confirm?'));
要检查用户是否确认,请创建一个带有actions_intent_CONFIRMATION
事件的Dialogflow意图。假设您将意图命名为Get Confirmation
,您可以按照以下方式获取信息:
if ('Get Confirmation'==$agent->getIntent()) { $conv = $agent->getActionConversation(); $confirmed = $conv->getArguments()->get('CONFIRMATION'); if ($confirmed) { $conv->close('Ok, it is confirmed'); } else { $conv->close('Alright then, it is canceled'); } }
界面能力
Google Assistant 可以在多种表面上使用,例如支持音频和显示体验的移动设备或仅支持音频体验的Google Home设备。
为了设计和构建适用于所有界面的对话,请使用界面能力来正确控制和范围您的对话。
$surface = $conv->getSurface(); if ($surface->hasScreen()) { // surface has screen } elseif ($surface->hasAudio()) { // surface has audio } elseif ($surface->hasMediaPlayback()) { // surface can play audio } elseif ($surface->hasWebBrowser()) { // user can interact with the content in a web browser }