유명현

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

1 const express = require("express"); 1 const express = require("express");
2 const line = require("@line/bot-sdk"); 2 const line = require("@line/bot-sdk");
3 +const setFlexMessage = require("./pushMessage");
3 require("dotenv").config(); 4 require("dotenv").config();
4 -console.log(process.env.channelAccessToken, process.env.channelSecret);
5 const config = { 5 const config = {
6 channelAccessToken: process.env.channelAccessToken, 6 channelAccessToken: process.env.channelAccessToken,
7 channelSecret: process.env.channelSecret, 7 channelSecret: process.env.channelSecret,
...@@ -22,14 +22,22 @@ function handleEvent(event) { ...@@ -22,14 +22,22 @@ function handleEvent(event) {
22 console.log(event); 22 console.log(event);
23 23
24 // push 24 // push
25 - client.pushMessage(event.source.userId, { 25 + // 매물 테스트 알림
26 - type: "text", 26 + return client.pushMessage(event.source.userId, {
27 - text: "hihihi", 27 + type: "flex",
28 - }); 28 + altText: "새로운 매물이 왔어요!",
29 - return client.replyMessage(event.replyToken, { 29 + contents: setFlexMessage(
30 - type: "text", 30 + "daangn",
31 - text: event.message.text, 31 + "RTX 3080",
32 + "1000000",
33 + "https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside",
34 + "https://www.daangn.com/articles/403755360"
35 + ),
32 }); 36 });
37 + // return client.replyMessage(event.replyToken, {
38 + // type: "text",
39 + // text: event.message.text,
40 + // });
33 } 41 }
34 42
35 app.listen(3000); 43 app.listen(3000);
......
1 +function priceToString(price) {
2 + return price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
3 +}
4 +
5 +function setFlexMessage(platform, name, price, thumbnailUrl, itemUrl) {
6 + let koreanPlatformName = "";
7 + if (platform === "daangn") {
8 + koreanPlatformName = "당근";
9 + } else if (platform === "joongna") {
10 + koreanPlatformName = "중고나라";
11 + } else if (platform === "bunjang") {
12 + koreanPlatformName = "번개나라";
13 + } else {
14 + koreanPlatformName = "Unknown";
15 + }
16 +
17 + let flexMessage = {
18 + type: "bubble",
19 + hero: {
20 + type: "image",
21 + url: thumbnailUrl,
22 + size: "full",
23 + aspectRatio: "20:13",
24 + aspectMode: "cover",
25 + action: {
26 + type: "uri",
27 + uri: itemUrl,
28 + },
29 + },
30 + body: {
31 + type: "box",
32 + layout: "vertical",
33 + contents: [
34 + {
35 + type: "text",
36 + text: name,
37 + weight: "bold",
38 + size: "xl",
39 + },
40 + // {
41 + // type: "box",
42 + // layout: "baseline",
43 + // margin: "md",
44 + // contents: [
45 + // {
46 + // type: "icon",
47 + // size: "sm",
48 + // url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
49 + // },
50 + // {
51 + // type: "icon",
52 + // size: "sm",
53 + // url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
54 + // },
55 + // {
56 + // type: "icon",
57 + // size: "sm",
58 + // url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
59 + // },
60 + // {
61 + // type: "icon",
62 + // size: "sm",
63 + // url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
64 + // },
65 + // {
66 + // type: "icon",
67 + // size: "sm",
68 + // url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png",
69 + // },
70 + // {
71 + // type: "text",
72 + // text: "4.0",
73 + // size: "sm",
74 + // color: "#999999",
75 + // margin: "md",
76 + // flex: 0,
77 + // },
78 + // ],
79 + // },
80 + {
81 + type: "box",
82 + layout: "vertical",
83 + margin: "lg",
84 + spacing: "sm",
85 + contents: [
86 + {
87 + type: "box",
88 + layout: "baseline",
89 + spacing: "sm",
90 + contents: [
91 + {
92 + type: "text",
93 + text: "플랫폼",
94 + color: "#aaaaaa",
95 + size: "sm",
96 + flex: 1,
97 + },
98 + {
99 + type: "text",
100 + text: koreanPlatformName,
101 + wrap: true,
102 + color: "#666666",
103 + size: "sm",
104 + flex: 5,
105 + },
106 + ],
107 + },
108 + {
109 + type: "box",
110 + layout: "baseline",
111 + spacing: "sm",
112 + contents: [
113 + {
114 + type: "text",
115 + text: "가격",
116 + color: "#aaaaaa",
117 + size: "sm",
118 + flex: 1,
119 + },
120 + {
121 + type: "text",
122 + text: priceToString(price * 1) + "원",
123 + wrap: true,
124 + color: "#666666",
125 + size: "sm",
126 + flex: 5,
127 + },
128 + ],
129 + },
130 + ],
131 + },
132 + ],
133 + },
134 + footer: {
135 + type: "box",
136 + layout: "vertical",
137 + spacing: "sm",
138 + contents: [
139 + {
140 + type: "button",
141 + style: "link",
142 + height: "sm",
143 + action: {
144 + type: "uri",
145 + label: "매물 확인",
146 + uri: itemUrl,
147 + },
148 + },
149 + {
150 + type: "box",
151 + layout: "vertical",
152 + contents: [],
153 + margin: "sm",
154 + },
155 + ],
156 + flex: 0,
157 + },
158 + };
159 + return flexMessage;
160 + //return JSON.stringify(flexMessage);
161 +}
162 +
163 +module.exports = setFlexMessage;