김시환

Merge branch 'sihwan' of http://khuhub.khu.ac.kr/2019102158/emergency_room_ChatBot into sihwan

1 +const express= require('express');
2 +const app = express();
1 var request = require('request'); 3 var request = require('request');
2 4
3 //필요한 주소 정보 (data 형식 확정 x ) 5 //필요한 주소 정보 (data 형식 확정 x )
...@@ -6,11 +8,11 @@ let address = { ...@@ -6,11 +8,11 @@ let address = {
6 "number" : 10, 8 "number" : 10,
7 "hospital_data" :[ 9 "hospital_data" :[
8 {"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470}, 10 {"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470},
9 - {"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708} 11 + {"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708},
12 + {"name" : "병원명", "address" : "병원 주소", "x" : 126.1058342, "y" : 37.359708}
10 ] 13 ]
11 } 14 }
12 15
13 -
14 var options = { 16 var options = {
15 'method': 'GET', 17 'method': 'GET',
16 'url': 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+address.current_address.x+',' 18 'url': 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+address.current_address.x+','
...@@ -21,6 +23,8 @@ var options = { ...@@ -21,6 +23,8 @@ var options = {
21 } 23 }
22 }; 24 };
23 25
26 +
27 +
24 request(options, function (error, response) { 28 request(options, function (error, response) {
25 if (error) throw new Error(error); 29 if (error) throw new Error(error);
26 let body = JSON.parse(response.body); 30 let body = JSON.parse(response.body);
......
1 +const express= require('express');
2 +const app = express();
3 +const request = require('request')
4 +
5 +const ID = '12rhzhzq7g';
6 +const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI';
7 +
8 +app.post('/direction',function(req,res){
9 + let data_body = req.body;
10 + var num = data_body.number;
11 + let count = 0;
12 +
13 +
14 + while (count<num){
15 + const _url = 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+data_body.address.current_address.x+','
16 + +data_body.address.current_address.y+'&goal='+data_body.hospital_data[count].x+','+data_body.hospital_data[count].y+'&option=trafast';
17 + }
18 + request.post(
19 + {
20 + method : 'GET',
21 + url : _url,
22 + headers : {
23 + 'X-NCP-APIGW-API-KEY-ID': ID,
24 + 'X-NCP-APIGW-API-KEY': KEY
25 + }
26 + },(error, res, body) => {
27 + let body = JSON.parse(res.body);
28 + var distance = body.route.trafast[0].summary.distance/1000; // km 단위
29 + var duration = body.route.trafast[0].summary.duration/1000/60; // 분 단위dy
30 + data_body.hospital_data[count].distance = distance;
31 + data_body.hospital_data[count].duration = duration;
32 + }
33 + );
34 + count += 1;
35 + res.send(data_body)
36 + }
37 +)
...@@ -10,36 +10,25 @@ const domain = "2019102158.oss2022chatbot.tk" ...@@ -10,36 +10,25 @@ const domain = "2019102158.oss2022chatbot.tk"
10 const sslport = 23023; 10 const sslport = 23023;
11 const bodyParser = require('body-parser'); 11 const bodyParser = require('body-parser');
12 const app = express(); 12 const app = express();
13 - 13 +var event_time =1
14 function main(eventObj,res){ 14 function main(eventObj,res){
15 request.post( 15 request.post(
16 { 16 {
17 url: TARGET_URL, 17 url: TARGET_URL,
18 headers: { 18 headers: {
19 - 'Authorization': `Bearer ${TOKEN}` 19 + 'Authorization': `Bearer ${TOKEN}`,
20 }, 20 },
21 json: { 21 json: {
22 "replyToken":eventObj.replyToken, //eventObj.replyToken 22 "replyToken":eventObj.replyToken, //eventObj.replyToken
23 "messages":[ 23 "messages":[
24 { 24 {
25 - "type": "text", // ① 25 + "type": "text", // ①
26 - "text": "응급 상황인가요?", 26 + "text": "응급 상황인가요?"},
27 - "quickReply": { // ② 27 + {"type": "text",
28 - "items": [ 28 + "text": "현재있는 위치의 주소나 보이는 곳을 입력하세요."},
29 - { 29 +
30 - "type": "action", // ③ 30 +
31 - "action": { 31 + ],
32 - "type": "postback",
33 - "label": "네",
34 - "data": "action=two",
35 - "displayText": "네",
36 - "inputOption": "openKeyboard",
37 - }
38 - },
39 - ]
40 - }
41 - }
42 - ],
43 } 32 }
44 },(error, response, body) => { 33 },(error, response, body) => {
45 34
...@@ -49,23 +38,51 @@ res.sendStatus(200); ...@@ -49,23 +38,51 @@ res.sendStatus(200);
49 } 38 }
50 39
51 function find_current(eventObj,res){ //Two 40 function find_current(eventObj,res){ //Two
41 + console.log(Address.getAddress(eventObj.message.text))
52 request.post( 42 request.post(
53 { 43 {
54 url: TARGET_URL, 44 url: TARGET_URL,
55 headers: { 45 headers: {
56 - 'Authorization': `Bearer ${TOKEN}` 46 + 'Authorization': `Bearer ${TOKEN}`,
57 }, 47 },
58 json: { 48 json: {
59 "replyToken":eventObj.replyToken, //eventObj.replyToken 49 "replyToken":eventObj.replyToken, //eventObj.replyToken
60 "messages":[ 50 "messages":[
61 - { 51 + {
62 - "type": "text", // ① 52 + "type": "location",
63 - "text": "응급 상황인가요?", 53 + "title": "현재위치",
64 - "type": "text", // ① 54 + "address": "경기 안산시 단원구 석수동길",
65 - "text": "현재있는 위치의 주소나 보이는 곳을 입력하세요.", 55 + "latitude": 37.3446767467006,
66 - 56 + "longitude": 126.807581282114
57 + },
58 + {
59 + "type": "template",
60 + "altText": "This is a buttons template",
61 + "template": {
62 + "type": "buttons",
63 + "text": "이곳이 맞나요?",
64 + "defaultAction": {
65 + "type": "uri",
66 + "label": "View detail",
67 + "uri": "http://example.com/page/123"
68 + },
69 + "actions": [
70 + {
71 + "type": "postback",
72 + "label": "네",
73 + "data": "action=buy&itemid=123"
74 + },
75 + {
76 + "type": "postback",
77 + "label": "아니요",
78 + "data": "action=add&itemid=123"
79 + },
80 + ]
81 + }
67 } 82 }
68 - ], 83 +
84 +
85 + ],
69 } 86 }
70 },(error, response, body) => { 87 },(error, response, body) => {
71 88
...@@ -78,16 +95,18 @@ res.sendStatus(200); ...@@ -78,16 +95,18 @@ res.sendStatus(200);
78 app.use(bodyParser.json()); 95 app.use(bodyParser.json());
79 app.post('/hook', function (req, res) { 96 app.post('/hook', function (req, res) {
80 var eventObj = req.body.events[0]; 97 var eventObj = req.body.events[0];
98 + var headers = req.headers;
81 console.log('======================', new Date() ,'======================'); 99 console.log('======================', new Date() ,'======================');
82 - console.log(eventObj) 100 + if(event_time==1){
83 - if(eventObj.postback){ 101 + main(eventObj,res)
84 - 102 + event_time=2
85 - find_current(eventObj,res)
86 -
87 -
88 } 103 }
89 - else{ 104 + else if (event_time ==2){
90 find_current(eventObj,res) 105 find_current(eventObj,res)
106 + event_time =3
107 + }
108 + else if (event_time ==3){
109 +
91 } 110 }
92 111
93 }); 112 });
......
1 +const express= require('express');
2 +const app = express();
3 +var request = require('request');
4 +
5 +//필요한 주소 정보 (data 형식 확정 x )
6 +let addresses = {
7 + "current_address" : { "address" : "현재 위치 주소", "x" : 127.1058342, "y" : 37.359708},
8 + "number" : 10,
9 + "hospital_data" :[
10 + {"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470, "distance" : 0, "duration" : 0},
11 + {"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708, "distance" : 0, "duration" : 0}
12 + ]
13 +}
14 +
15 +var options = {
16 + 'method': 'GET',
17 + 'url': 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+address.current_address.x+','
18 + +address.current_address.y+'&goal='+address.hospital_data[0].x+','+address.hospital_data[0].y+'&option=trafast',
19 + 'headers': {
20 + 'X-NCP-APIGW-API-KEY-ID': '12rhzhzq7g',
21 + 'X-NCP-APIGW-API-KEY': 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'
22 + }
23 +};
24 +
25 +const ID = '12rhzhzq7g';
26 +const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI';
27 +
28 +function direction(data){
29 + var num = data.number;
30 + for (let i = 0; i< num;i++){
31 + const _url = 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+data.current_address.x+','+data.current_address.y+'&goal='+data.hospital_data[i].x+','+data.hospital_data[i].y+'&option=trafast';
32 + options.url = _url;
33 +
34 + request(options,function(error, respose){
35 + var databody = JSON.parse(respose.body);
36 + var distance = databody.route.trafast[0].summary.distance/1000; // km 단위
37 + var duration = databody.route.trafast[0].summary.duration/1000/60; // 분 단위dy
38 + data.hospital_data[count].distance = distance;
39 + data.hospital_data[count].duration = duration;
40 + }
41 + )
42 + }
43 + return data}
44 +console.log(direction(address))
...\ No newline at end of file ...\ No newline at end of file