유명현

Merge branch 'feature/line-bot-v3' into 'main'

Feature/line bot v3

다음과 같은 기능이 구현되었습니다.
- DB에 유저-키워드 등록
- node-schedule을 이용해 1시간에 한번, 등록된 매물을 유저에게 carousel message로 알림

See merge request !19
...@@ -10,12 +10,6 @@ sequelize ...@@ -10,12 +10,6 @@ sequelize
10 .sync({ force: false }) 10 .sync({ force: false })
11 .then(() => { 11 .then(() => {
12 console.log("database connection complete"); 12 console.log("database connection complete");
13 - database.addKeyword("rtx3060", "junseok");
14 - database.getKeywordsByUserId("junseok");
15 - database.deleteKeyword("phobyjun", "rtx3080");
16 - database.getAllUsers();
17 - database.getUsersByKeyword("rtx3060");
18 - database.getAllKeywords();
19 }) 13 })
20 .catch((err) => { 14 .catch((err) => {
21 console.log("database connection failed"); 15 console.log("database connection failed");
......
1 +const { marketMultiSearch } = require("../search/marketSearch");
2 +const setCarouselMessage = require("../message/setCarouselMessage");
3 +// Database APIs
4 +const db = require("../../apis/database");
5 +// API List
6 +// database.addKeyword = async function(keyword, userId)
7 +// database.deleteKeyword = async function(userId, keyword)
8 +// database.getKeywordsByUserId = async function(userId)
9 +// database.getUsersByKeyword = async function(keyword)
10 +// database.getAllUsers = async function()
11 +// database.getAllKeywords = async function()
12 +
13 +const checkMamul = (client) => {
14 + db.getAllKeywords().then((keywords) => {
15 + for (let i = 0, pending = Promise.resolve(); i < keywords.length; i++) {
16 + pending = db.getUsersByKeyword(keywords[i]).then((userIds) => {
17 + marketMultiSearch(keywords[i]).then((res) => {
18 + client.multicast(userIds, [setCarouselMessage(res)]);
19 + });
20 + });
21 + }
22 + });
23 +};
24 +
25 +module.exports = { checkMamul };
1 +// Line chatbot + Message generate functions
1 const line = require("@line/bot-sdk"); 2 const line = require("@line/bot-sdk");
2 const setFlexMessage = require("./message/setFlexMessage"); 3 const setFlexMessage = require("./message/setFlexMessage");
3 const setCarouselMessage = require("./message/setCarouselMessage"); 4 const setCarouselMessage = require("./message/setCarouselMessage");
4 -const fs = require("fs"); 5 +
6 +// Market Search
5 const { daangnSingleSearch } = require("./search/daangnSearch"); 7 const { daangnSingleSearch } = require("./search/daangnSearch");
6 const { daangnMultiSearch } = require("./search/daangnSearch"); 8 const { daangnMultiSearch } = require("./search/daangnSearch");
7 const { joongnaSingleSearch } = require("./search/joongnaSearch"); 9 const { joongnaSingleSearch } = require("./search/joongnaSearch");
...@@ -10,12 +12,36 @@ const { bunjangSingleSearch } = require("./search/bunjangSearch"); ...@@ -10,12 +12,36 @@ const { bunjangSingleSearch } = require("./search/bunjangSearch");
10 const { bunjangMultiSearch } = require("./search/bunjangSearch"); 12 const { bunjangMultiSearch } = require("./search/bunjangSearch");
11 const { marketMultiSearch } = require("./search/marketSearch"); 13 const { marketMultiSearch } = require("./search/marketSearch");
12 14
15 +// File search - Will be deleted (Unused)
16 +const fs = require("fs");
17 +
18 +// Cron for Mamul Notification
19 +const schedule = require("node-schedule");
20 +const job = schedule.scheduleJob("0 */1 * * *", () => {
21 + checkMamul(client);
22 +});
23 +
24 +// Database APIs
25 +const db = require("../apis/database");
26 +// API List
27 +// database.addKeyword = async function(keyword, userId)
28 +// database.deleteKeyword = async function(userId, keyword)
29 +// database.getKeywordsByUserId = async function(userId)
30 +// database.getUsersByKeyword = async function(keyword)
31 +// database.getAllUsers = async function()
32 +// database.getAllKeywords = async function()
33 +
34 +// Import credentials for Line chatbot
13 require("dotenv").config({ path: __dirname + "/../.env" }); 35 require("dotenv").config({ path: __dirname + "/../.env" });
14 const config = { 36 const config = {
15 channelAccessToken: process.env.channelAccessToken, 37 channelAccessToken: process.env.channelAccessToken,
16 channelSecret: process.env.channelSecret, 38 channelSecret: process.env.channelSecret,
17 }; 39 };
18 40
41 +// Cron for Mamul Notification
42 +const { checkMamul } = require("./checkMamul/checkMamul");
43 +
44 +// Line chat bot client & event
19 const client = new line.Client(config); 45 const client = new line.Client(config);
20 46
21 let waitNewMamulList = []; // 매물 키워드 입력 기다리는 목록 47 let waitNewMamulList = []; // 매물 키워드 입력 기다리는 목록
...@@ -28,7 +54,7 @@ function handleEvent(event) { ...@@ -28,7 +54,7 @@ function handleEvent(event) {
28 var found = waitNewMamulList.indexOf(event.source.userId); 54 var found = waitNewMamulList.indexOf(event.source.userId);
29 if (found == -1) { 55 if (found == -1) {
30 waitNewMamulList.push(event.source.userId); 56 waitNewMamulList.push(event.source.userId);
31 - console.log(waitNewMamulList); 57 + console.log(`waitNewMamulList Changed : ${waitNewMamulList}`);
32 return Promise.resolve( 58 return Promise.resolve(
33 client.replyMessage(event.replyToken, { 59 client.replyMessage(event.replyToken, {
34 type: "text", 60 type: "text",
...@@ -54,7 +80,7 @@ function handleEvent(event) { ...@@ -54,7 +80,7 @@ function handleEvent(event) {
54 "1000000", 80 "1000000",
55 "https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside", 81 "https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside",
56 "https://www.daangn.com/articles/403755360", 82 "https://www.daangn.com/articles/403755360",
57 - "test설명" 83 + "채굴X, 흡연X, 반려동물X 입니다.\n직거래 희망하며, 쿨거래시 네고 1만원 가능합니다."
58 ), 84 ),
59 }) 85 })
60 ); 86 );
...@@ -75,9 +101,13 @@ function handleEvent(event) { ...@@ -75,9 +101,13 @@ function handleEvent(event) {
75 waitNewMamulList.splice(found, 1); 101 waitNewMamulList.splice(found, 1);
76 console.log(waitNewMamulList[found]); 102 console.log(waitNewMamulList[found]);
77 return Promise.resolve( 103 return Promise.resolve(
104 + db.addKeyword(event.message.text, event.source.userId),
78 client.replyMessage(event.replyToken, { 105 client.replyMessage(event.replyToken, {
79 type: "text", 106 type: "text",
80 - text: "매물이 등록되었습니다!\n등록된 매물: " + event.message.text, 107 + text: `매물이 등록되었습니다!\n등록된 매물: ${event.message.text}`,
108 + }),
109 + marketMultiSearch(event.message.text).then((res) => {
110 + client.pushMessage(event.source.userId, setCarouselMessage(res));
81 }) 111 })
82 ); 112 );
83 } 113 }
......
This diff is collapsed. Click to expand it.
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
20 "dotenv": "^16.0.1", 20 "dotenv": "^16.0.1",
21 "express": "^4.18.1", 21 "express": "^4.18.1",
22 "mysql2": "^2.3.3", 22 "mysql2": "^2.3.3",
23 + "node-schedule": "^2.1.0",
23 "nodemon": "^2.0.16", 24 "nodemon": "^2.0.16",
24 "sequelize": "^6.20.0" 25 "sequelize": "^6.20.0"
25 } 26 }
......