유명현

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
.sync({ force: false })
.then(() => {
console.log("database connection complete");
database.addKeyword("rtx3060", "junseok");
database.getKeywordsByUserId("junseok");
database.deleteKeyword("phobyjun", "rtx3080");
database.getAllUsers();
database.getUsersByKeyword("rtx3060");
database.getAllKeywords();
})
.catch((err) => {
console.log("database connection failed");
......
const { marketMultiSearch } = require("../search/marketSearch");
const setCarouselMessage = require("../message/setCarouselMessage");
// Database APIs
const db = require("../../apis/database");
// API List
// database.addKeyword = async function(keyword, userId)
// database.deleteKeyword = async function(userId, keyword)
// database.getKeywordsByUserId = async function(userId)
// database.getUsersByKeyword = async function(keyword)
// database.getAllUsers = async function()
// database.getAllKeywords = async function()
const checkMamul = (client) => {
db.getAllKeywords().then((keywords) => {
for (let i = 0, pending = Promise.resolve(); i < keywords.length; i++) {
pending = db.getUsersByKeyword(keywords[i]).then((userIds) => {
marketMultiSearch(keywords[i]).then((res) => {
client.multicast(userIds, [setCarouselMessage(res)]);
});
});
}
});
};
module.exports = { checkMamul };
// Line chatbot + Message generate functions
const line = require("@line/bot-sdk");
const setFlexMessage = require("./message/setFlexMessage");
const setCarouselMessage = require("./message/setCarouselMessage");
const fs = require("fs");
// Market Search
const { daangnSingleSearch } = require("./search/daangnSearch");
const { daangnMultiSearch } = require("./search/daangnSearch");
const { joongnaSingleSearch } = require("./search/joongnaSearch");
......@@ -10,12 +12,36 @@ const { bunjangSingleSearch } = require("./search/bunjangSearch");
const { bunjangMultiSearch } = require("./search/bunjangSearch");
const { marketMultiSearch } = require("./search/marketSearch");
// File search - Will be deleted (Unused)
const fs = require("fs");
// Cron for Mamul Notification
const schedule = require("node-schedule");
const job = schedule.scheduleJob("0 */1 * * *", () => {
checkMamul(client);
});
// Database APIs
const db = require("../apis/database");
// API List
// database.addKeyword = async function(keyword, userId)
// database.deleteKeyword = async function(userId, keyword)
// database.getKeywordsByUserId = async function(userId)
// database.getUsersByKeyword = async function(keyword)
// database.getAllUsers = async function()
// database.getAllKeywords = async function()
// Import credentials for Line chatbot
require("dotenv").config({ path: __dirname + "/../.env" });
const config = {
channelAccessToken: process.env.channelAccessToken,
channelSecret: process.env.channelSecret,
};
// Cron for Mamul Notification
const { checkMamul } = require("./checkMamul/checkMamul");
// Line chat bot client & event
const client = new line.Client(config);
let waitNewMamulList = []; // 매물 키워드 입력 기다리는 목록
......@@ -28,7 +54,7 @@ function handleEvent(event) {
var found = waitNewMamulList.indexOf(event.source.userId);
if (found == -1) {
waitNewMamulList.push(event.source.userId);
console.log(waitNewMamulList);
console.log(`waitNewMamulList Changed : ${waitNewMamulList}`);
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "text",
......@@ -54,7 +80,7 @@ function handleEvent(event) {
"1000000",
"https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside",
"https://www.daangn.com/articles/403755360",
"test설명"
"채굴X, 흡연X, 반려동물X 입니다.\n직거래 희망하며, 쿨거래시 네고 1만원 가능합니다."
),
})
);
......@@ -75,9 +101,13 @@ function handleEvent(event) {
waitNewMamulList.splice(found, 1);
console.log(waitNewMamulList[found]);
return Promise.resolve(
db.addKeyword(event.message.text, event.source.userId),
client.replyMessage(event.replyToken, {
type: "text",
text: "매물이 등록되었습니다!\n등록된 매물: " + event.message.text,
text: `매물이 등록되었습니다!\n등록된 매물: ${event.message.text}`,
}),
marketMultiSearch(event.message.text).then((res) => {
client.pushMessage(event.source.userId, setCarouselMessage(res));
})
);
}
......
This diff is collapsed. Click to expand it.
......@@ -20,6 +20,7 @@
"dotenv": "^16.0.1",
"express": "^4.18.1",
"mysql2": "^2.3.3",
"node-schedule": "^2.1.0",
"nodemon": "^2.0.16",
"sequelize": "^6.20.0"
}
......