신원형

initial talk feature

asdffdsa.txt
token.json
setting.json
node_modules
\ No newline at end of file
......
var express = require('express');
const express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = '채널 토큰으로 변경'
const talk = require('./talk')
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "도메인 변경"
const sslport = 23023;
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
var source = eventObj.source;
var message = eventObj.message;
// request log
console.log('======================', new Date() ,'======================');
console.log('[request]', req.body);
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const tokens = JSON.parse(fs.readFileSync("setting.json"))
const sslport = 23023;
const domain = tokens.domain
const TOKEN = tokens.channel
const id = tokens.id
const pw = tokens.pw
function sendText(replyToken, messages) {
request.post(
{
url: TARGET_URL,
......@@ -30,38 +24,77 @@ app.post('/hook', function (req, res) {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
"replyToken": replyToken,
"messages": [
{
"type":"text",
"text":"Hello, user"
},
"type": "text",
"text": messages
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
// https://developers.line.biz/en/reference/messaging-api/#image-message
// OR
// https://developers.line.biz/en/reference/messaging-api/#location-message
function sendImage(replyToken, imageUrl) {
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": replyToken,
"messages": [
{
"type":"text",
"text":"May I help you?"
"type": "image",
"originalContentUrl": imageUrl,
"previewImageUrl": imageUrl
}
]
}
},(error, response, body) => {
}, (error, response, body) => {
console.log(body)
});
}
var app = express();
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
// var source = eventObj.source;
// var message = eventObj.message;
// request log
console.log('======================', new Date(), '======================');
console.log('[request]', req.body);
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
sendText(eventObj.replyToken, talk.start(id, pw)) // Test only
//sendImage(eventObj.replyToken)
res.sendStatus(200);
});
try {
const option = {
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'),
key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(),
cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(),
};
HTTPS.createServer(option, app).listen(sslport, () => {
console.log(`[HTTPS] Server is started on port ${sslport}`);
console.log(`[HTTPS] Server is started on port ${sslport}`);
});
} catch (error) {
} catch (error) {
console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
console.log(error);
}
}
......
......@@ -13,7 +13,7 @@
const ADayForMS = 1000 * 60 * 60 * 24
export async function is_possible_schedule(date, assignments) {
async function is_possible_schedule(date, assignments) {
const assignments_dates = assignments.map(it => new Date(it.due_date))
const is_disqualified = assignments_dates.filter(it => {
const current_timestamp = date.getTime()
......@@ -23,8 +23,11 @@ export async function is_possible_schedule(date, assignments) {
})
if(is_disqualified) {
return is_disqualified.at(0)
const disq = is_disqualified.at(0)
return `${disq.course_name}: ${disq.assignment_name} [${disq.points} 점]`
} else {
return null
}
}
\ No newline at end of file
}
module.exports.is_possible_schedule = is_possible_schedule
\ No newline at end of file
......
class Talk {
}
\ No newline at end of file
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
......