如何在Dialogflow中的自定义事件中返回丰富消息?

时间:2018-11-28 03:11:12

标签: javascript node.js dialogflow

我在线找到了以下代码,并试图使其与我的bot项目一起工作(学习如何使用express.js编写代码并构建bot,而无需使用firebase)。

我正在使用它从第三方网站获取json并在Dialogflow bot中回复信息。它可以收集json键,值(数据:{})并使用事件名称(名称:“ pic-found”)回复事件。它将以文本响应(使用此设置的响应:#pic-found.content)答复,但仅以文本答复。

是否有一种方法可以使它以丰富消息的形式回复json数据(例如,如果json数据包含图像,多个项目等,则是卡片,轮播等)?

'use strict'

    let axios = require('axios')

    const handler = (interaction) => {
      return new Promise((resolve, reject) => {
        // Check for parameters
        if (!interaction.parameters.hasOwnProperty('tags')) {
          reject(new Error('missing symbol parameter'))
        }

        let tags = interaction.parameters['tags']

        // Fetch the info using rest api
        axios
          .get(`http://example.com/wp-json/wp/v2/posts/${tags}`)
          .then(axiosResponse => {
            // Retrieve the info from the response object
            let posts = axiosResponse.data

            // Check if the API returned an error
            if (posts.Response && posts.Response === 'Error') {
              // The API returned an error
              // So build the response object to trigger the failure intent
              interaction.response.followupEvent = {
                name: 'pic-not-found',
                data: {}
              }
            } else {
              // The posts have been successfully retrieved
              // So build the response object to trigger the success intent
              interaction.response.followupEvent = {
                name: 'pic-found',
                data: {
                        title: posts.title.rendered
                        content: posts.content.rendered
                      }
              }
            }

            // Resolve the Promise to say that the handler performed the action without any error
            resolve()
          })
          .catch(e => {
            // An error occured during the request to the API
            // Reject the Promise to say that an error occured while the handler was performing the action
            reject(e)
          })
      })
    }

    module.exports = handler

0 个答案:

没有答案