首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对话流实现webhook调用失败

对话流实现webhook调用失败
EN

Stack Overflow用户
提问于 2020-01-04 00:30:57
回答 2查看 7.2K关注 0票数 2

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

代码语言:javascript
运行
复制
'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);
       
                       }
   });  
  

}

这里还有原始的API响应

代码语言:javascript
运行
复制
{
  "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": {}
    }
  }
} 

下面是履行请求:

代码语言:javascript
运行
复制
{
  "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控制台的屏幕截图。

有没有人能给我指点一下,我这里遗漏了什么?

EN

回答 2

Stack Overflow用户

发布于 2020-01-05 04:50:28

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

代码语言:javascript
运行
复制
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部分中添加一行。

更新

目前还不清楚你何时何地收到了“不可用”的错误,但如果你使用Dialogflow的内联编辑器,一个可能的原因是它使用的是Firebase "Spark“定价方案,该方案对Google网络以外的网络呼叫有限制。

你可以使用upgrade to the Blaze plan,这确实需要一张信用卡在文件上,但确实包括了Spark计划的免费级别,所以你不应该在少量使用期间产生任何费用。这将允许网络调用。

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

这表示某个属性(或某个属性的索引)正在尝试引用未定义的内容。

还不清楚这到底是哪一行,但这些行都是可疑的:

代码语言:javascript
运行
复制
    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中。例如,会不会在发回的内容中没有"data“或"author”字段?

查看https://newsapi.org/docs/endpoints/everything,这些看起来都不是字段,但有一个返回的articles属性,其中包含一组文章。您可能希望将其编入索引,并获得所需的属性。

更新

它看起来就像这样,尽管您使用下面这一行将参数加载到变量中

代码语言:javascript
运行
复制
// Get the city and date from the request
let search = req.body.queryResult.parameters['search'];// city is a required param

实际上,您并没有在任何地方使用search变量。相反,您似乎使用下面这一行将文字字符串"search“传递给了您的函数

代码语言:javascript
运行
复制
callNewsApi('search').then((output) => {

我猜它会搜索“搜索”这个词。

您指出"it to to the catch call“,这表明调用过程中出现了错误。您没有在catch部分中显示任何日志记录,记录抛出的异常可能很有用,这样您就可以知道为什么它会转到catch部分。就像这样

代码语言:javascript
运行
复制
}).catch((xx) => {
  console.error(xx);
  res.json({ 'fulfillmentText': `I don't know the news but I hope it's good!` });
});

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

票数 2
EN

Stack Overflow用户

发布于 2020-04-10 06:42:58

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59582045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档