This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
/
app.js
196 lines (186 loc) · 5.63 KB
/
app.js
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
const { App } = require('@slack/bolt');
// 環境変数の SLACK_BOT_TOKEN, SLACK_SIGNING_SECRET が設定されていることが前提
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.use(args => {
console.log(JSON.stringify(args));
args.next();
});
const restaurants = [
{ name: '割烹よし田', url: 'https://tabelog.com/fukuoka/A4001/A400103/40000692/' },
{ name: '真', url: 'https://tabelog.com/fukuoka/A4001/A400103/40003911/' },
{ name: '新三浦 天神店', url: 'https://tabelog.com/fukuoka/A4001/A400103/40000066/' },
{ name: '利花苑 大名本店', url: 'https://tabelog.com/fukuoka/A4001/A400104/40000443/' },
{ name: '106 サウスインディアン 福岡天神店', url: 'https://tabelog.com/fukuoka/A4001/A400103/40041110/' },
];
function getRecommendationBlocks() {
const restaurant = restaurants[Math.floor(Math.random() * restaurants.length)];
// blocks
return [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `:shallow_pan_of_food: <${restaurant.url}|${restaurant.name}> はいかがですか?`
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"action_id": "find-another", // このキー名で app.action と連動する
"text": {
"type": "plain_text",
"text": "他の店を見る",
"emoji": true
},
"value": "next"
},
{
"type": "button",
"action_id": "add-review", // このキー名で app.action と連動する
"text": {
"type": "plain_text",
"text": "レビューをつける",
"emoji": true
},
"value": JSON.stringify(restaurant) // このレストラン情報を次の処理へ引回す
},
]
},
];
}
app.command('/lunch', async ({ ack, respond }) => {
ack();
// response_url を使って応答する
respond({
response_type: 'ephemeral', // このコマンドを実行したユーザにだけ見えるメッセージ、say の場合はチャンネルに普通に post される
blocks: getRecommendationBlocks()
});
});
app.action('find-another', async ({ body, context, ack, respond }) => {
ack();
respond({
response_type: 'ephemeral', // 再びこのユーザにだけ見えるメッセージ
replace_original: true, // もともとあったメッセージを置き換える
blocks: getRecommendationBlocks()
})
});
app.action('add-review', async ({ body, context, ack }) => {
ack();
const sentData = JSON.parse(body.actions[0].value);
const restaurantName = sentData.name;
app.client.views.open({
"token": context.botToken,
"trigger_id": body.trigger_id,
"view": {
"type": "modal",
"callback_id": "modal-callback-id",
"title": {
"type": "plain_text",
"text": `「${restaurantName}」を評価`
},
"submit": {
"type": "plain_text",
"text": "送信する"
},
"close": {
"type": "plain_text",
"text": "閉じる"
},
"blocks": [
{
"type": "input",
"block_id": "rating",
"label": {
"type": "plain_text",
"text": "評価"
},
"element": {
"action_id": "single_action",
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": "Select an item",
"emoji": true
},
"options": [
{
"text": {
"type": "plain_text",
"text": ":star:",
"emoji": true
},
"value": "1"
},
{
"text": {
"type": "plain_text",
"text": ":star::star:",
"emoji": true
},
"value": "2"
},
{
"text": {
"type": "plain_text",
"text": ":star::star::star:",
"emoji": true
},
"value": "3"
},
]
}
},
{
"type": "input",
"block_id": "date-of-visit",
"label": {
"type": "plain_text",
"text": "行った日"
},
"element": {
"type": "datepicker",
"action_id": "single_action",
"initial_date": "2019-09-26",
"placeholder": {
"type": "plain_text",
"text": "行った日を選んでください",
"emoji": true
}
}
},
{
"type": "input",
"block_id": "comment",
"label": {
"type": "plain_text",
"text": "コメント"
},
"element": {
"type": "plain_text_input",
"action_id": "single_action",
"multiline": true,
}
},
],
}
})
.then(res => console.log(JSON.stringify(res)))
.catch(err => console.log(JSON.stringify(err)));
});
app.view('modal-callback-id', ({ body, ack }) => {
ack();
const stateValues = body.view.state.values;
// 本当はここで何かする
console.log(JSON.stringify(stateValues));
});
(async () => {
// 3000 ポートを listen するアプリを起動します
// (Bolt の内部実装はデフォルトでは Express.js です)
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();