김시환

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

const express= require('express');
const app = express();
var request = require('request');
//필요한 주소 정보 (data 형식 확정 x )
......@@ -6,11 +8,11 @@ let address = {
"number" : 10,
"hospital_data" :[
{"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470},
{"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708}
{"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708},
{"name" : "병원명", "address" : "병원 주소", "x" : 126.1058342, "y" : 37.359708}
]
}
var options = {
'method': 'GET',
'url': 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+address.current_address.x+','
......@@ -21,6 +23,8 @@ var options = {
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
let body = JSON.parse(response.body);
......
const express= require('express');
const app = express();
const request = require('request')
const ID = '12rhzhzq7g';
const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI';
app.post('/direction',function(req,res){
let data_body = req.body;
var num = data_body.number;
let count = 0;
while (count<num){
const _url = 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+data_body.address.current_address.x+','
+data_body.address.current_address.y+'&goal='+data_body.hospital_data[count].x+','+data_body.hospital_data[count].y+'&option=trafast';
}
request.post(
{
method : 'GET',
url : _url,
headers : {
'X-NCP-APIGW-API-KEY-ID': ID,
'X-NCP-APIGW-API-KEY': KEY
}
},(error, res, body) => {
let body = JSON.parse(res.body);
var distance = body.route.trafast[0].summary.distance/1000; // km 단위
var duration = body.route.trafast[0].summary.duration/1000/60; // 분 단위dy
data_body.hospital_data[count].distance = distance;
data_body.hospital_data[count].duration = duration;
}
);
count += 1;
res.send(data_body)
}
)
......@@ -10,36 +10,25 @@ const domain = "2019102158.oss2022chatbot.tk"
const sslport = 23023;
const bodyParser = require('body-parser');
const app = express();
var event_time =1
function main(eventObj,res){
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
'Authorization': `Bearer ${TOKEN}`,
},
json: {
"replyToken":eventObj.replyToken, //eventObj.replyToken
"messages":[
{
"type": "text", // ①
"text": "응급 상황인가요?",
"quickReply": { // ②
"items": [
{
"type": "action", // ③
"action": {
"type": "postback",
"label": "네",
"data": "action=two",
"displayText": "네",
"inputOption": "openKeyboard",
}
},
]
}
}
],
"type": "text", // ①
"text": "응급 상황인가요?"},
{"type": "text",
"text": "현재있는 위치의 주소나 보이는 곳을 입력하세요."},
],
}
},(error, response, body) => {
......@@ -49,23 +38,51 @@ res.sendStatus(200);
}
function find_current(eventObj,res){ //Two
console.log(Address.getAddress(eventObj.message.text))
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
'Authorization': `Bearer ${TOKEN}`,
},
json: {
"replyToken":eventObj.replyToken, //eventObj.replyToken
"messages":[
{
"type": "text", // ①
"text": "응급 상황인가요?",
"type": "text", // ①
"text": "현재있는 위치의 주소나 보이는 곳을 입력하세요.",
{
"type": "location",
"title": "현재위치",
"address": "경기 안산시 단원구 석수동길",
"latitude": 37.3446767467006,
"longitude": 126.807581282114
},
{
"type": "template",
"altText": "This is a buttons template",
"template": {
"type": "buttons",
"text": "이곳이 맞나요?",
"defaultAction": {
"type": "uri",
"label": "View detail",
"uri": "http://example.com/page/123"
},
"actions": [
{
"type": "postback",
"label": "네",
"data": "action=buy&itemid=123"
},
{
"type": "postback",
"label": "아니요",
"data": "action=add&itemid=123"
},
]
}
}
],
],
}
},(error, response, body) => {
......@@ -78,16 +95,18 @@ res.sendStatus(200);
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
var headers = req.headers;
console.log('======================', new Date() ,'======================');
console.log(eventObj)
if(eventObj.postback){
find_current(eventObj,res)
if(event_time==1){
main(eventObj,res)
event_time=2
}
else{
else if (event_time ==2){
find_current(eventObj,res)
event_time =3
}
else if (event_time ==3){
}
});
......
const express= require('express');
const app = express();
var request = require('request');
//필요한 주소 정보 (data 형식 확정 x )
let addresses = {
"current_address" : { "address" : "현재 위치 주소", "x" : 127.1058342, "y" : 37.359708},
"number" : 10,
"hospital_data" :[
{"name" : "병원명", "address" : "병원 주소", "x" : 129.075986, "y" : 35.179470, "distance" : 0, "duration" : 0},
{"name" : "병원명", "address" : "병원 주소", "x" : 127.1058342, "y" : 37.359708, "distance" : 0, "duration" : 0}
]
}
var options = {
'method': 'GET',
'url': 'https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start='+address.current_address.x+','
+address.current_address.y+'&goal='+address.hospital_data[0].x+','+address.hospital_data[0].y+'&option=trafast',
'headers': {
'X-NCP-APIGW-API-KEY-ID': '12rhzhzq7g',
'X-NCP-APIGW-API-KEY': 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI'
}
};
const ID = '12rhzhzq7g';
const KEY = 'FhD45P91TxG2820MadrsiPOUjI6bQMJhddnHZIeI';
function direction(data){
var num = data.number;
for (let i = 0; i< num;i++){
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';
options.url = _url;
request(options,function(error, respose){
var databody = JSON.parse(respose.body);
var distance = databody.route.trafast[0].summary.distance/1000; // km 단위
var duration = databody.route.trafast[0].summary.duration/1000/60; // 분 단위dy
data.hospital_data[count].distance = distance;
data.hospital_data[count].duration = duration;
}
)
}
return data}
console.log(direction(address))
\ No newline at end of file