Skip to content

Commit c0a2217

Browse files
committed
整理程式,刪除無意義註解與consoe.log、排版整理,部分命名不一致
1 parent 196856c commit c0a2217

9 files changed

+723
-767
lines changed

FCF-DataCollection.js

+29-30
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
var request = require("request");
2-
module.exports = function (RED) {
32

4-
function Dispatcher(config) {
3+
module.exports = function(RED) {
4+
function DataCollection(config) {
5+
56
RED.nodes.createNode(this, config);
67
var node = this;
78
var context = this.context();
89
node.rules = config.rules;
910
node.collect = config.collect;
1011

11-
this.on('input', function (msg) {
12-
12+
this.on('input', function(msg) {
1313

1414
var rules = node.rules;
1515
var collect = node.collect;
16-
console.log(collect);
1716
var output = [];
1817

1918
if (collect == "query")
@@ -31,18 +30,19 @@ module.exports = function (RED) {
3130
msg.payload = rules[context.get("dataCount")].topic;
3231
context.set("dataCount", context.get("dataCount") - 1);
3332
node.send(output);
34-
} else if (context.get("dataCount") > -1) {
35-
output[0] = msg;
36-
output[1] = null;
37-
var query = context.get("query");
38-
query[rules[context.get("dataCount") + 1].topic2] = msg.payload.content;
39-
context.set("query", query);
40-
41-
msg.payload = rules[context.get("dataCount")].topic;
42-
context.set("dataCount", context.get("dataCount") - 1);
33+
}
34+
else if (context.get("dataCount") > -1) {
35+
output[0] = msg;
36+
output[1] = null;
37+
var query = context.get("query");
38+
query[rules[context.get("dataCount") + 1].topic2] = msg.payload.content;
39+
context.set("query", query);
4340

44-
node.send(output);
45-
} else {
41+
msg.payload = rules[context.get("dataCount")].topic;
42+
context.set("dataCount", context.get("dataCount") - 1);
43+
node.send(output);
44+
}
45+
else {
4646
output[0] = null;
4747
output[1] = msg;
4848
var query = context.get("query");
@@ -69,18 +69,20 @@ module.exports = function (RED) {
6969
msg.payload = rules[context.get("dataCount")].topic;
7070
context.set("dataCount", context.get("dataCount") - 1);
7171
node.send(output);
72-
} else if (context.get("dataCount") > -1) {
73-
output[0] = msg;
74-
output[1] = null;
75-
var userData = context.get("userData");
76-
userData[rules[context.get("dataCount") + 1].topic2] = msg.payload.content;
77-
context.set("userData", userData);
72+
}
73+
else if (context.get("dataCount") > -1) {
74+
output[0] = msg;
75+
output[1] = null;
76+
var userData = context.get("userData");
77+
userData[rules[context.get("dataCount") + 1].topic2] = msg.payload.content;
78+
context.set("userData", userData);
7879

79-
msg.payload = rules[context.get("dataCount")].topic;
80-
context.set("dataCount", context.get("dataCount") - 1);
80+
msg.payload = rules[context.get("dataCount")].topic;
81+
context.set("dataCount", context.get("dataCount") - 1);
8182

82-
node.send(output);
83-
} else {//輸出第二個分支
83+
node.send(output);
84+
}
85+
else {
8486
output[0] = null;
8587
output[1] = msg;
8688
var userData = context.get("userData");
@@ -90,10 +92,7 @@ module.exports = function (RED) {
9092
context.set("dataCount", null);
9193
node.send(output);
9294
}
93-
94-
9595
});
9696
}
97-
98-
RED.nodes.registerType('FCF-DataCollection', Dispatcher);
97+
RED.nodes.registerType('FCF-DataCollection', DataCollection);
9998
};

FCF-Dispatcher.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
var request = require("request");//會去找node_module
2-
module.exports = function (RED) {
1+
var request = require("request");
2+
3+
module.exports = function(RED) {
4+
function Dispatcher(config) {
35

4-
function Dispatcher(config) {//這裡開始會把使用者的query送到api.ai做intent判斷,我們根據intent的action來做處理
56
RED.nodes.createNode(this, config);
67
var node = this;
78
node.token = config.token;
89
node.rules = config.rules;
9-
//官方的寫法是node.on(.........
10-
this.on('input', function (msg) {
10+
11+
this.on('input', function(msg) {
1112
var rules = node.rules;
1213
var token = encodeURIComponent(node.token);
1314
var output = [];
1415
var matched = false;
1516
var action;
16-
console.log("使用者的query="+msg.payload.content);//msg.payload.content是使用者輸入的語句,如:我想去香米湯湯
17-
request({// request node.js才有的東西
17+
request({
1818
uri: "https://api.api.ai/v1/query?v=20150910",
1919
method: "POST",
2020
headers: {
@@ -28,27 +28,21 @@ module.exports = function (RED) {
2828
"lang": "zh",
2929
"sessionId": "1234567890"
3030
})
31-
}, function (error, response, body) {//最好要去做error例外處理,要去看error是不是200,其他400就要去另外處理
32-
var action = JSON.parse(body);//body是api.ai回應的body?
33-
console.log("Dispatcher:API.AI回應的body=");
34-
console.log(action);
35-
rules.forEach(function (rule) {//??????????
36-
if ((action.result.action).toString() == (rule.topic).toString()) {//如果api.ai回傳的action與我在面板輸入的action值一樣
37-
matched = true;//????
38-
if ((action.result.action).toString() == "input.unknown")//????
31+
}, function(error, response, body) {
32+
var action = JSON.parse(body);
33+
rules.forEach(function(rule) {
34+
if ((action.result.action).toString() == (rule.topic).toString()) {
35+
matched = true;
36+
if ((action.result.action).toString() == "input.unknown")
3937
msg.payload = action.result.fulfillment.speech;
4038
output.push(msg);
4139
} else {
4240
output.push(null);
4341
}
4442
});
4543
node.send(output);
46-
4744
});
48-
49-
5045
});
5146
}
52-
53-
RED.nodes.registerType('FCF-Dispatcher', Dispatcher);//第二個參數Dispatcher是要映射到這裡的參數名稱
47+
RED.nodes.registerType('FCF-Dispatcher', Dispatcher);
5448
};

0 commit comments

Comments
 (0)