Dialogflow实现Webhook调用失败

时间:2020-01-03 16:30:56

标签: javascript firebase dialogflow-es javascript-objects dialogflow-es-fulfillment

enter image description here我是dialogflow实现的新手,我正在尝试根据用户问题从新闻API检索新闻。我遵循了新闻API提供的文档,但是我无法从搜索结果中捕获任何响应,因为在控制台中运行该函数不是错误。我更改了代码,看起来现在它可以到达newsapi端点,但未获取任何结果。我正在利用https://newsapi.org/docs/client-libraries/node-js发出搜索有关该主题的所有内容的请求。当我诊断该功能时,它说“ Webhook调用失败。错误:无法使用。”

'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const http = require('http');


const host = 'newsapi.org';
const NewsAPI = require('newsapi');
const newsapi = new NewsAPI('63756dc5caca424fb3d0343406295021');

process.env.DEBUG = 'dialogflow:debug';

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) =>
{
  // Get the city 
  let search = req.body.queryResult.parameters['search'];// search is a required param
  

  // Call the weather API
  callNewsApi(search).then((response) => {
    res.json({ 'fulfillmentText': response }); // Return the results of the news API to Dialogflow
  }).catch((xx) => {
    console.error(xx);
    res.json({ 'fulfillmentText': `I don't know the news but I hope it's good!` });
  });
});

function callNewsApi(search) 
{
  console.log(search);
  newsapi.v2.everything
  (
    { 
        q: 'search',
        langauge: 'en',
        sortBy: 'relevancy',
        source: 'cbc-news',
        domains: 'cbc.ca',
        from: '2019-12-31',
        to: '2020-12-12',
        page: 2
    }
  ).then (response => {console.log(response);
                       {                               

                      
     let articles = response['data']['articles'][0];
                      

        // Create response
        
let responce = `Current news in the $search with following title is  ${articles['titile']} which says that 
        ${articles['description']}`;

        // Resolve the promise with the output text
        console.log(output);
       
                       }
   });  
  

}

这也是RAW API响应

{
  "responseId": "a871b8d2-16f2-4873-a5d1-b907a07adb9a-b4ef8d5f",
  "queryResult": {
    "queryText": "what is the latest news about toronto",
    "parameters": {
      "search": [
        "toronto"
      ]
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/misty-ktsarh/agent/intents/b52c5774-e5b7-494a-8f4c-f783ebae558b",
      "displayName": "misty.news"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {
      "webhook_latency_ms": 543
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "code": 14,
    "message": "Webhook call failed. Error: UNAVAILABLE."
  },
  "outputAudio": "UklGRlQqAABXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAAZGF0YTAqAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA... (The content is truncated. Click `COPY` for the original JSON.)",
  "outputAudioConfig": {
    "audioEncoding": "OUTPUT_AUDIO_ENCODING_LINEAR_16",
    "synthesizeSpeechConfig": {
      "speakingRate": 1,
      "voice": {}
    }
  }
} 

这是履行要求:

{
  "responseId": "a871b8d2-16f2-4873-a5d1-b907a07adb9a-b4ef8d5f",
  "queryResult": {
    "queryText": "what is the latest news about toronto",
    "parameters": {
      "search": [
        "toronto"
      ]
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "intent": {
      "name": "projects/misty-ktsarh/agent/intents/b52c5774-e5b7-494a-8f4c-f783ebae558b",
      "displayName": "misty.news"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {
      "webhook_latency_ms": 543
    },
    "languageCode": "en"
  },
  "webhookStatus": {
    "code": 14,
    "message": "Webhook call failed. Error: UNAVAILABLE."
  },
  "outputAudio": "UklGRlQqAABXQVZFZm10IBAAAAABAAEAwF0AAIC7AAACABAAZGF0YTAqAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA... (The content is truncated. Click `COPY` for the original JSON.)",
  "outputAudioConfig": {
    "audioEncoding": "OUTPUT_AUDIO_ENCODING_LINEAR_16",
    "synthesizeSpeechConfig": {
      "speakingRate": 1,
      "voice": {}
    }
  }
}

这也是firebase控制台的屏幕截图。 enter image description here

有人可以指导我在这里我想念的是什么吗?

2 个答案:

答案 0 :(得分:2)

关键是错误消息中的前三行:

Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'newsapi'

是说无法加载newsapi模块,最可能的原因是您没有在package.json文件中将其列为依赖项。

如果使用的是Dialogflow内联编辑器,则需要选择package.json标签,并在dependencies部分中添加一行。

Inline Editor

更新

不清楚何时/何地出现“ UNAVAILABLE”错误,但是如果您使用Dialogflow的Inline Editor,则可能的原因之一是它使用Firebase的“ Spark”定价计划,该计划存在局限性在Google网络外部的网络通话中。

您可以upgrade to the Blaze plan,该文件确实需要使用信用卡,但其中包含Spark计划的免费套餐,因此在轻度使用期间不会招致任何费用。这样可以进行网络通话。

更新基于TypeError: Cannot read property '0' of undefined

这表明一个属性(或可能是一个属性的索引)正在尝试引用未定义的内容。

目前尚不清楚确切是哪一行,但是这些行都是可疑的:

    let response = JSON.parse(body);
    let source = response['data']['source'][0];
    let id = response['data']['id'][0];
    let name = response['data']['name'][0];
    let author = response['author'][0];
    let title = response['title'][0];
    let description = response['description'][0];

因为它们都引用了属性。我将检查确切返回的内容并存储在response中。例如,发送回的内容中是否没有“数据”或“作者”字段?

https://newsapi.org/docs/endpoints/everything看,这些 are 字段似乎都不存在,但是有一个articles属性被发回,其中包含一组文章。您可能希望为其编制索引并获取所需的属性。

更新

看起来像这样,尽管您正在用此行将参数加载到变量中

// Get the city and date from the request
let search = req.body.queryResult.parameters['search'];// city is a required param

您实际上并没有在任何地方使用search变量。相反,您似乎在此行中将文字字符串“搜索”传递给函数

callNewsApi('search').then((output) => {

我猜是在搜索“搜索”一词。

您指示“它转到捕获部分”,这表明通话中出现了问题。您不会在catch部分中显示任何日志记录,并且记录抛出的异常可能很有用,因此您知道为什么将其发送到catch部分。像

}).catch((xx) => {
  console.error(xx);
  res.json({ 'fulfillmentText': `I don't know the news but I hope it's good!` });
});

是正常的,但是由于您似乎将其记录在.on('error')部分中,因此显示该错误可能很有用。

答案 1 :(得分:0)

意图的名称和我用来进行调用的变量在Casing中有所不同,我想调用是区分大小写的,只要注意这一点