신원형

main.js temparatory edit

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;
...@@ -21,6 +14,11 @@ const TOKEN = tokens.channel ...@@ -21,6 +14,11 @@ const TOKEN = tokens.channel
21 const id = tokens.id 14 const id = tokens.id
22 const pw = tokens.pw 15 const pw = tokens.pw
23 16
17 +const canvas = require('./khcanvas')
18 +const selector = require('./schedule_selector')
19 +const weather = require('./weather')
20 +
21 +
24 function sendText(replyToken, messages) { 22 function sendText(replyToken, messages) {
25 request.post( 23 request.post(
26 { 24 {
...@@ -46,7 +44,7 @@ function sendText(replyToken, messages) { ...@@ -46,7 +44,7 @@ function sendText(replyToken, messages) {
46 // OR 44 // OR
47 // https://developers.line.biz/en/reference/messaging-api/#location-message 45 // https://developers.line.biz/en/reference/messaging-api/#location-message
48 46
49 -function sendImage(replyToken, imageUrl) { 47 +function sendLocation(replyToken, latitude, longitude, locationAdd, locationName) {
50 request.post( 48 request.post(
51 { 49 {
52 url: TARGET_URL, 50 url: TARGET_URL,
...@@ -70,9 +68,28 @@ function sendImage(replyToken, imageUrl) { ...@@ -70,9 +68,28 @@ function sendImage(replyToken, imageUrl) {
70 }); 68 });
71 } 69 }
72 70
71 +function filter_date(date, id, pw) {
72 + const schedule = canvas.get_schedule(id, pw, date)
73 + const first_todo = selector.is_possible_schedule(date, schedule)
74 +
75 + if (first_todo) {
76 + return `제출되지 않은 과제가 있습니다. ${first_todo}`
77 + }
78 +
79 + return null
80 +}
81 +/*
82 +
83 +1. 사용자가 아무 메세지나 입력?
84 +2. 이캠퍼스 일정 찾기
85 +3. 날씨 적절한지 판단
86 +4. 카테고리 물어보기
87 +5. 랜덤 위치를 알려주는 메세지 반환
88 +
89 +*/
73 var app = express(); 90 var app = express();
74 app.use(bodyParser.json()); 91 app.use(bodyParser.json());
75 -app.post('/hook', function (req, res) { 92 +app.post('/hook', async function (req, res) {
76 93
77 var eventObj = req.body.events[0]; 94 var eventObj = req.body.events[0];
78 // var source = eventObj.source; 95 // var source = eventObj.source;
...@@ -84,20 +101,30 @@ app.post('/hook', function (req, res) { ...@@ -84,20 +101,30 @@ app.post('/hook', function (req, res) {
84 console.log('[request source] ', eventObj.source); 101 console.log('[request source] ', eventObj.source);
85 console.log('[request message]', eventObj.message); 102 console.log('[request message]', eventObj.message);
86 103
87 - sendText(eventObj.replyToken, talk.start(id, pw)) // Test only 104 + const today = new Date()
88 - //sendImage(eventObj.replyToken) 105 + const filter_result = filter_date(today, id, pw)
106 +
107 + if (filter_result) {
108 + sendText(eventObj.replyToken, filter_result)
109 + }
110 +
111 + (await weather.get_weather_current()).then(it => {
112 +
113 + })
114 +
115 +
89 116
90 res.sendStatus(200); 117 res.sendStatus(200);
91 }); 118 });
92 119
93 try { 120 try {
94 - const option = { 121 + // const option = {
95 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), 122 + // ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'),
96 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), 123 + // key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(),
97 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), 124 + // cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(),
98 - }; 125 + // };
99 126
100 - HTTPS.createServer(option, app).listen(sslport, () => { 127 + HTTPS.createServer(app).listen(sslport, () => {
101 console.log(`[HTTPS] Server is started on port ${sslport}`); 128 console.log(`[HTTPS] Server is started on port ${sslport}`);
102 }); 129 });
103 } catch (error) { 130 } catch (error) {
......
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