Skip to content
Draft

test #13

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
07ac8c4
refactor: add fetch, send message, utils to lib
seoljiwon Jun 9, 2022
0ebbd83
refactor: add sender, url to constants
seoljiwon Jun 9, 2022
987fea2
refactor: add auth to config
seoljiwon Jun 9, 2022
05e86cd
refactor: use send message function
seoljiwon Jun 9, 2022
34bdb1d
refactor: use summon function
seoljiwon Jun 9, 2022
d3eba81
refactor: add check num to check text
seoljiwon Jun 9, 2022
7e4a24d
style: add prettierrc
seoljiwon Jun 9, 2022
b8037ab
test: add console log to test
seoljiwon Jun 9, 2022
c592251
rename: rename index to server
seoljiwon Jun 9, 2022
26591cd
fix: fix import error
seoljiwon Jun 11, 2022
9b1bfee
refactor: cast string in send message func
seoljiwon Jun 11, 2022
9cb2211
fix: fix error handling order
seoljiwon Jun 11, 2022
b1013ea
refactor: import auth in fetch func
seoljiwon Jun 11, 2022
230fd57
fix: fix error handling to print message
seoljiwon Jun 11, 2022
f81134b
fix: fix additional path error
seoljiwon Jun 11, 2022
b93d0ab
fix: fix async await
seoljiwon Jun 11, 2022
361307c
feat: add get random num func for unique selection
seoljiwon Jun 11, 2022
2333287
refactor: change export method
seoljiwon Jun 11, 2022
bd39673
feat: add handling for limit num
seoljiwon Jun 11, 2022
a182313
feat: change return type set to array
seoljiwon Jun 11, 2022
cbe1610
fix: fix declaration order
seoljiwon Jun 11, 2022
f1773dc
feat: change negative num to positive num
seoljiwon Jun 11, 2022
134663e
fix: change value
seoljiwon Jun 11, 2022
19d21a6
feat: add handling when num is zero
seoljiwon Jun 11, 2022
0fab46e
feat: check num only string exist
seoljiwon Jun 11, 2022
cdae859
fix: fix isInt for negative num, last manager for float num
seoljiwon Jun 11, 2022
4124bd3
fix: change constant to let
seoljiwon Jun 11, 2022
997062e
fix: fix condition
seoljiwon Jun 11, 2022
5a7959f
fix: fix float condition
seoljiwon Jun 11, 2022
04d909b
fix: change negative num not to floor
seoljiwon Jun 11, 2022
b79ee5c
style: change msg value
seoljiwon Jun 11, 2022
4d9fba9
Merge branch 'main' into develop
seoljiwon Jun 11, 2022
02cb584
Merge branch 'develop' into main
seoljiwon Jun 11, 2022
acc7e7d
Merge branch 'main' of https://github.com/aiccuracy/devkor-channel
seoljiwon Jun 11, 2022
07d334a
feat: add routing
seoljiwon Jun 11, 2022
c5b7c50
fix: fix import error
seoljiwon Jun 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 3 additions & 59 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import express from 'express';
import dotenv from 'dotenv';
import { summon } from './src/summon.js';
import { sendMessage } from "./src/lib/sendMessage.js";
import { sender } from "./src/constants/sender.js";
import { stop } from "./src/lib/stop.js";

import router from './routes/index.js';

dotenv.config();
const port = process.env.PORT || 8080;
Expand All @@ -13,61 +11,7 @@ const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

const botName = 'DevKor';

app.post('/select', async (res) => {
try {
const { body } = res;
const { event, entity } = body;
const { plainText = '', personType = '', chatId: groupId } = entity;

const isPushEvent = event === 'push';
const keyword = '명';
const hasKeyword = plainText.includes(keyword);
const isManager = personType === 'manager';

const needToSummon = isPushEvent && hasKeyword && isManager;

if (needToSummon) {
summon(plainText, keyword, groupId, botName);
}
} catch (err) {
console.log(err);
}
});

