gpt4 book ai didi

node.js - DialogFlow中如何接收Kommunicate发送的数据?

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:23 25 4
gpt4 key购买 nike

我正在为我的网站创建一个聊天机器人。因此,我使用 DialogFlowKommunicate.io 来实现同样的目的。我在我的 html 文件中添加了 kommunicate 给出的脚本。为了将自定义数据从 html 页面发送到机器人,我们在 "onInit" 函数中使用 chatContext 变量,但我无法了解如何在我的对话流机器人中使用该数据。这是我的通讯脚本:

(function(d, m){
var kommunicateSettings = {"appId":"7519ee060abee2b532e8565aa0527aed","popupWidget":true,"automaticChatOpenOnNavigation":true,
"title": "test",
"appSettings": {
"chatWidget": {
"popup": true, // To enable or disable the pre-chat popup (boolean)
//"emojilibrary" : true
},
"chatPopupMessage": [{
"message": "Wanna ask something related to "+document.title+ "?", // Message to be displayed in the pre-chat popup (string)
"delay": 3000 // Delay interval of pre-chat popup (number in milliseconds)
}],
},
"onInit" : function(){
var chatContext = {
"lat":"Latitude° N",
"lon":"Longitude° E"
}
Kommunicate.updateChatContext(chatContext);
}
};

var s = document.createElement("script"); s.type = "text/javascript"; s.async = true;
s.src = "https://widget.kommunicate.io/v2/kommunicate.app";
var h = document.getElementsByTagName("head")[0]; h.appendChild(s);
window.kommunicate = m; m._globals = kommunicateSettings;
})(document, window.kommunicate || {});

这是我的 DialogFlow 代码:

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Payload} =require('dialogflow-fulfillment');
const i18n= require('i18n');
const express=require('express');
var router=express.Router();

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements


exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

function welcome(agent) {
agent.add(`Welcome to my agent!`);
}

function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}

function About(agent){
agent.add(`We are a company!`);
}

// // Uncomment and edit to make your own intent handler
// // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
// // below to get this function to be run when a Dialogflow intent is matched
// function yourFunctionHandler(agent) {
// agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
// agent.add(new Card({
// title: `Title: this is a card title`,
// imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
// text: `This is the body text of a card. You can even use line\n breaks and emoji! 💁`,
// buttonText: 'This is a button',
// buttonUrl: 'https://assistant.google.com/'
// })
// );
// agent.add(new Suggestion(`Quick Reply`));
// agent.add(new Suggestion(`Suggestion`));
// agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
// }

// // Uncomment and edit to make your own Google Assistant intent handler
// // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
// // below to get this function to be run when a Dialogflow intent is matched
// function googleAssistantHandler(agent) {
// let conv = agent.conv(); // Get Actions on Google library conv instance
// conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
// agent.add(conv); // Add Actions on Google library responses to your agent's response
// }
// // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
// // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('About the company', About);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});

我想在欢迎消息中显示“lat”和“lon”。

最佳答案

可以从您的履行代码访问您从 Kommunicate 聊天小部件传入的 chatContext 对象中的数据。此数据作为 originalDetectIntentRequest 参数的一部分在请求对象中提供。

这是示例:

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {

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

console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));

console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

console.log('Data passed as chatContext from Kommunicate is: ' + JSON.stringify(request.body.originalDetectIntentRequest) );

.
.
.
.
.


});

关于node.js - DialogFlow中如何接收Kommunicate发送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59854170/

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