박하늘

app.js : fuzzy string button update

...@@ -10,26 +10,39 @@ const sslport = 23023; ...@@ -10,26 +10,39 @@ const sslport = 23023;
10 10
11 const bodyParser = require('body-parser'); 11 const bodyParser = require('body-parser');
12 var app = express(); 12 var app = express();
13 +
14 +
15 +
16 +
13 app.use(bodyParser.json()); 17 app.use(bodyParser.json());
14 app.post('/hook', function (req, res) { 18 app.post('/hook', function (req, res) {
15 19
16 var eventObj = req.body.events[0]; 20 var eventObj = req.body.events[0];
17 var source = eventObj.source; 21 var source = eventObj.source;
18 -
19 var message = eventObj.message; 22 var message = eventObj.message;
20 - var pystring;
21 -
22 - const spawn = require("child_process").spawn;
23 - const process = spawn("python", ["basic.py", eventObj.message.text]);
24 - const Callback = (data) => {
25 - console.log("Data :", data.toString());
26 - pystring = data.toString();
27 // request log 23 // request log
28 console.log('======================', new Date() ,'======================'); 24 console.log('======================', new Date() ,'======================');
29 console.log('[request]', req.body); 25 console.log('[request]', req.body);
30 console.log('[request source] ', eventObj.source); 26 console.log('[request source] ', eventObj.source);
31 console.log('[request message]', eventObj.message); 27 console.log('[request message]', eventObj.message);
32 28
29 + basicinform(eventObj.replyToken, eventObj.message.text)
30 +
31 + res.sendStatus(200);
32 +
33 +});
34 +
35 +function basicinform(replyToken, message) {
36 +
37 + var pystring;
38 + const spawn = require("child_process").spawn;
39 + const process = spawn("python", ["basic.py", message]);
40 + const Callback = (data) => {
41 + console.log("Data :", data.toString());
42 + pystring = data.toString();
43 + if(pystring[0] == '1')
44 + {
45 + pystring = pystring.replace('1현', '현');
33 request.post( 46 request.post(
34 { 47 {
35 url: TARGET_URL, 48 url: TARGET_URL,
...@@ -37,31 +50,75 @@ app.post('/hook', function (req, res) { ...@@ -37,31 +50,75 @@ app.post('/hook', function (req, res) {
37 'Authorization': `Bearer ${TOKEN}` 50 'Authorization': `Bearer ${TOKEN}`
38 }, 51 },
39 json: { 52 json: {
40 - "replyToken":eventObj.replyToken, 53 + "replyToken":replyToken,
41 "messages":[ 54 "messages":[
42 { 55 {
43 "type":"text", 56 "type":"text",
44 "text":pystring 57 "text":pystring
58 + }
59 + ]
60 + }
61 + },(error, response, body) => {
62 + console.log(body)
63 + });
64 + }
65 + else
66 + {
67 + var candarr = pystring.split('\n')
68 + request.post(
69 + {
70 + url: TARGET_URL,
71 + headers: {
72 + 'Authorization': `Bearer ${TOKEN}`
45 }, 73 },
74 + json: {
75 + "replyToken":replyToken,
76 + "messages":[
46 { 77 {
47 - "type":"text", 78 + "type": "text",
48 - "text":"May I help you?" 79 + "text": pystring,
80 + "quickReply": {
81 + "items": [
82 + {
83 + "type": "action",
84 + "action": {
85 + "type": "message",
86 + "label": candarr[0],
87 + "text": candarr[0]
88 + }
89 + },
90 + {
91 + "type": "action",
92 + "action": {
93 + "type": "message",
94 + "label": candarr[1],
95 + "text": candarr[1]
96 + }
97 + },
98 + {
99 + "type": "action",
100 + "action": {
101 + "type": "message",
102 + "label": candarr[2],
103 + "text": candarr[2]
104 + }
105 + }
106 + ]
107 + }
49 } 108 }
50 ] 109 ]
110 +
51 } 111 }
52 },(error, response, body) => { 112 },(error, response, body) => {
53 console.log(body) 113 console.log(body)
54 }); 114 });
115 + }
55 116
56 117
57 - res.sendStatus(200);
58 118
59 }; 119 };
60 process.stdout.on("data", Callback); 120 process.stdout.on("data", Callback);
61 - 121 +}
62 -
63 -
64 -});
65 122
66 try { 123 try {
67 const option = { 124 const option = {
......