-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
296 lines (271 loc) · 7.52 KB
/
Copy pathapp.js
File metadata and controls
296 lines (271 loc) · 7.52 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const { App } = require('@slack/bolt')
const axios = require('axios')
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN
})
// Store for user API keys
const userKeys = new Map()
// Home tab
app.event('app_home_opened', async ({ event, client }) => {
await client.views.publish({
user_id: event.user,
view: {
type: 'home',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: '*🚀 Welcome to BlackRoad!*\n\nDeploy and manage your apps directly from Slack.'
}
},
{
type: 'actions',
elements: [
{
type: 'button',
text: { type: 'plain_text', text: '⚡ Quick Deploy' },
action_id: 'quick_deploy',
style: 'primary'
},
{
type: 'button',
text: { type: 'plain_text', text: '📊 View Stats' },
action_id: 'view_stats'
},
{
type: 'button',
text: { type: 'plain_text', text: '📦 Deployments' },
action_id: 'list_deployments'
}
]
}
]
}
})
})
// Slash command: /deploy
app.command('/deploy', async ({ command, ack, client }) => {
await ack()
await client.views.open({
trigger_id: command.trigger_id,
view: {
type: 'modal',
callback_id: 'deploy_modal',
title: { type: 'plain_text', text: 'Deploy App' },
submit: { type: 'plain_text', text: 'Deploy' },
blocks: [
{
type: 'input',
block_id: 'app_name',
element: {
type: 'plain_text_input',
action_id: 'name',
placeholder: { type: 'plain_text', text: 'my-awesome-app' }
},
label: { type: 'plain_text', text: 'App Name' }
},
{
type: 'input',
block_id: 'app_source',
element: {
type: 'plain_text_input',
action_id: 'source',
placeholder: { type: 'plain_text', text: 'github.com/user/repo' }
},
label: { type: 'plain_text', text: 'Source (optional)' },
optional: true
}
]
}
})
})
// Deploy modal submission
app.view('deploy_modal', async ({ ack, body, view, client }) => {
await ack()
const userId = body.user.id
const apiKey = userKeys.get(userId)
if (!apiKey) {
await client.chat.postMessage({
channel: userId,
text: '❌ Please set your API key first with `/blackroad-login`'
})
return
}
const appName = view.state.values.app_name.name.value
const appSource = view.state.values.app_source.source.value
// Send deploying message
const msg = await client.chat.postMessage({
channel: userId,
text: '⏳ Deploying...',
blocks: [
{
type: 'section',
text: { type: 'mrkdwn', text: `⏳ Deploying *${appName}*...` }
}
]
})
try {
const response = await axios.post('https://api.blackroad.io/v1/deployments', {
name: appName,
source: appSource || 'slack'
}, {
headers: { 'Authorization': `Bearer ${apiKey}` }
})
await client.chat.update({
channel: userId,
ts: msg.ts,
text: '✅ Deployed successfully!',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `✅ *Deployed successfully!*\n\n*Name:* ${response.data.name}\n*URL:* ${response.data.url}\n*Status:* ${response.data.status}`
}
},
{
type: 'actions',
elements: [
{
type: 'button',
text: { type: 'plain_text', text: '🌐 Open' },
url: response.data.url
},
{
type: 'button',
text: { type: 'plain_text', text: '📊 Dashboard' },
url: 'https://blackroad.io/dashboard'
}
]
}
]
})
} catch (error) {
await client.chat.update({
channel: userId,
ts: msg.ts,
text: `❌ Deployment failed: ${error.message}`
})
}
})
// Slash command: /blackroad-stats
app.command('/blackroad-stats', async ({ command, ack, client }) => {
await ack()
const userId = command.user_id
const apiKey = userKeys.get(userId)
if (!apiKey) {
await client.chat.postEphemeral({
channel: command.channel_id,
user: userId,
text: '❌ Please set your API key first with `/blackroad-login`'
})
return
}
try {
const response = await axios.get('https://api.blackroad.io/v1/analytics?range=7d', {
headers: { 'Authorization': `Bearer ${apiKey}` }
})
await client.chat.postMessage({
channel: command.channel_id,
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: '*📊 Analytics (Last 7 Days)*'
}
},
{
type: 'section',
fields: [
{ type: 'mrkdwn', text: `*Requests:*\n${response.data.requests.toLocaleString()}` },
{ type: 'mrkdwn', text: `*Uptime:*\n${response.data.uptime}%` },
{ type: 'mrkdwn', text: `*Latency:*\n${response.data.latency}ms` },
{ type: 'mrkdwn', text: `*Users:*\n${response.data.users?.toLocaleString() || '-'}` }
]
}
]
})
} catch (error) {
await client.chat.postEphemeral({
channel: command.channel_id,
user: userId,
text: `❌ Error: ${error.message}`
})
}
})
// Slash command: /blackroad-login
app.command('/blackroad-login', async ({ command, ack, client }) => {
await ack()
await client.views.open({
trigger_id: command.trigger_id,
view: {
type: 'modal',
callback_id: 'login_modal',
title: { type: 'plain_text', text: 'Login to BlackRoad' },
submit: { type: 'plain_text', text: 'Save' },
blocks: [
{
type: 'input',
block_id: 'api_key',
element: {
type: 'plain_text_input',
action_id: 'key',
placeholder: { type: 'plain_text', text: 'Your API key' }
},
label: { type: 'plain_text', text: 'API Key' }
},
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: 'Get your API key at https://blackroad.io/settings/api-keys'
}
]
}
]
}
})
})
// Login modal submission
app.view('login_modal', async ({ ack, body, view }) => {
await ack()
const userId = body.user.id
const apiKey = view.state.values.api_key.key.value
userKeys.set(userId, apiKey)
})
// Quick deploy button
app.action('quick_deploy', async ({ ack, body, client }) => {
await ack()
await client.views.open({
trigger_id: body.trigger_id,
view: {
type: 'modal',
callback_id: 'deploy_modal',
title: { type: 'plain_text', text: 'Quick Deploy' },
submit: { type: 'plain_text', text: 'Deploy' },
blocks: [
{
type: 'input',
block_id: 'app_name',
element: {
type: 'plain_text_input',
action_id: 'name',
placeholder: { type: 'plain_text', text: 'my-app' }
},
label: { type: 'plain_text', text: 'App Name' }
}
]
}
})
})
// Start the app
;(async () => {
await app.start()
console.log('⚡ BlackRoad Slack Bot is running!')
})()