Как вернуть насыщенные сообщения в пользовательских событиях в Dialogflow? - PullRequest
0 голосов
/ 28 ноября 2018

Я нашел следующий код в сети и пытаюсь заставить его работать с моим проектом бота (учимся кодировать и создавать бота с помощью express.js, без firebase).

Я использую его дляполучите json со сторонних сайтов и отправьте информацию в бот Dialogflow.Он может собирать ключ json, значение (data: {}) и отвечать в событиях, используя имя события (name: '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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...