代码之家  ›  专栏  ›  技术社区  ›  Alex Wohlbruck

通过Dialogflow webhook实现返回对Google上的操作的丰富响应

  •  2
  • Alex Wohlbruck  · 技术社区  · 7 年前

    要让Google Assistant向用户显示丰富的响应,必须向其提供如下响应 example on the Actions on Google docs . 然而,由于我使用Dialogflow作为我的服务器和Google之间的中间方,我需要提供 some kind of response to Dialogflow 在我的webhooks中,表示应该有丰富的响应。从该链接中可以看到,文档提到了如何向FB Messenger、Kik、LINE等发送丰富的响应,但没有提到Google Assistant。

    我错过了什么?我在Dialogflow web控制台中看到了丰富响应的选项,但在那里,我似乎只能输入硬编码响应,而没有来自服务器的动态数据。这样做的正确方法是什么?

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  7
  •   Nazeem    7 年前

    使用Dialogflow集成,您的webhook应该为丰富响应返回的JSON响应如下所示:

    {
        "data":{
            "google":{
                "expectUserResponse":true,
                "noInputPrompts":[
    
                ],
                "richResponse":{
                    "items":[
                        {
                            "simpleResponse":{
                                "textToSpeech":"Welcome to this Basic Card",
                                "displayText":"Welcome to this Basic Card"
                            }
                        },
                        {
                            "basicCard":{
                                "buttons":[
                                    {
                                        "title":"Button Title",
                                        "openUrlAction":{
                                            "url":"https://some.url"
                                        }
                                    }
                                ],
                                "formattedText":"Some text",
                                "image":{
                                    "url":"http://some_image.jpg",
                                    "accessibilityText":"Accessibility text describing the image"
                                },
                                "title":"Card Title"
                            }
                        }
                    ],
                    "suggestions":[
                        {
                            "title":"Aléatoire"
                        },
                        {
                            "title":"Top"
                        }
                    ]
                }
            }
        }
    }
    

    如果您正在使用 Node.js library 您还可以使用提供的Dialogflow集成方法来构建 rich response .

        2
  •  2
  •   Bruno Araújo    7 年前

    如果您使用的是节点。js您应该调用该方法 buildRichResponse() 然后添加项作为该对象的子对象,如下所示:

    app.ask(app.buildRichResponse()
    .addSimpleResponse('A text to be spoken')
    .addBasicCard(app.buildBasicCard('Some text to be displayed')
      .setTitle('A title')
      .addButton('Read more', 'https://example.google.com/something')
      .setImage('https://example.google.com/image.png', 'Image alternate text')
      .setImageDisplay('CROPPED')
      )
    );
    

    这是添加BasicCard的一个示例,您可以在以下位置查看如何添加旋转木马、列表和建议芯片 https://developers.google.com/actions/assistant/responses#rich-responses