-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
210 lines (200 loc) · 5.71 KB
/
main.gs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function doPost(e) {
let body = JSON.parse(e.postData.contents);
Logger.log("Received: %s", body);
// Slack Event APIのURL verification
if (body.type === "url_verification") {
return ContentService.createTextOutput(body.challenge).setMimeType(
ContentService.MimeType.TEXT
);
}
// app_mention以外のイベントは無視
if (body.event.type !== "app_mention") {
return ContentService.createTextOutput(
"Cannot process this event type"
).setMimeType(ContentService.MimeType.TEXT);
}
let message = body.event.text;
let channel = body.event.channel;
let thread_ts = body.event.ts;
try {
// authorizationsに含まれるbotのIDを取得
let botIds = body.authorizations
.filter((u) => u.is_bot)
.map((u) => u.user_id);
// botIdsに含まれるIDをメッセージから削除
let query = message
.replace(new RegExp(`<@(${botIds.join("|")})>`, "g"), "")
.trim();
// 検索ワードがない場合はエラーを返す
if (!query) {
sendSlackMessage({
channel: channel,
thread_ts: thread_ts,
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: ":warning: 検索ワードを認識できません",
emoji: true,
},
},
],
});
return ContentService.createTextOutput("Error").setMimeType(
ContentService.MimeType.TEXT
);
}
// CacheServiceを使って多重起動防止
const cacheKey = channel + ":" + thread_ts;
if (CacheService.getScriptCache().get(cacheKey)) {
return ContentService.createTextOutput("OK").setMimeType(
ContentService.MimeType.TEXT
);
}
CacheService.getScriptCache().put(cacheKey, query);
// 検索処理
return wpSearch(query, { channel, thread_ts });
} catch (e) {
sendSlackMessage({
channel: channel,
thread_ts: thread_ts,
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: ":warning: エラーが発生しました",
emoji: true,
},
},
],
});
return ContentService.createTextOutput("Error: " + e).setMimeType(
ContentService.MimeType.TEXT
);
}
}
function wpSearch(query, eventData) {
let baseUrl =
PropertiesService.getScriptProperties().getProperty("WP_BASE_URL");
let wpUser =
PropertiesService.getScriptProperties().getProperty("WP_USERNAME");
let wpPass =
PropertiesService.getScriptProperties().getProperty("WP_PASSWORD");
let url = baseUrl + "/wp-json/wp/v2/search?search=";
let auth = Utilities.base64Encode(`${wpUser}:${wpPass}`);
let response = UrlFetchApp.fetch(url + query, {
method: "GET",
headers: {
Authorization: `Basic ${auth}`,
},
});
let contents = JSON.parse(response.getContentText()).map((content) => {
return {
title: content.title,
url: content.url,
};
});
let blockKit = createBlockKit(query, contents);
let slackMessage = {
channel: eventData.channel,
thread_ts: eventData.thread_ts,
blocks: blockKit,
};
return sendSlackMessage(slackMessage);
}
// Slackにメッセージを送信する
function sendSlackMessage(response) {
let slacktoken =
PropertiesService.getScriptProperties().getProperty("SLACK_BOT_TOKEN");
let url = "https://slack.com/api/chat.postMessage";
let options = {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + slacktoken,
},
payload: JSON.stringify(response),
};
UrlFetchApp.fetch(url, options);
return ContentService.createTextOutput("OK").setMimeType(
ContentService.MimeType.TEXT
);
}
// 検索結果を元にBlock Kitを生成する
function createBlockKit(query, contents) {
let blockKit = [];
let template = JSON.parse(JSON.stringify(blockKitTemplate)); // ミューテーションを避けるためにディープコピーする
// ヘッダーテキストにクエリを追加
template.header.text.text = query + " " + template.header.text.text;
// ヘッダーをblockKitに追加
blockKit.push(template.header);
// 結果数をblockKitに追加
let resultCount = JSON.parse(JSON.stringify(template.resultCount));
resultCount.elements[1].text = contents.length + resultCount.elements[1].text;
blockKit.push(resultCount);
// ディバイダーをblockKitに追加
blockKit.push(template.divider);
// 検索結果をblockKitに追加
let results = JSON.parse(JSON.stringify(template.results));
results.elements[0].elements = contents.map((content) => {
let item = JSON.parse(JSON.stringify(template.item));
item.elements[0].url = content.url;
item.elements[0].text = content.title;
return item;
});
blockKit.push(results);
return blockKit;
}
// Block Kitのテンプレート
let blockKitTemplate = {
header: {
type: "header",
text: {
type: "plain_text",
text: "を含む記事の検索結果",
emoji: true,
},
},
resultCount: {
type: "context",
elements: [
{
type: "image",
image_url: "https://github.com/identicons/neko-room.png",
alt_text: "your-logo",
},
{
type: "mrkdwn",
text: "件の記事が見つかりました!",
},
],
},
divider: {
type: "divider",
},
results: {
type: "rich_text",
elements: [
{
type: "rich_text_list",
style: "bullet",
elements: [],
},
],
},
item: {
type: "rich_text_section",
elements: [
{
type: "link",
url: "https://example.com",
text: "page-title",
style: {
bold: true,
},
},
],
},
};