윤준석

Merge branch 'feature/220525_rich_menu' into 'main'

Feature/220525 rich menu



See merge request !20
const setKeywordsFlexMessage = require("../message/setKeywordsFlexMessage")
const db = require("../../apis/database");
const checkKeywords = (client, event) => {
db.getKeywordsByUserId(event.source.userId).then((keywords) => {
flexMessage = setKeywordsFlexMessage(keywords);
client.replyMessage(event.replyToken, flexMessage)
})
};
module.exports = { checkKeywords };
\ No newline at end of file
......@@ -10,7 +10,7 @@ const db = require("../../apis/database");
// database.getAllUsers = async function()
// database.getAllKeywords = async function()
const checkMamul = (client) => {
const multiCheckMamul = (client) => {
db.getAllKeywords().then((keywords) => {
for (let i = 0, pending = Promise.resolve(); i < keywords.length; i++) {
pending = db.getUsersByKeyword(keywords[i]).then((userIds) => {
......@@ -22,4 +22,14 @@ const checkMamul = (client) => {
});
};
module.exports = { checkMamul };
const checkMamul = (client, userId) => {
db.getKeywordsByUserId(userId).then((keywords) => {
for (let i = 0, pending = Promise.resolve(); i< keywords.length; i++) {
pending = marketMultiSearch(keywords[i]).then((res) => {
client.pushMessage(userId, setCarouselMessage(res));
});
};
});
};
module.exports = { multiCheckMamul, checkMamul };
......
......@@ -2,6 +2,7 @@
const line = require("@line/bot-sdk");
const setFlexMessage = require("./message/setFlexMessage");
const setCarouselMessage = require("./message/setCarouselMessage");
const setKeywordsFlexMessage = require("./message/setKeywordsFlexMessage")
// Market Search
const { daangnSingleSearch } = require("./search/daangnSearch");
......@@ -18,7 +19,7 @@ const fs = require("fs");
// Cron for Mamul Notification
const schedule = require("node-schedule");
const job = schedule.scheduleJob("0 */1 * * *", () => {
checkMamul(client);
multiCheckMamul(client);
});
// Database APIs
......@@ -39,20 +40,23 @@ const config = {
};
// Cron for Mamul Notification
const { checkMamul } = require("./checkMamul/checkMamul");
const { multiCheckMamul, checkMamul } = require("./check/checkMamul");
const { checkKeywords } = require("./check/checkKeywords")
// Line chat bot client & event
const client = new line.Client(config);
let waitNewMamulList = []; // 매물 키워드 입력 기다리는 목록
let waitDeleteMamulList = []; // 매물 삭제 키워드 입력 기다리는 목록
function handleEvent(event) {
if (event.type !== "message" || event.message.type !== "text") {
console.log(event);
if (event.type == "postback") {
if (event.postback.data == "new") {
var found = waitNewMamulList.indexOf(event.source.userId);
if (found == -1) {
if (event.postback.data == "newKeyword") {
var foundNew = waitNewMamulList.indexOf(event.source.userId);
if (foundNew == -1) {
waitNewMamulList.push(event.source.userId);
console.log(`waitNewMamulList Changed : ${waitNewMamulList}`);
return Promise.resolve(
......@@ -69,37 +73,36 @@ function handleEvent(event) {
})
);
}
} else if (event.postback.data == "check") {
} else if (event.postback.data == "checkItems") {
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "flex",
altText: "등록된 매물",
contents: setFlexMessage(
"daangn",
"RTX 3080",
"1000000",
"https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside",
"https://www.daangn.com/articles/403755360",
"채굴X, 흡연X, 반려동물X 입니다.\n직거래 희망하며, 쿨거래시 네고 1만원 가능합니다."
),
})
checkMamul(client, event.source.userId),
);
} else if (event.postback.data == "deleteKeyword") {
var foundDelete = waitDeleteMamulList.indexOf(event.source.userId);
if (foundDelete == -1) {
waitDeleteMamulList.push(event.source.userId);
console.log(`waitDeleteMamulList Changed : ${waitDeleteMamulList}`);
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "text",
text: "삭제할 매물 키워드를 알려주세요!",
})
);
}
} else if (event.postback.data == "checkKeywords") {
return Promise.resolve(
checkKeywords(client, event)
)
}
}
return Promise.resolve(null);
} else {
console.log(event);
var found = waitNewMamulList.indexOf(event.source.userId);
if (found == -1) {
return Promise.resolve(
marketMultiSearch(event.message.text).then((res) => {
client.pushMessage(event.source.userId, setCarouselMessage(res));
})
);
} else {
var foundNew = waitNewMamulList.indexOf(event.source.userId);
if (foundNew != -1) {
// TODO: 서버에 키워드 등록하는 api
waitNewMamulList.splice(found, 1);
console.log(waitNewMamulList[found]);
waitNewMamulList.splice(foundNew, 1);
console.log(waitNewMamulList[foundNew]);
return Promise.resolve(
db.addKeyword(event.message.text, event.source.userId),
client.replyMessage(event.replyToken, {
......@@ -111,6 +114,19 @@ function handleEvent(event) {
})
);
}
var foundDelete = waitDeleteMamulList.indexOf(event.source.userId);
if (foundDelete != -1) {
waitDeleteMamulList.splice(foundDelete, 1);
console.log(waitDeleteMamulList[foundDelete]);
return Promise.resolve(
db.deleteKeyword(event.source.userId, event.message.text),
client.replyMessage(event.replyToken, {
type: "text",
text: `매물이 삭제되었습니다!\n삭제된 매물: ${event.message.text}`,
})
)
}
}
}
......@@ -165,52 +181,83 @@ module.exports = { handleEvent, config };
/*리치메뉴 설정*/
// let richMenu = {
// size: {
// width: 2500,
// height: 843,
// },
// selected: false,
// name: "Nice richmenu",
// chatBarText: "Tap to open",
// areas: [
// {
// bounds: {
// x: 0,
// y: 0,
// width: 1250,
// height: 843,
// },
// action: {
// type: "postback",
// label: "new",
// data: "new",
// displayText: "키워드 등록",
// inputOption: "openKeyboard",
// fillInText: "",
// },
// size: {
// width: 2006,
// height: 827,
// },
// {
// bounds: {
// x: 1250,
// y: 0,
// width: 1250,
// height: 843,
// },
// action: {
// type: "postback",
// label: "check",
// data: "check",
// displayText: "최신 매물 확인",
// inputOption: "openKeyboard",
// fillInText: "",
// },
// },
// ],
// selected: false,
// name: "Real richMenu",
// chatBarText: "메뉴 열기",
// areas: [
// {
// bounds: {
// x: 0,
// y: 0,
// width: 1003,
// height: 413,
// },
// action: {
// type: "postback",
// label: "newKeyword",
// data: "newKeyword",
// displayText: "키워드 추가",
// inputOption: "openKeyboard",
// fillInText: "",
// },
// },
// {
// bounds: {
// x: 1003,
// y: 0,
// width: 1003,
// height: 413,
// },
// action: {
// type: "postback",
// label: "deleteKeyword",
// data: "deleteKeyword",
// displayText: "키워드 삭제",
// inputOption: "openKeyboard",
// fillInText: "",
// },
// },
// {
// bounds: {
// x: 0,
// y: 413,
// width: 1003,
// height: 414,
// },
// action: {
// type: "postback",
// label: "checkKeywords",
// data: "checkKeywords",
// displayText: "키워드 확인",
// },
// },
// {
// bounds: {
// x: 1003,
// y: 413,
// width: 1003,
// height: 414,
// },
// action: {
// type: "postback",
// label: "checkItems",
// data: "checkItems",
// displayText: "매물 즉시 검색",
// },
// },
// ],
// };
//// 등록
// client.createRichMenu(richMenu).then((richMenuId) => console.log(richMenuId));
// 등록
// client.createRichMenu(richMenu).then((richMenuId) => {
// console.log(richMenuId)
// });
// client.setRichMenuImage(
// "richmenu-183eff606f059b8244f0a625b54bddf1",
// fs.createReadStream("./static/img/richMenu.jpg")
// );
// client.setDefaultRichMenu("richmenu-183eff606f059b8244f0a625b54bddf1");
// "richmenu-ab4bba1c3c9235be50e3e8924fabd940",
// fs.createReadStream("./static/image/richMenu.png")
// );
// client.setDefaultRichMenu("richmenu-ab4bba1c3c9235be50e3e8924fabd940");
//
......
function setKeywordsFlexMessage(keywords) {
let flexMessage = {
type: "bubble",
body: {
type: "box",
layout: "vertical",
contents: [
{
type: "text",
text: "매무리 봇",
weight: "bold",
color: "#1DB446",
size: "sm",
},
{
type: "text",
text: "등록된 키워드",
weight: "bold",
size: "xxl",
margin: "md"
},
{
type: "separator",
margin: "xxl"
},
{
type: "box",
layout: "vertical",
contents: [],
margin: "md"
}
]
},
};
for (let i = 0; i < keywords.length; i++) {
const textbox = createKeywordTextBox(keywords[i]);
flexMessage.body.contents[3].contents.push(textbox);
}
return {
type: "flex",
altText: "키워드 조회 오류",
contents: flexMessage
}
}
function createKeywordTextBox(keyword) {
return {
type: "text",
text: keyword,
size: "lg",
align: "center",
margin: "md"
}
}
module.exports = setKeywordsFlexMessage;
\ No newline at end of file