Node.js 需要在nodejs中连接不同服务器中的文件
需要在一台服务器上连接index.html以运行不同的服务器: 一台服务器中的index.html:Node.js 需要在nodejs中连接不同服务器中的文件,node.js,google-cloud-platform,dialogflow-es,Node.js,Google Cloud Platform,Dialogflow Es,需要在一台服务器上连接index.html以运行不同的服务器: 一台服务器中的index.html: <script> const chatux = new ChatUx(); const opt = { bot: { wakeupText: 'Hi',//user input which is automatically send to server on startup botPhoto: 'images/bot.png
<script>
const chatux = new ChatUx();
const opt = {
bot: {
wakeupText: 'Hi',//user input which is automatically send to server on startup
botPhoto: 'images/bot.png',//URL of bot photo image
humanPhoto: 'images/human.png',//URL of human photo image
widget: {
sendLabel: 'Send',
placeHolder: 'Say something'
}
},
api: {
endpoint: 'https://test7.domain.org:3000/chat',//chat server
method: 'GET',//HTTP METHOD when requesting chat server
dataType: 'json',//json or jsonp is available
params:{'country':'ca', 'sessionID':randomString()}
},
window: {
title: 'AURA - The Art of Living Assistant',
size: {
width: 350,
height: 500,
minWidth: 300,
minHeight: 300,
titleHeight: 50,
}, appearance: {
border: {
shadow: '2px 2px 10px rgba(0, 0, 0, 0.5)',
width: 0,
radius: 6
},
titleBar: {
fontSize: 16,
color: '#888888',
background: '#ffffff',
leftMargin: 40,
height: 40,
buttonWidth: 36,
buttonHeight: 16,
buttonColor: 'white',
buttons: [
{
fa: 'fas fa-times',
name: 'hideButton',
visible: true
}
],
buttonsOnLeft: [
{
fa: 'fas fa-comment-alt',
name: 'info',
visible: true
}
],
},
}
},
wakeupButton: {
right: 20,
bottom: 20,
size: 60,
fontSize: 25
},
methods: {
onChatWindowCreate: (win) => {
console.log('#onChatWindowCreate');
},
onChatWindowPause: (win) => {
console.log('#onChatWindowPause');
},
onChatWindowResume: (win) => {
console.log('#onChatWindowResume');
},
onUserInput: (userInputText) => {
console.log('#onUserInput userInputText=' + userInputText);
if (userInputText === 'end') {
const consumed = true;
chatux.dispose();
return consumed;
}
},
onServerResponse: (response) => {
console.log('#onServerResponse response=' + JSON.stringify(response));
return response;
}
}
};
//initialize
chatux.init(opt);
chatux.start(true);
</script>
运行节点app.js时
在test7服务器中,它开始运行,
但是我无法从test7获取对ites3的响应
从test7向ites3发送数据,反之亦然。您的test7服务器从哪里运行?它是从云计算运行的吗?应用引擎?虚拟机?另外,您是否可以尝试在不指定端口的情况下从index.html发出GET请求,例如
端点:'https://test7.domain.org/chat“
这是虚拟机,已经试过了。不起作用如果你认为这是谷歌方面的问题,请提供您的项目id以及所有必要步骤和最低限度的工作示例,以便分配给您的问题的工程师彻底调查并尝试诊断此问题。
const dialogflow = require('dialogflow');
const uuid = require('uuid');
const structjson = require('structjson');
var express = require('express');
var app = express();
const hostname = 'test7.artofliving.org';
const port = 3000;
const projectId = 'chatbot-demo-oeujqg'
const keyFilename = 'public/credentials.json'
// enabling CORS
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '*');
next();
});
app.use(express.static('https://ites3.domain.org/sites/all/themes/uniss'));
app.get('/chat', function(req, res) {
console.log("SESSION ID "+ req.query.sessionID);
var sessionId = req.query.sessionID;
const query = req.query.text;
const country = req.query.country;
invokeDialogflow(query, res, sessionId, country);
});
// what port to run server on
app.listen(3000, function() {
console.log('server started on port 3000');
});
/**
* Send a query to the dialogflow agent, and return the query result.
* @param {string} projectId The project to be used
*/
async function invokeDialogflow(query, res, sessionId, country) {
// A unique identifier for the given session
// Create a new session
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryParams: {
"payload": structjson.jsonToStructProto({"country":country})
},
queryInput: {
text: {
// The query to send to the dialogflow agent
text: query,
// The language used by the client (en-US)
languageCode: 'en-US',
},
}
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log('Detected intent');
const result = responses[0].queryResult;
// console.log(JSON.stringify(result));
const fulfilmenText = result.fulfillmentMessages;
var fulfillmentMsg1 = result.fulfillmentMessages[1];
if (fulfillmentMsg1 != null) {
var jsonPayload = structjson.structProtoToJson(fulfillmentMsg1.payload);
res.json(jsonPayload);
// console.log(jsonPayload);
} else if (result.webhookPayload != null) {
var jsonPayload = structjson.structProtoToJson(result.webhookPayload);
console.log('WEBHOOK PAYLOAD');
res.json(jsonPayload);
// console.log(jsonPayload);
} else if (result.fulfillmentMessages[0] != null) {
var fulfillmentMsg0 = result.fulfillmentMessages[0];
if (fulfillmentMsg0.text != null) {
var fulfillmentText = result.fulfillmentMessages[0].text.text;
const response = {
output: []
};
const msg = response.output;
msg.push({
type: 'text',
value: '' + fulfillmentText + ''
});
res.json(response);
} else {
var jsonPayload = structjson.structProtoToJson(fulfillmentMsg0.payload);
res.json(jsonPayload);
}
}
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
}