송용우

Update Slack api

......@@ -18,22 +18,22 @@
## API Table
| group | description | method | URL | Detail | Auth |
| ------- | --------------------------- | --------- | ----------------------- | -------- | --------- |
| user | 유저 등록 | POST | api/user | 바로가기 | JWT Token |
| user | 유저 삭제 | DELETE | api/user:id | 바로가기 | JWT Token |
| user | 특정 유저 조회 | GET | api/user:id | 바로가기 | None |
| user | 전체 유저 조회 | GET | api/user | 바로가기 | JWT Token |
| friend | 유저 친구 등록 | POST | api/friend | 바로가기 | JWT Token |
| friend | 유저의 친구 조회 | GET | api/friend:id | 바로가기 | None |
| profile | 유저가 푼 문제 조회(백준) | GET | api/profile/solvedBJ:id | 바로가기 | None |
| profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | 바로가기 | None |
| profile | 유저 정보 수정 | POST | api/profile/setprofile | 바로가기 | JWT TOKEN |
| profile | 유저 정보 받아오기 | POST | api/profile/getprofile | 바로가기 | JWT |
| profile | 추천 문제 조회 | POST | api/profile/recommend | 바로가기 | None |
| notify | 슬랙 메시지 전송 요청 | POST | api/notify/ |
| slack | 바로가기 | Jwt Token |
| auth | 로그인 | POST | api/auth/login | 바로가기 | None |
| auth | 로그아웃 | POST | api/auth/logout | 바로가기 | JWT Token |
| auth | 회원가입 | POST | api/auth/register | 바로가기 | None |
| auth | 로그인 확인 | GET | api/auth/check | 바로가기 | None |
| group | description | method | URL | Detail | Auth |
| ------- | --------------------------------- | ------ | ----------------------- | -------- | --------- |
| user | 유저 등록 | POST | api/user | 바로가기 | JWT Token |
| user | 유저 삭제 | DELETE | api/user:id | 바로가기 | JWT Token |
| user | 특정 유저 조회 | GET | api/user:id | 바로가기 | None |
| user | 전체 유저 조회 | GET | api/user | 바로가기 | JWT Token |
| friend | 유저 친구 등록 | POST | api/friend | 바로가기 | JWT Token |
| friend | 유저의 친구 조회 | GET | api/friend:id | 바로가기 | None |
| profile | 유저가 푼 문제 조회(백준) | GET | api/profile/solvedBJ:id | 바로가기 | None |
| profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | 바로가기 | None |
| profile | 유저 정보 수정 | POST | api/profile/setprofile | 바로가기 | JWT TOKEN |
| profile | 유저 정보 받아오기 | POST | api/profile/getprofile | 바로가기 | JWT |
| profile | 추천 문제 조회 | POST | api/profile/recommend | 바로가기 | None |
| notify | 슬랙 메시지 전송 요청 (성취여부) | POST | api/notify/goal | 바로가기 | Jwt Token |
| notify | 슬랙 메시지 전송 요청 (문제 추천) | POST | api/notify/recommend | 바로가기 | None |
| auth | 로그인 | POST | api/auth/login | 바로가기 | None |
| auth | 로그아웃 | POST | api/auth/logout | 바로가기 | JWT Token |
| auth | 회원가입 | POST | api/auth/register | 바로가기 | None |
| auth | 로그인 확인 | GET | api/auth/check | 바로가기 | None |
......
......@@ -1341,3 +1341,44 @@
::ffff:127.0.0.1 - - [24/Jun/2020:15:41:51 +0000] "GET /api/auth/check HTTP/1.1" 200 55 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
::ffff:127.0.0.1 - - [24/Jun/2020:15:41:51 +0000] "POST /api/profile/getprofile HTTP/1.1" 401 12 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
::ffff:127.0.0.1 - - [24/Jun/2020:15:41:51 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 33293 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
::1 - - [24/Jun/2020:16:47:20 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:47:43 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:48:07 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:49:04 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:50:59 +0000] "POST /api/notify/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:52:40 +0000] "POST /api/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::ffff:127.0.0.1 - - [24/Jun/2020:16:52:55 +0000] "POST /api/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:54:01 +0000] "POST /api/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:54:14 +0000] "POST /api/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:54:28 +0000] "POST /api/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:56:14 +0000] "POST /api/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:56:33 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 33293 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:56:52 +0000] "POST /api/notify/slack/goal HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:57:18 +0000] "POST /api/notify/slack/goal HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:16:57:50 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:00:04 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::ffff:127.0.0.1 - - [24/Jun/2020:17:00:27 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 33293 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
::ffff:127.0.0.1 - - [24/Jun/2020:17:00:29 +0000] "POST /api/profile/setprofile HTTP/1.1" 200 33293 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
::1 - - [24/Jun/2020:17:00:37 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:01:46 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::ffff:127.0.0.1 - - [24/Jun/2020:17:12:37 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:13:17 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:13:36 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:14:19 +0000] "POST /api/notify/slack/goal HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:22:37 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:23:06 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:27:52 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:30:55 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:32:42 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:34:03 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:36:50 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:40:43 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:42:09 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:43:17 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:53:11 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:53:24 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:54:40 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:55:11 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:55:24 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:17:55:39 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
::1 - - [24/Jun/2020:18:00:32 +0000] "POST /api/notify/slack/recommend HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
......
......@@ -3,7 +3,7 @@ const api = new Router();
const auth = require("./auth");
const friend = require("./friend");
const notify = require("./profile");
const notify = require("./notify");
const user = require("./user");
const profile = require("./profile");
......
const Router = require("koa-router");
const notify = new Router();
notify.post("/slack");
const slackCtrl = require("./slack.ctrl");
notify.post("/slack/goal", slackCtrl.slackGoal);
notify.post("/slack/recommend", slackCtrl.slackRecommend);
module.exports = notify;
......
const Profile = require("../../models/profile");
const sendSlack = require("../../util/sendSlack");
const problem_set = require("../../data/problem_set");
const compareBJ = require("../../util/compareBJ");
/*
POST api/notify/slack/goal
{
username: "username"
}
*/
exports.slackGoal = async (ctx) => {
try {
const { username } = ctx.request.body;
const profile = await Profile.findByUsername(username);
if (!profile) {
ctx.status = 401;
return;
}
let slackURL = profile.getslackURL();
if (!slackURL) {
ctx.status = 401;
return;
}
let goalNum = profile.getgoalNum();
let todayNum = profile.getTodaySovled();
let message = "";
if (goalNum < todayNum) {
message =
"오늘의 목표 " +
goalNum +
"문제 중 " +
todayNum +
"문제를 풀었습니다." +
"\n" +
"잘하셨습니다!";
} else {
message =
"오늘의 목표 " +
goalNum +
"문제 중 " +
todayNum +
"문제를 풀었습니다." +
"\n" +
"분발하세요!";
}
sendSlack.send(message, slackURL);
} catch (e) {
ctx.throw(500, e);
}
};
/*
POST api/notify/slack/recommend
{
username: "username"
}
*/
exports.slackRecommend = async (ctx) => {
try {
console.log("1");
const { username } = ctx.request.body;
const profile = await Profile.findByUsername(username);
if (!profile) {
ctx.status = 401;
return;
}
let slackURL = profile.getslackURL();
if (!slackURL) {
ctx.status = 401;
return;
}
let unsolved_data = compareBJ.compareBJ(
profile.getBJdata(),
problem_set.problem_set
);
let recommendData = compareBJ.randomItem(unsolved_data);
if (!recommendData) {
ctx.status = 401;
return;
}
let message =
"오늘의 추천 문제는 " +
recommendData.problem_number +
"번 " +
" <https://www.boj.kr/" +
recommendData.problem_number +
"|" +
recommendData.problem_title +
">" +
" 입니다.";
sendSlack.send(message, slackURL);
} catch (e) {
ctx.throw(500, e);
}
};
This diff is collapsed. Click to expand it.
......@@ -20,6 +20,17 @@ ProfileSchema.methods.getBJID = function () {
ProfileSchema.methods.getBJdata = function () {
return this.solvedBJ;
};
ProfileSchema.methods.getslackURL = function () {
return this.slackWebHookURL;
};
ProfileSchema.methods.getgoalNum = function () {
return this.goalNum;
};
ProfileSchema.methods.getTodaySovled = function () {
if (this.solvedBJ_date) {
return this.solvedBJ_date.presentNum;
}
};
ProfileSchema.methods.serialize = function () {
const data = this.toJSON();
......
exports.compareBJ = function (solvedBJ_new, problem_set) {
try {
let new_obj = [];
for (let i = 0; i < solvedBJ_new.length; i++) {
if (!problem_set.includes(String(solvedBJ_new[i].problem_number))) {
new_obj.push(solvedBJ_new[i]);
for (let i = 0; i < problem_set.length; i++) {
let found = false;
for (let j = 0; j < solvedBJ_new.length; j++) {
if (solvedBJ_new[j].problem_number == problem_set[i].problem_number) {
found = true;
break;
}
}
if (!found) {
new_obj.push(problem_set[i]);
}
}
console.log(new_obj);
return new_obj;
} catch (e) {
console.log(e);
......
const Slack = require("slack-node"); // 슬랙 모듈 사용
/*
const webhookUri =
"https://hooks.slack.com/services/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"; // Webhook URL
"https://hooks.slack.com/services/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"; // Webhook URL
*/
const slack = new Slack();
slack.setWebhook(webhookUri);
const send = async (message) => {
exports.send = async (message, webhookUri) => {
const slack = new Slack();
slack.setWebhook(webhookUri);
slack.webhook(
{
text: message,
......@@ -16,5 +17,3 @@ const send = async (message) => {
}
);
};
send("hello");
......
......@@ -8,7 +8,7 @@ const test = async (userid) => {
return_lst.push(lst[i].problem_number);
}
var stringJson = JSON.stringify(return_lst) + "\n";
var stringJson = JSON.stringify(lst) + "\n";
fs.open("test.json", "a", "666", function (err, id) {
if (err) {
console.log("file open err!!");
......
This diff is collapsed. Click to expand it.