유명현

ADD: richMenu + waitNewMamulList

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("./apis/setFlexMessage"); 3 const setFlexMessage = require("./apis/setFlexMessage");
4 +const fs = require("fs");
5 +
4 require("dotenv").config(); 6 require("dotenv").config();
5 const config = { 7 const config = {
6 channelAccessToken: process.env.channelAccessToken, 8 channelAccessToken: process.env.channelAccessToken,
...@@ -15,30 +17,138 @@ app.post("/webhook", line.middleware(config), (req, res) => { ...@@ -15,30 +17,138 @@ app.post("/webhook", line.middleware(config), (req, res) => {
15 }); 17 });
16 18
17 const client = new line.Client(config); 19 const client = new line.Client(config);
20 +
21 +let waitNewMamulList = []; // 매물 키워드 입력 기다리는 목록
22 +
18 function handleEvent(event) { 23 function handleEvent(event) {
19 if (event.type !== "message" || event.message.type !== "text") { 24 if (event.type !== "message" || event.message.type !== "text") {
25 + console.log(event);
26 + if (event.type == "postback") {
27 + if (event.postback.data == "new") {
28 + var found = waitNewMamulList.indexOf(event.source.userId);
29 + if (found == -1) {
30 + waitNewMamulList.push(event.source.userId);
31 + console.log(waitNewMamulList);
32 + return Promise.resolve(
33 + client.replyMessage(event.replyToken, {
34 + type: "text",
35 + text: "등록할 매물 키워드를 알려주세요!",
36 + })
37 + );
38 + } else {
39 + return Promise.resolve(
40 + client.replyMessage(event.replyToken, {
41 + type: "text",
42 + text: "등록할 매물 키워드를 알려주세요!",
43 + })
44 + );
45 + }
46 + } else if (event.postback.data == "check") {
47 + return Promise.resolve(
48 + client.replyMessage(event.replyToken, {
49 + type: "flex",
50 + altText: "등록된 매물",
51 + contents: setFlexMessage(
52 + "daangn",
53 + "RTX 3080",
54 + "1000000",
55 + "https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside",
56 + "https://www.daangn.com/articles/403755360"
57 + ),
58 + })
59 + );
60 + }
61 + }
20 return Promise.resolve(null); 62 return Promise.resolve(null);
63 + } else {
64 + console.log(event);
65 + var found = waitNewMamulList.indexOf(event.source.userId);
66 + if (found == -1) {
67 + return Promise.resolve(
68 + client.replyMessage(event.replyToken, {
69 + type: "text",
70 + text: "왼쪽 하단 메뉴버튼(☰)을 클릭해 상호작용 해주세요!",
71 + })
72 + );
73 + } else {
74 + // TODO: 서버에 키워드 등록하는 api
75 + waitNewMamulList.splice(found, 1);
76 + console.log(waitNewMamulList[found]);
77 + return Promise.resolve(
78 + client.replyMessage(event.replyToken, {
79 + type: "text",
80 + text: "매물이 등록되었습니다!\n등록된 매물: " + event.message.text,
81 + })
82 + );
83 + }
21 } 84 }
22 - console.log(event);
23 -
24 - // push
25 - // 매물 테스트 알림
26 - return client.pushMessage(event.source.userId, {
27 - type: "flex",
28 - altText: "새로운 매물이 왔어요!",
29 - contents: setFlexMessage(
30 - "daangn",
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 - ),
36 - });
37 - // return client.replyMessage(event.replyToken, {
38 - // type: "text",
39 - // text: event.message.text,
40 - // });
41 } 85 }
42 86
43 -app.listen(3000); 87 +const port = 1231;
44 -console.log("listening..."); 88 +app.listen(port);
89 +console.log(`listening...\nport : ${port}`);
90 +
91 +/*Push Message*/
92 +// client.pushMessage(event.source.userId, {
93 +// type: "flex",
94 +// altText: "새로운 매물이 왔어요!",
95 +// contents: setFlexMessage(
96 +// "daangn",
97 +// "RTX 3080",
98 +// "1000000",
99 +// "https://dnvefa72aowie.cloudfront.net/origin/article/202205/94cdd237258671d5806a70f64ab2b3c7dcd790da0384b394ef5809fe10c08ced.webp?q=95&s=1440x1440&t=inside",
100 +// "https://www.daangn.com/articles/403755360"
101 +// ),
102 +// })
103 +
104 +/*리치메뉴 설정*/
105 +// let richMenu = {
106 +// size: {
107 +// width: 2500,
108 +// height: 843,
109 +// },
110 +// selected: false,
111 +// name: "Nice richmenu",
112 +// chatBarText: "Tap to open",
113 +// areas: [
114 +// {
115 +// bounds: {
116 +// x: 0,
117 +// y: 0,
118 +// width: 1250,
119 +// height: 843,
120 +// },
121 +// action: {
122 +// type: "postback",
123 +// label: "new",
124 +// data: "new",
125 +// displayText: "키워드 등록",
126 +// inputOption: "openKeyboard",
127 +// fillInText: "",
128 +// },
129 +// },
130 +// {
131 +// bounds: {
132 +// x: 1250,
133 +// y: 0,
134 +// width: 1250,
135 +// height: 843,
136 +// },
137 +// action: {
138 +// type: "postback",
139 +// label: "check",
140 +// data: "check",
141 +// displayText: "최신 매물 확인",
142 +// inputOption: "openKeyboard",
143 +// fillInText: "",
144 +// },
145 +// },
146 +// ],
147 +// };
148 +//// 등록
149 +// client.createRichMenu(richMenu).then((richMenuId) => console.log(richMenuId));
150 +// client.setRichMenuImage(
151 +// "richmenu-183eff606f059b8244f0a625b54bddf1",
152 +// fs.createReadStream("./static/img/richMenu.jpg")
153 +// );
154 +// client.setDefaultRichMenu("richmenu-183eff606f059b8244f0a625b54bddf1");
......