gpt4 book ai didi

javascript - Dialogflow Fulfillment webhook 调用失败

转载 作者:行者123 更新时间:2023-12-04 10:48:45 25 4
gpt4 key购买 nike

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
谁能指导我在这里缺少什么?

最佳答案

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

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

更新

目前尚不清楚您何时/何地收到“不可用”错误,但如果您使用 Dialogflow 的内联编辑器,一个可能的原因是它使用 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 ,看起来这些都不是字段,但有一个 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')部分,表明该错误可能有用。

关于javascript - Dialogflow Fulfillment webhook 调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59582045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com