-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
76 lines (65 loc) · 2.19 KB
/
slack.go
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
package main
import (
"context"
"fmt"
"net/http"
sparta "github.com/mweagle/Sparta"
"github.com/mweagle/Sparta/aws/events"
"github.com/sirupsen/logrus"
)
////////////////////////////////////////////////////////////////////////////////
// Hello world event handler
//
func helloSlackbot(ctx context.Context, apiRequest events.APIGatewayRequest) (map[string]interface{}, error) {
logger, _ := ctx.Value(sparta.ContextKeyLogger).(*logrus.Logger)
bodyParams, bodyParamsOk := apiRequest.Body.(map[string]interface{})
if !bodyParamsOk {
return nil, fmt.Errorf("Failed to type convert body. Type: %T", apiRequest.Body)
}
logger.WithFields(logrus.Fields{
"BodyType": fmt.Sprintf("%T", bodyParams),
"BodyValue": fmt.Sprintf("%+v", bodyParams),
}).Info("Slack slashcommand values")
// 2. Create the response
// Slack formatting:
// https://api.slack.com/docs/formatting
responseText := "Here's what I understood"
for eachKey, eachParam := range bodyParams {
responseText += fmt.Sprintf("\n*%s*: %+v", eachKey, eachParam)
}
// 4. Setup the response object:
// https://api.slack.com/slash-commands, "Responding to a command"
responseData := map[string]interface{}{
"response_type": "in_channel",
"text": responseText,
"mrkdwn": true,
}
return responseData, nil
}
func spartaLambdaFunctions(api *sparta.API) []*sparta.LambdaAWSInfo {
var lambdaFunctions []*sparta.LambdaAWSInfo
lambdaFn := sparta.HandleAWSLambda(sparta.LambdaName(helloSlackbot),
helloSlackbot,
sparta.IAMRoleDefinition{})
if nil != api {
apiGatewayResource, _ := api.NewResource("/slack", lambdaFn)
_, err := apiGatewayResource.NewMethod("POST", http.StatusCreated, http.StatusCreated)
if nil != err {
panic("Failed to create /hello resource")
}
}
return append(lambdaFunctions, lambdaFn)
}
////////////////////////////////////////////////////////////////////////////////
// Main
func main() {
// Register the function with the API Gateway
apiStage := sparta.NewStage("v1")
apiGateway := sparta.NewAPIGateway("SpartaSlackbot", apiStage)
// Deploy it
sparta.Main("SpartaSlackbot",
fmt.Sprintf("Sparta app that responds to Slack commands"),
spartaLambdaFunctions(apiGateway),
apiGateway,
nil)
}