gpt4 book ai didi

dialogflow-es - 带有 Webhook 客户端 : Error agent. getContext 的 DialogFlow 不是 IncomingMessage.resp 中的函数

转载 作者:行者123 更新时间:2023-12-04 15:54:56 26 4
gpt4 key购买 nike

我正在使用 Firebase 云功能开发一个 DialogFlow 项目。我向我的 API 发送 GET 请求并从服务器接收回数据,然后将其显示在 fulfillment 文本属性上。没关系。

现在我需要将来自服务器的 cookie session ID 存储在一种“应用程序变量”中,以便跟踪客户端和服务器之间的交互。我尝试将 cookie session 保存在 Context 对象中,这样做时出现错误:

Agent.getContext 不是 IncomingMessage.resp 中的函数

代码如下:

exports.dialogflowFirebaseFulfillment = functions.https.onRequest( ( req, resp ) =>{

const agent = new WebhookClient( {request: req, response: resp} );

//SET CONTEXT HERE

const context = {
'name': 'sessione',
'lifespan': 2,
'parameters': {'idSessione': c.sessionID}
};
agent.setContext( context );

if( context ){
console.log( '*--*****************-- name context =' + context.name );
}

//...some stuff etc-

//call my function to consume API

callAVA( strRicerca ).then( ( strOutput ) =>{

return resp.json( {'fulfillmentText': strOutput} );
} ).catch( ( error ) =>{

return resp.json( {'fulfillmentText': 'error error'} );

} );
} );

// added parameter Agent-
function callAVA( agent, strRicerca ){
return new Promise( ( resolve, reject ) =>{

https.get( options, ( resp ) =>{

//variable options contains set cookies values , host name etc-

let data = '';

// A chunk of data has been recieved.
resp.on( 'data', ( chunk ) =>{
data += chunk;

} );

// The whole response has been received. Print out the result.
resp.on( 'end', () =>{

let c = JSON.parse( data );

let strOutput = c.output[0].output;
console.log( '----- ID DELLA SESSIONE : ' + c.sessionID );
if( c.sessionID ){

// I NEED TO SAVE IDSESSION FROM SERVER -

let context = agent.getContext( 'sessione' );

// GOT ERROR: agent.getContext is not a function at IncomingMessage.resp (????)


context.parameters.idSessione = c.sessionID;
}


resolve( strOutput );

//DO OTHER STUFF...
} );

resp.on( "error", ( err ) =>{
console.log( "Error: " + err.message );

reject( err.message );
} );

} );
} );
}

有没有其他方法可以跟踪向服务器发送请求的用户?我宁愿在不访问数据库的情况下保持简单......非常感谢,任何帮助将不胜感激。 :)

____________________________________ 编辑__________________________________暂时搁置上下文,我意识到我遗漏了一些要点,所以我已经根据您的建议修改了我的代码如下:

const functions = require('firebase-functions');
const querystring = require('querystring');
const {WebhookClient} = require('dialogflow-fulfillment');
const https = require('http');


postData = querystring.stringify({
'searchText': 'ciao',
'user':'',
'pwd':'',
'ava':'FarmaInfoBot'

});
const options = {
hostname: '86.107.98.69',
port: 8080,
path: ‘/mypath/search_2?searchText=',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'Cookie':'JSESSIONID=' // -> this comes from the server, store it in context, conv.data or what else-> to do
}
};

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, resp) => {

const agent = new WebhookClient({ request: req, response: resp});

let strRicerca='';
if (req.body.queryResult.parameters['searchText']) {
strRicerca=req.body.queryResult.parameters['searchText'];

options.path+=strRicerca+'&user=&pwd=&ava=FarmaInfoBot';
}

/* IS THIS OK NOW TO INVOKE FUNCTION LIKE THIS, NOW THAT I ADDED AGENT ? */
callAVA(agent, strRicerca).then((strOutput)=> {

return resp.json({ 'fulfillmentText': strOutput });
}).catch((error) => {

return resp.json({ 'fulfillmentText': 'error!!!!!!!!!!'});

});



function callAVA(agent, strRicerca) {
return new Promise((resolve, reject) => {


https.get(options, (resp) => {


let data = '';
console.log(`STATUS RESPONSE: ${resp.statusCode}`);
console.log(`HEADERS RESPONSE: ${JSON.stringify(resp.headers)}`);

resp.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
var c=JSON.parse(chunk);

});
resp.on('end', () => {


let c=JSON.parse(data);

let strOutput= agent.add('Static response from Agent'); // c.output[0].output;

console.log(strOutput);


resolve(strOutput);


});
resp.on("error", (err) => {
console.log("Error: " + err.message);

reject(err.message);
});
});

});

}

let intentMap = new Map();
intentMap.set('test_fb', callAVA);
agent.handleRequest(intentMap);
});

现在程序正在运行,但我收到一个空响应,崩溃发生在结束事件中,正在解析 Json 数据。我的最终目标是找到一种方法,将来自服务器的 cookie 保存在一个全局变量中,并在整个对话中保持它。

最佳答案

看来这里有一些问题。

如果您的错误的直接原因是您将 callAVA() 函数定义为

function callAVA( agent, strRicerca )

但是当你在前面几行调用它时,你忽略了 agent 参数:

callAVA( strRicerca )

但是,从长远来看,您使用它的方式还有其他问题会给您带来麻烦。

虽然您正在做的事情可能是有效的,并且可能有效,但它也不同于通常的处理方式,并且您可能会因此导致一些问题。

通常,您创建 agent,然后针对它注册 Intent Handlers。有点像

let intentMap = new Map();
intentMap.set('intent.name.1', intentHandlerFunction1);
intentMap.set('intent.name.2', intentHandlerFunction2);
agent.handleRequest(intentMap);

它将使用单个参数 agent 调用 Intent 名称的匹配函数。 (顺便说一句 - 返回 Promise 做得很好!你的那部分是正确的。)

作为 Intent 处理程序的一部分,您需要在 Promise 完成之前调用 agent.setContext(context) 。在后面的 Intent handler 中,当你想知道这个值时,你需要调用 agent.getContext('context-name') 它应该返回整个 Context 对象,包括剩余的生命周期和参数为了它..

更新

您在第一次编辑中所做的更改还不够。问题是您混合使用两种不同的方式来调用 Intent Handler:

  1. 在一种方式中,您直接使用第二个参数调用它。作为结果的一部分,它将 JSON 发送到 resp 对象。

  2. 换句话说,您已经使用 intentMap.set() 注册了它,这导致它被调用(仅使用 agent 参数)当调用 handleRequest() 并通过 agent 对象设置响应时。

一般来说,除非你想自己操作所有的JSON,否则你应该依赖方法(2)。您应该自己直接调用 Intent Handler 函数,而应该让 handleRequest() 为您处理它。

由于您需要 strRicerca 对象,您可以在函数运行的相同范围内提供它,这似乎已经是这样了。因此您可能不需要将其作为参数包含在内。

关于dialogflow-es - 带有 Webhook 客户端 : Error agent. getContext 的 DialogFlow 不是 IncomingMessage.resp 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52189160/

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