유명현

EDIT: .gitignore (.env) & ADD: line flexMessage

const express = require("express");
const line = require("@line/bot-sdk");
const setFlexMessage = require("./pushMessage");
require("dotenv").config();
console.log(process.env.channelAccessToken, process.env.channelSecret);
const config = {
channelAccessToken: process.env.channelAccessToken,
channelSecret: process.env.channelSecret,
......@@ -22,14 +22,22 @@ function handleEvent(event) {
console.log(event);
// push
client.pushMessage(event.source.userId, {
type: "text",
text: "hihihi",
});
return client.replyMessage(event.replyToken, {
type: "text",
text: event.message.text,
// 매물 테스트 알림
return 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"
),
});
// return client.replyMessage(event.replyToken, {
// type: "text",
// text: event.message.text,
// });
}
app.listen(3000);
......
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;