setCarouselMessage.js
4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const setFlexMessage = require("./setFlexMessage");
function setCarouselMessage(mamuls, keyword) {
let flexMessages = [];
let flexMessage = {};
if (
mamuls[0] == undefined
// &&
// mamuls[1] == undefined &&
// mamuls[2] == undefined &&
// mamuls[3] == undefined &&
// mamuls[4] == undefined &&
// mamuls[5] == undefined &&
// mamuls[6] == undefined &&
// mamuls[7] == undefined &&
// mamuls[8] == undefined &&
// mamuls[9] == undefined &&
// mamuls[10] == undefined &&
// mamuls[11] == undefined
) {
let nonMamulMessage = {
type: "flex",
altText: "매물 검색 에러",
contents: setFlexMessage(
"-",
"매물이 없습니다!",
"0",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Error.svg/515px-Error.svg.png",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Error.svg/515px-Error.svg.png",
"-"
),
};
return nonMamulMessage;
}
for (i = 0; i < mamuls.length; i++) {
if (mamuls[i] == undefined) {
continue;
}
try {
if (
mamuls[i]["platform"] === "bunjang" ||
mamuls[i]["platform"] === "번개장터"
) {
mamuls[i]["thumbnailUrl"] = mamuls[i]["thumbnailUrl"].replace(
"{",
"%7B"
);
mamuls[i]["thumbnailUrl"] = mamuls[i]["thumbnailUrl"].replace(
"}",
"%7D"
);
}
if (
mamuls[i]["thumbnailUrl"] == undefined ||
mamuls[i]["thumbnailUrl"] == ""
) {
mamuls[i]["thumbnailUrl"] =
"https://upload.wikimedia.org/wikipedia/commons/5/5f/Grey.PNG";
}
if (mamuls[i]["extraInfo"] == undefined || mamuls[i]["extraInfo"] == "") {
mamuls[i]["extraInfo"] = "없음";
} else {
if (
mamuls[i]["platform"] === "joongna" ||
mamuls[i]["platform"] === "중고나라"
) {
let searchDot = mamuls[i]["extraInfo"].indexOf("...");
if (searchDot !== -1) {
mamuls[i]["extraInfo"] = mamuls[i]["extraInfo"].slice(0, searchDot);
}
}
console.log(`unparsed extraInfo : \n${mamuls[i]["extraInfo"]}`);
let searchValue = "\n";
let pos = 0;
let foundPos = 0;
for (let j = 0; j < 4 && foundPos !== -1; j++) {
foundPos = mamuls[i]["extraInfo"].indexOf(searchValue, pos);
pos = foundPos + 1;
}
console.log(`pos: ${pos}`);
if (foundPos !== -1) {
mamuls[i]["extraInfo"] =
mamuls[i]["extraInfo"].slice(0, foundPos) + "\n...";
console.log(`parsed extraInfo : \n${mamuls[i]["extraInfo"]}`);
}
if (mamuls[i]["extraInfo"].length > 40) {
mamuls[i]["extraInfo"] =
mamuls[i]["extraInfo"].slice(0, 40) + "\n...";
console.log(`parsed extraInfo : \n${mamuls[i]["extraInfo"]}`);
}
}
// } else if (mamuls[i]["extraInfo"].length > 70) {
// mamuls[i]["extraInfo"] = mamuls[i]["extraInfo"].slice(0, 70) + "\n...";
// } else {
// }
// else if (mamuls[i]["extraInfo"].indexOf("\n") !== -1) {
// console.log(mamuls[i]["extraInfo"].indexOf("\n", 4));
// let slicePoint = mamuls[i]["extraInfo"].indexOf("\n", 4);
// mamuls[i]["extraInfo"] =
// mamuls[i]["extraInfo"].slice(0, slicePoint) + "\n...";
// }
flexMessage = setFlexMessage(
mamuls[i]["platform"],
mamuls[i]["name"],
mamuls[i]["price"],
mamuls[i]["thumbnailUrl"],
mamuls[i]["itemUrl"],
mamuls[i]["extraInfo"]
);
flexMessages.push(flexMessage);
} catch (err) {
console.log(err);
continue;
}
}
flexMessages[0]["header"] = {
type: "box",
layout: "horizontal",
contents: [
{ type: "text", text: "매무리 봇", size: "sm", color: "#1DB446" },
{
type: "text",
text: `키워드: ${keyword}`,
align: "end",
color: "#1DB446",
weight: "bold",
},
],
};
let carouselMessage = {
type: "flex",
altText: `유저님의 ${keyword}매물 알림!`,
contents: {
type: "carousel",
contents: flexMessages,
},
};
return carouselMessage;
}
module.exports = setCarouselMessage;