Dialogflow实现Webhook调用失败 [英] Dialogflow Fulfilment webhook call failed

查看:77
本文介绍了Dialogflow实现Webhook调用失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是dialogflow实现的新手,我正在尝试根据用户问题从新闻API检索新闻。我遵循了新闻API提供的文档,但是我无法从搜索结果中捕获任何响应,因为在控制台中运行该函数不是错误。我更改了代码,看起来现在它可以到达newsapi端点,但未获取任何结果。我正在使用


有人可以指导我在这里缺少什么吗?

解决方案

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

 功能失败关于加载用户代码。错误消息:无法加载文件index.js中的代码。 
您是否在package.json依赖项中列出了所有必需的模块?
详细的堆栈跟踪:错误:找不到模块'newsapi'

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



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





更新



目前尚不清楚您何时何地正在收到无法使用错误,但如果您使用Dialogflow的内联编辑器,则可能是由于它使用的是Firebase Spark定价计划,该计划对Google网络外部的网络通话有限制。



您可以升级到Blaze计划,该记录确实需要使用信用卡,但其中包含Spark计划的免费套餐,因此在轻度使用期间您不应该承担任何费用。



Update 基于 TypeError:无法读取未定义的属性'0' code>



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



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

  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];

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



查看 https://newsapi.org/docs/endpoints/everything ,看来这些 are 字段都不是,但是有一个回传的 articles 属性包含一系列文章。您可能希望对其进行索引并获得所需的属性。



更新



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

  //获取城市和日期从请求
中让search = req.body.queryResult.parameters ['search']; //城市是必需的参数

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

  callNewsApi('search')。然后((输出)=> {

它将搜索搜索一词,我



您表示转到捕获部分,这表示通话中出了点问题。在捕获部分中未显示任何日志记录,并且记录抛出的异常可能很有用,所以您知道为什么将其发送到catch部分。

 })。catch((xx)=> {
console.error(xx);
res.json({'fulfillmentText':`我不知道了新闻,但我希望这很好!`});
});

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


I am new to dialogflow fulfillment and I am trying to retrieve news from news API based on user questions. I followed documentation provided by news API, but I am not able to catch any responses from the search results, when I run the function in console it is not errors. I changed the code and it looks like now it is reaching to the newsapi endpoint but it is not fetching any results. I am utilizing https://newsapi.org/docs/client-libraries/node-js to make a request to search everything about the topic. when I diagnoise the function it says " Webhook call failed. Error: UNAVAILABLE. "

'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);
       
                       }
   });  
  

}

Also here is RAW API response

{
  "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": {}
    }
  }
} 

And Here is fulfillment request:

{
  "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": {}
    }
  }
}

Also here is the screenshot from the firebase console.

Can anyone guide me what is that I am missing in here?

解决方案

The key is the first three lines in the error message:

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'

It is saying that the newsapi module couldn't be loaded and that the most likely cause of this is that you didn't list this as a dependency in your package.json file.

If you are using the Dialogflow Inline Editor, you need to select the package.json tab and add a line in the dependencies section.

Update

It isn't clear exactly when/where you're getting the "UNAVAILABLE" error, but one likely cause if you're using Dialogflow's Inline Editor is that it is using the Firebase "Spark" pricing plan, which has limitations on network calls outside Google's network.

You can upgrade to the Blaze plan, which does require a credit card on file, but does include the Spark plan's free tier, so you shouldn't incur any costs during light usage. This will allow for network calls.

Update based on TypeError: Cannot read property '0' of undefined

This indicates that either a property (or possibly an index of a property) is trying to reference against something that is undefined.

It isn't clear which line, exactly, this may be, but these lines all are suspicious:

    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];

since they are all referencing a property. I would check to see exactly what comes back and gets stored in response. For example, could it be that there is no "data" or "author" field in what is sent back?

Looking at https://newsapi.org/docs/endpoints/everything, it looks like none of these are fields, but that there is an articles property sent back which contains an array of articles. You may wish to index off that and get the attributes you want.

Update

It looks like that, although you are loading the parameter into a variable with this line

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

You don't actually use the search variable anywhere. Instead, you seem to be passing a literal string "search" to your function with this line

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

which does a search for the word "search", I guess.

You indicated that "it goes to the catch portion", which indicates that something went wrong in the call. You don't show any logging in the catch portion, and it may be useful to log the exception that is thrown, so you know why it is going to the catch portion. Something like

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

is normal, but since it looks like you're logging it in the .on('error') portion, showing that error might be useful.

这篇关于Dialogflow实现Webhook调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