如何获取富文本响应对话框流程php API

时间:2018-07-21 08:34:31

标签: php dialogflow

您好,我正在尝试使用php API从dialogflow获得丰富的文本响应 我的示例代码

$session = $sessionsClient->sessionName('agentId', $sessionId ?: uniqid());
$textInput = new TextInput();
$textInput->setText('question');
$textInput->setLanguageCode('en-US');

$queryInput = new QueryInput();
$queryInput->setText($textInput);

$response = $sessionsClient->detectIntent($session, $queryInput);
$queryResult = $response->getQueryResult();
$fulfilmentText = $queryResult->getFulfillmentText();
echo fulfilmentText;
$sessionsClient->close();

上面的代码工作正常,但是当我将$fulfilmentText = $queryResult->getFulfillmentText();替换为$fulfillmentMessages = $queryResult->getFulfillmentMessages();并在打印时出现错误

  

不能将类Google \ Protobuf \ Internal \ RepeatedField的对象   转换为字符串

任何输入将不胜感激

1 个答案:

答案 0 :(得分:1)

在默认回复中,富文本!
富文本格式响应适用于其他渠道(Facebook Messenger,Skype ...)

您可以使用以下内容:

$fulfilmentMessages = $queryResult->getFulfillmentMessages();
$output = [];
foreach ($fulfilmentMessages as $number => $message) {
    switch ($message->getMessage()) {
        case 'text':
            $output[] = ['text' => $message->getText()->getText()->offsetGet(0)];
            break; 
        case 'card':
            $card = $message->getCard();
            $buttons = [];
            foreach ($card->getButtons() as $button) {
                 $buttons[] = [
                     'text' => $button->getText(),
                     'postback' => $button->getPostback()
                 ];
             }
             $output[] = ['card' => [
                'title' => $card->getTitle(),
                'subtitle' => $card->getSubTitle(),
                'image' => $card->getImageUri(),
                'buttons' => $buttons
             ]];
             break;
      }
}

$ output 将包含一个包含普通文本和富文本格式响应的数组。您需要为其制作一个漂亮的自定义视图。