-
Notifications
You must be signed in to change notification settings - Fork 36
/
create-webhook.js
72 lines (65 loc) · 2.1 KB
/
create-webhook.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
const https = require('https')
exports.handler = function(event, context) {
// https://lambda/?code=xxx&state=
// If this is the initial request.
if (event.httpMethod === 'GET' && event.queryStringParameters.code) {
// Retrieve the initial auth
// https://slack.com/api/oauth.access?client_id&client_secret&code
let { client_id, client_secret } = process.env
const optionspost = {
host: 'slack.com',
path: `/api/oauth.access?client_id=${client_id}&client_secret=${client_secret}&code=${event.queryStringParameters.code}`,
method: 'GET'
}
let body = ''
var reqPost = https
.request(optionspost, function(res) {
res.on('data', function(chunk) {
body += chunk
})
res.on('end', function() {
let jsonStr = JSON.parse(body)
// {"ok":true,
// "access_token":"xoxp-xxx-xxx-xxx",
// "scope":"identify,incoming-webhook,chat:write:bot",
// "user_id":"xxx","team_name":"xxx","team_id":"xxx"}
if (jsonStr.ok) {
// context.succeed({
// 'statusCode': 200,
// body: JSON.stringify({'ok': true, 'access_token': jsonStr.access_token}),
// })
context.succeed({
statusCode: 301,
headers: {
Location: `https://juliuscc.github.io/semantic-release-slack-bot/index.html?access_token=${jsonStr.access_token}`
}
})
} else {
context.succeed({
statusCode: 401,
body: JSON.stringify({
ok: false,
message: 'Invalid credentials'
})
})
}
})
})
.on('error', function(e) {
context.succeed({
statusCode: 500,
body: JSON.stringify({
ok: false,
message: 'Could not connect to slack.'
})
})
})
reqPost.write('')
reqPost.end()
} else {
context.succeed({
statusCode: 400,
body: JSON.stringify({ ok: false, message: 'Invalid request.' })
})
}
}