유명현

Merge branch 'feature/line_bot' into 'main'

Line bot 기본 기능 구현



See merge request !16
function priceToString(price) {
return price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function setFlexMessage(platform, name, price, thumbnailUrl, itemUrl) {
let koreanPlatformName = "";
if (platform === "daangn") {
koreanPlatformName = "당근";
} else if (platform === "joongna") {
koreanPlatformName = "중고나라";
} else if (platform === "bunjang") {
koreanPlatformName = "번개나라";
} else {
koreanPlatformName = "Unknown";
}
let flexMessage = {
type: "bubble",
hero: {
type: "image",
url: thumbnailUrl,
size: "full",
aspectRatio: "20:13",
aspectMode: "cover",
action: {
type: "uri",
uri: itemUrl,
},
},
body: {
type: "box",
layout: "vertical",
contents: [
{
type: "text",
text: name,
weight: "bold",
size: "xl",
},
// {
// type: "box",
// layout: "baseline",
// margin: "md",
// contents: [
// {
// type: "icon",
// size: "sm",
// url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
// },
// {
// type: "icon",
// size: "sm",
// url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
// },
// {
// type: "icon",
// size: "sm",
// url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
// },
// {
// type: "icon",
// size: "sm",
// url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
// },
// {
// type: "icon",
// size: "sm",
// url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png",
// },
// {
// type: "text",
// text: "4.0",
// size: "sm",
// color: "#999999",
// margin: "md",
// flex: 0,
// },
// ],
// },
{
type: "box",
layout: "vertical",
margin: "lg",
spacing: "sm",
contents: [
{
type: "box",
layout: "baseline",
spacing: "sm",
contents: [
{
type: "text",
text: "플랫폼",
color: "#aaaaaa",
size: "sm",
flex: 1,
},
{
type: "text",
text: koreanPlatformName,
wrap: true,
color: "#666666",
size: "sm",
flex: 5,
},
],
},
{
type: "box",
layout: "baseline",
spacing: "sm",
contents: [
{
type: "text",
text: "가격",
color: "#aaaaaa",
size: "sm",
flex: 1,
},
{
type: "text",
text: priceToString(price * 1) + "원",
wrap: true,
color: "#666666",
size: "sm",
flex: 5,
},
],
},
],
},
],
},
footer: {
type: "box",
layout: "vertical",
spacing: "sm",
contents: [
{
type: "button",
style: "link",
height: "sm",
action: {
type: "uri",
label: "매물 확인",
uri: itemUrl,
},
},
{
type: "box",
layout: "vertical",
contents: [],
margin: "sm",
},
],
flex: 0,
},
};
return flexMessage;
//return JSON.stringify(flexMessage);
}
module.exports = setFlexMessage;
var express = require("express");
var app = express();
const express = require("express");
const line = require("@line/bot-sdk");
const setFlexMessage = require("./apis/setFlexMessage");
const fs = require("fs");
app.get("/", function (req, res) {
res.send("<h1>hello express!</h1>");
require("dotenv").config();
const config = {
channelAccessToken: process.env.channelAccessToken,
channelSecret: process.env.channelSecret,
};
const app = express();
app.post("/webhook", line.middleware(config), (req, res) => {
Promise.all(req.body.events.map(handleEvent)).then((result) =>
res.json(result)
);
});
app.listen(3000);
const client = new line.Client(config);
let waitNewMamulList = []; // 매물 키워드 입력 기다리는 목록
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) {
waitNewMamulList.push(event.source.userId);
console.log(waitNewMamulList);
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "text",
text: "등록할 매물 키워드를 알려주세요!",
})
);
} else {
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "text",
text: "등록할 매물 키워드를 알려주세요!",
})
);
}
} else if (event.postback.data == "check") {
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"
),
})
);
}
}
return Promise.resolve(null);
} else {
console.log(event);
var found = waitNewMamulList.indexOf(event.source.userId);
if (found == -1) {
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "text",
text: "왼쪽 하단 메뉴버튼(☰)을 클릭해 상호작용 해주세요!",
})
);
} else {
// TODO: 서버에 키워드 등록하는 api
waitNewMamulList.splice(found, 1);
console.log(waitNewMamulList[found]);
return Promise.resolve(
client.replyMessage(event.replyToken, {
type: "text",
text: "매물이 등록되었습니다!\n등록된 매물: " + event.message.text,
})
);
}
}
}
const port = 1231;
app.listen(port);
console.log(`listening...\nport : ${port}`);
/*Push Message*/
// client.pushMessage(event.source.userId, {
// 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"
// ),
// })
/*리치메뉴 설정*/
// 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: "",
// },
// },
// {
// bounds: {
// x: 1250,
// y: 0,
// width: 1250,
// height: 843,
// },
// action: {
// type: "postback",
// label: "check",
// data: "check",
// displayText: "최신 매물 확인",
// inputOption: "openKeyboard",
// fillInText: "",
// },
// },
// ],
// };
//// 등록
// client.createRichMenu(richMenu).then((richMenuId) => console.log(richMenuId));
// client.setRichMenuImage(
// "richmenu-183eff606f059b8244f0a625b54bddf1",
// fs.createReadStream("./static/img/richMenu.jpg")
// );
// client.setDefaultRichMenu("richmenu-183eff606f059b8244f0a625b54bddf1");
......
This diff is collapsed. Click to expand it.
......@@ -15,6 +15,8 @@
"author": "",
"license": "MIT",
"dependencies": {
"@line/bot-sdk": "^7.5.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"nodemon": "^2.0.16"
}
......