김연우

modify main.js

const express = require('express');
const request = require('request');
const talk = require('./talk')
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const bodyParser = require('body-parser');
// var latitude = 37.2429832;
// var longitude = 127.0749535;
// var locationAdd = "경기 용인시 기흥구 서그내로49번길 13"
// var location_name = "카페 서천"
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const tokens = JSON.parse(fs.readFileSync("setting.json"))
const sslport = 23023;
......@@ -25,6 +18,10 @@ var first = false; //첫 시도인지
var second = false; //첫번째 분류 선택했는지
var destCar = "";
var destination = [];
const canvas = require('./khcanvas')
const selector = require('./schedule_selector')
const weather = require('./weather')
function sendText(replyToken, messages) {
request.post(
......@@ -51,7 +48,7 @@ function sendText(replyToken, messages) {
// OR
// https://developers.line.biz/en/reference/messaging-api/#location-message
function sendImage(replyToken, imageUrl) {
function sendLocation(replyToken, latitude, longitude, locationAdd, locationName) {
request.post(
{
url: TARGET_URL,
......@@ -75,9 +72,28 @@ function sendImage(replyToken, imageUrl) {
});
}
function filter_date(date, id, pw) {
const schedule = canvas.get_schedule(id, pw, date)
const first_todo = selector.is_possible_schedule(date, schedule)
if (first_todo) {
return `제출되지 않은 과제가 있습니다. ${first_todo}`
}
return null
}
/*
1. 사용자가 아무 메세지나 입력?
2. 이캠퍼스 일정 찾기
3. 날씨 적절한지 판단
4. 카테고리 물어보기
5. 랜덤 위치를 알려주는 메세지 반환
*/
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
app.post('/hook', async function (req, res) {
var eventObj = req.body.events[0];
var source = eventObj.source;
......@@ -89,6 +105,17 @@ app.post('/hook', function (req, res) {
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
const today = new Date()
const filter_result = filter_date(today, id, pw)
if (filter_result) {
sendText(eventObj.replyToken, filter_result)
}
(await weather.get_weather_current()).then(it => {
})
if (first == false && eventObj.message.text == "처음") {
request.post(
{
......@@ -109,8 +136,7 @@ app.post('/hook', function (req, res) {
console.log(body)
});
first = true;
}
}
else if (first == true && second == false) {
if (eventObj.message.text == 1) { //식사 선택
......
const canvas = require('./khcanvas')
const selector = require('./schedule_selector')
function start(id, pw) {
const today = new Date()
const filter_result = filter_date(today, id, pw)
if(filter_result) {
return filter_result
}
}
function filter_date(date, id, pw) {
const schedule = canvas.get_schedule(id, pw, date)
const first_todo = selector.is_possible_schedule(date, schedule)
if(first_todo) {
return `제출되지 않은 과제가 있습니다. ${first_todo}`
}
return null
}
module.exports.start = start
\ No newline at end of file
......@@ -10,7 +10,7 @@ const axios = require('axios')
// 예) 6/2일 오후 4시 호출 => 6/2일 정오 날씨 반환 (정오 기준이므로)
// 온도의 경우 단위는 섭씨입니다.
/*example
/*example - forecast
"dt": 1653620400,
"sunrise": 1653596132,
......@@ -52,6 +52,34 @@ const axios = require('axios')
"uvi": 7.71 //The maximum value of UV index for the day
*/
/*example - current
{
"dt": 1653989440,
"sunrise": 1653941622,
"sunset": 1653993914,
"temp": 23.74,
"feels_like": 22.82,
"pressure": 1008,
"humidity": 25,
"dew_point": 2.56,
"uvi": 0.17,
"clouds": 20,
"visibility": 10000,
"wind_speed": 5.66,
"wind_deg": 300,
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
]
}
*/
async function get_weather_forecast(date) {
const lat = 37.24764302276268 //위도
const lon = 127.0783992268606 //경도
......@@ -62,6 +90,16 @@ async function get_weather_forecast(date) {
return await axios.default.get(target).then(it => { return extract_from(date, it.data) })
}
async function get_weather_current() {
const lat = 37.24764302276268 //위도
const lon = 127.0783992268606 //경도
const api_key = "336ddd01d3d6f78782eed90d3921bc7e"
const target = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=minutely,hourly,alerts&appid=${api_key}&units=metric`
return await axios.default.get(target).then(it => { return extract_current(it.data) })
}
function extract_from(date, json_response) {
const target_timestamp = Math.floor(date.getTime() / 1000)
......@@ -70,6 +108,10 @@ function extract_from(date, json_response) {
return json_response.daily[target_index]
}
function extract_current(json_response) {
return json_response.current
}
function find_min_index(array) {
let lowest_index = 0
for (var i = 0; i < array.length; i++) {
......@@ -81,4 +123,5 @@ function find_min_index(array) {
return lowest_index
}
module.exports.get_weather_forecast = get_weather_forecast
\ No newline at end of file
module.exports.get_weather_forecast = get_weather_forecast
module.exports.get_weather_current = get_weather_current
\ No newline at end of file
......