김연우

modify main.js

1 const express = require('express'); 1 const express = require('express');
2 const request = require('request'); 2 const request = require('request');
3 -const talk = require('./talk')
4 3
5 const fs = require('fs'); 4 const fs = require('fs');
6 const path = require('path'); 5 const path = require('path');
7 const HTTPS = require('https'); 6 const HTTPS = require('https');
8 const bodyParser = require('body-parser'); 7 const bodyParser = require('body-parser');
9 8
10 -// var latitude = 37.2429832;
11 -// var longitude = 127.0749535;
12 -// var locationAdd = "경기 용인시 기흥구 서그내로49번길 13"
13 -// var location_name = "카페 서천"
14 -
15 -
16 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 9 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
17 const tokens = JSON.parse(fs.readFileSync("setting.json")) 10 const tokens = JSON.parse(fs.readFileSync("setting.json"))
18 const sslport = 23023; 11 const sslport = 23023;
...@@ -25,6 +18,10 @@ var first = false; //첫 시도인지 ...@@ -25,6 +18,10 @@ var first = false; //첫 시도인지
25 var second = false; //첫번째 분류 선택했는지 18 var second = false; //첫번째 분류 선택했는지
26 var destCar = ""; 19 var destCar = "";
27 var destination = []; 20 var destination = [];
21 +const canvas = require('./khcanvas')
22 +const selector = require('./schedule_selector')
23 +const weather = require('./weather')
24 +
28 25
29 function sendText(replyToken, messages) { 26 function sendText(replyToken, messages) {
30 request.post( 27 request.post(
...@@ -51,7 +48,7 @@ function sendText(replyToken, messages) { ...@@ -51,7 +48,7 @@ function sendText(replyToken, messages) {
51 // OR 48 // OR
52 // https://developers.line.biz/en/reference/messaging-api/#location-message 49 // https://developers.line.biz/en/reference/messaging-api/#location-message
53 50
54 -function sendImage(replyToken, imageUrl) { 51 +function sendLocation(replyToken, latitude, longitude, locationAdd, locationName) {
55 request.post( 52 request.post(
56 { 53 {
57 url: TARGET_URL, 54 url: TARGET_URL,
...@@ -75,9 +72,28 @@ function sendImage(replyToken, imageUrl) { ...@@ -75,9 +72,28 @@ function sendImage(replyToken, imageUrl) {
75 }); 72 });
76 } 73 }
77 74
75 +function filter_date(date, id, pw) {
76 + const schedule = canvas.get_schedule(id, pw, date)
77 + const first_todo = selector.is_possible_schedule(date, schedule)
78 +
79 + if (first_todo) {
80 + return `제출되지 않은 과제가 있습니다. ${first_todo}`
81 + }
82 +
83 + return null
84 +}
85 +/*
86 +
87 +1. 사용자가 아무 메세지나 입력?
88 +2. 이캠퍼스 일정 찾기
89 +3. 날씨 적절한지 판단
90 +4. 카테고리 물어보기
91 +5. 랜덤 위치를 알려주는 메세지 반환
92 +
93 +*/
78 var app = express(); 94 var app = express();
79 app.use(bodyParser.json()); 95 app.use(bodyParser.json());
80 -app.post('/hook', function (req, res) { 96 +app.post('/hook', async function (req, res) {
81 97
82 var eventObj = req.body.events[0]; 98 var eventObj = req.body.events[0];
83 var source = eventObj.source; 99 var source = eventObj.source;
...@@ -89,6 +105,17 @@ app.post('/hook', function (req, res) { ...@@ -89,6 +105,17 @@ app.post('/hook', function (req, res) {
89 console.log('[request source] ', eventObj.source); 105 console.log('[request source] ', eventObj.source);
90 console.log('[request message]', eventObj.message); 106 console.log('[request message]', eventObj.message);
91 107
108 + const today = new Date()
109 + const filter_result = filter_date(today, id, pw)
110 +
111 + if (filter_result) {
112 + sendText(eventObj.replyToken, filter_result)
113 + }
114 +
115 + (await weather.get_weather_current()).then(it => {
116 +
117 + })
118 +
92 if (first == false && eventObj.message.text == "처음") { 119 if (first == false && eventObj.message.text == "처음") {
93 request.post( 120 request.post(
94 { 121 {
...@@ -110,7 +137,6 @@ app.post('/hook', function (req, res) { ...@@ -110,7 +137,6 @@ app.post('/hook', function (req, res) {
110 }); 137 });
111 first = true; 138 first = true;
112 } 139 }
113 -
114 else if (first == true && second == false) { 140 else if (first == true && second == false) {
115 141
116 if (eventObj.message.text == 1) { //식사 선택 142 if (eventObj.message.text == 1) { //식사 선택
......
1 -const canvas = require('./khcanvas')
2 -const selector = require('./schedule_selector')
3 -
4 -function start(id, pw) {
5 - const today = new Date()
6 - const filter_result = filter_date(today, id, pw)
7 -
8 - if(filter_result) {
9 - return filter_result
10 - }
11 -
12 -
13 -}
14 -
15 -function filter_date(date, id, pw) {
16 - const schedule = canvas.get_schedule(id, pw, date)
17 - const first_todo = selector.is_possible_schedule(date, schedule)
18 -
19 - if(first_todo) {
20 - return `제출되지 않은 과제가 있습니다. ${first_todo}`
21 - }
22 -
23 - return null
24 -}
25 -
26 -module.exports.start = start
...\ No newline at end of file ...\ No newline at end of file
...@@ -10,7 +10,7 @@ const axios = require('axios') ...@@ -10,7 +10,7 @@ const axios = require('axios')
10 // 예) 6/2일 오후 4시 호출 => 6/2일 정오 날씨 반환 (정오 기준이므로) 10 // 예) 6/2일 오후 4시 호출 => 6/2일 정오 날씨 반환 (정오 기준이므로)
11 // 온도의 경우 단위는 섭씨입니다. 11 // 온도의 경우 단위는 섭씨입니다.
12 12
13 -/*example 13 +/*example - forecast
14 14
15 "dt": 1653620400, 15 "dt": 1653620400,
16 "sunrise": 1653596132, 16 "sunrise": 1653596132,
...@@ -52,6 +52,34 @@ const axios = require('axios') ...@@ -52,6 +52,34 @@ const axios = require('axios')
52 "uvi": 7.71 //The maximum value of UV index for the day 52 "uvi": 7.71 //The maximum value of UV index for the day
53 53
54 */ 54 */
55 +
56 +/*example - current
57 +{
58 + "dt": 1653989440,
59 + "sunrise": 1653941622,
60 + "sunset": 1653993914,
61 + "temp": 23.74,
62 + "feels_like": 22.82,
63 + "pressure": 1008,
64 + "humidity": 25,
65 + "dew_point": 2.56,
66 + "uvi": 0.17,
67 + "clouds": 20,
68 + "visibility": 10000,
69 + "wind_speed": 5.66,
70 + "wind_deg": 300,
71 + "weather": [
72 + {
73 + "id": 801,
74 + "main": "Clouds",
75 + "description": "few clouds",
76 + "icon": "02d"
77 + }
78 + ]
79 + }
80 +
81 +
82 +*/
55 async function get_weather_forecast(date) { 83 async function get_weather_forecast(date) {
56 const lat = 37.24764302276268 //위도 84 const lat = 37.24764302276268 //위도
57 const lon = 127.0783992268606 //경도 85 const lon = 127.0783992268606 //경도
...@@ -62,6 +90,16 @@ async function get_weather_forecast(date) { ...@@ -62,6 +90,16 @@ async function get_weather_forecast(date) {
62 return await axios.default.get(target).then(it => { return extract_from(date, it.data) }) 90 return await axios.default.get(target).then(it => { return extract_from(date, it.data) })
63 } 91 }
64 92
93 +async function get_weather_current() {
94 + const lat = 37.24764302276268 //위도
95 + const lon = 127.0783992268606 //경도
96 + const api_key = "336ddd01d3d6f78782eed90d3921bc7e"
97 +
98 + const target = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=minutely,hourly,alerts&appid=${api_key}&units=metric`
99 +
100 + return await axios.default.get(target).then(it => { return extract_current(it.data) })
101 +}
102 +
65 function extract_from(date, json_response) { 103 function extract_from(date, json_response) {
66 const target_timestamp = Math.floor(date.getTime() / 1000) 104 const target_timestamp = Math.floor(date.getTime() / 1000)
67 105
...@@ -70,6 +108,10 @@ function extract_from(date, json_response) { ...@@ -70,6 +108,10 @@ function extract_from(date, json_response) {
70 return json_response.daily[target_index] 108 return json_response.daily[target_index]
71 } 109 }
72 110
111 +function extract_current(json_response) {
112 + return json_response.current
113 +}
114 +
73 function find_min_index(array) { 115 function find_min_index(array) {
74 let lowest_index = 0 116 let lowest_index = 0
75 for (var i = 0; i < array.length; i++) { 117 for (var i = 0; i < array.length; i++) {
...@@ -82,3 +124,4 @@ function find_min_index(array) { ...@@ -82,3 +124,4 @@ function find_min_index(array) {
82 } 124 }
83 125
84 module.exports.get_weather_forecast = get_weather_forecast 126 module.exports.get_weather_forecast = get_weather_forecast
127 +module.exports.get_weather_current = get_weather_current
...\ No newline at end of file ...\ No newline at end of file
......