app.post('/stop', async (res) => {
try {
const { body } = res;
const { event, entity } = body;
const { plainText = '', personType = '', chatId: groupId } = entity;

const isPushEvent = event === 'push';
const keyword = '/멈춰';
const hasKeyword = plainText.includes(keyword);
const isManager = personType === 'manager';

const needToSummon = isPushEvent && hasKeyword && isManager;

if (needToSummon) {
const body = {
blocks: [
{
type: 'text',
value: "Okay! I will stop it!",
},
],
options: ['actAsManager'],
};
sendMessage(sender.GROUP, groupId, 'messages', { botName: botName }, body, 'post');
setTimeout(() => {
stop()
}, 5000);
}
} catch (err) {
console.log(err);
}
})
app.use('/', router);

app.listen(port, () => {
console.log(`Listening on port ${port}...`);
Expand Down
30 changes: 0 additions & 30 deletions src/celebrate.js

This file was deleted.

File renamed without changes.
11 changes: 0 additions & 11 deletions src/lib/stop.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';

const router = express.Router();

router.use('/select', require('./select'));
router.use('/stop', require('./stop'));

module.exports = router;
28 changes: 28 additions & 0 deletions src/routes/select/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import express from 'express';
import { select } from './select.js';

const router = express.Router();
const botName = 'DevKor';

router.post('/select', async (res) => {
try {
const { body } = res;
const { event, entity } = body;
const { plainText = '', personType = '', chatId: groupId } = entity;

const isPushEvent = event === 'push';
const keyword = '명';
const hasKeyword = plainText.includes(keyword);
const isManager = personType === 'manager';

const needToSummon = isPushEvent && hasKeyword && isManager;

if (needToSummon) {
select(plainText, keyword, groupId, botName);
}
} catch (err) {
console.log(err);
}
});

module.exports = router;
33 changes: 30 additions & 3 deletions src/summon.js → src/routes/select/select.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import { sender } from './constants/sender.js';
import { sendMessage } from './lib/sendMessage.js';
import { getMembers, selectMembers } from './randomSelect.js';
import { celebrate, personalAnnounce } from './celebrate.js';
import { getMembers, selectMembers } from '../../lib/selectRandomMem.js';
import { checkText } from './lib/checkText.js';

export const summon = async (plainText, keyword, groupId, botName) => {
const celebrate = (msg, groupId, botName) => {
const body = {
blocks: [
{
type: 'text',
value: msg,
},
],
options: ['actAsManager'],
};

sendMessage(sender.GROUP, groupId, 'messages', { botName: botName }, body, 'post');
};

const personalAnnounce = (managerId, botName) => {
const body = {
blocks: [
{
type: 'text',
value: '🎉당첨🎉 축하드립니다!',
},
],
options: ['actAsManager'],
};

sendMessage(sender.ANNOUNCEMENTS, undefined, 'announce', { botName: botName, managerIds: managerId }, body, 'post');
};

export const select = async (plainText, keyword, groupId, botName) => {
const [n, isInt, msg] = checkText(plainText, keyword);

// negative number or real numbers
Expand Down
42 changes: 42 additions & 0 deletions src/routes/stop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import express from 'express';
import { sendMessage } from './src/lib/sendMessage.js';
import { sender } from './src/constants/sender.js';
import { stop } from './stop.js';

const router = express.Router();
const botName = 'DevKor';

router.post('/stop', async (res) => {
try {
const { body } = res;
const { event, entity } = body;
const { plainText = '', personType = '', chatId: groupId } = entity;

const isPushEvent = event === 'push';
const keyword = '/멈춰';
const hasKeyword = plainText.includes(keyword);
const isManager = personType === 'manager';

const needToSummon = isPushEvent && hasKeyword && isManager;

if (needToSummon) {
const body = {
blocks: [
{
type: 'text',
value: 'Okay! I will stop it!',
},
],
options: ['actAsManager'],
};
sendMessage(sender.GROUP, groupId, 'messages', { botName: botName }, body, 'post');
setTimeout(() => {
stop();
}, 5000);
}
} catch (err) {
console.log(err);
}
});

module.exports = router;
11 changes: 11 additions & 0 deletions src/routes/stop/stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node
import shell from 'shelljs';

export const stop = () => {
shell.cd('~');

if (shell.exec('heroku ps:stop run').code !== 0) {
shell.echo('Error: Heroku server down failed');
shell.exit(1);
}
};