-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·103 lines (87 loc) · 2.55 KB
/
main.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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/slack-go/slack"
"github.com/vbonduro/f1-fantasy-bot/internal/commands"
"github.com/vbonduro/f1-fantasy-bot/internal/slackutil"
)
var CommandHandler commands.Handler
func init() {
err := CommandHandler.Init()
if err != nil {
panic(err)
}
}
func slashCommandHandler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
command := slackutil.MakeSlashCommand(request.Body)
// todo(vbonduro): Should use slack secret instead, but not sure how just yet :(
if command.Token != os.Getenv("SLACK_VERIFICATION_TOKEN") {
return events.APIGatewayProxyResponse{
Body: "Invalid token.",
StatusCode: http.StatusOK,
}, nil
}
err := CommandHandler.Handle(command)
if err != nil {
log.Printf("Command " + command.Text + " failed with error " + err.Error())
return events.APIGatewayProxyResponse{
Body: fmt.Sprintf("Error: %s", err.Error()),
StatusCode: http.StatusOK,
}, nil
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
}, nil
}
// todo: refactor and make nicer
func handleBlockAction(action *slack.BlockAction, user slack.User, channel slack.Channel) {
if strings.HasPrefix(action.ActionID, "/f1") {
command := slackutil.SlashCommand{}
command.Command = "/f1"
command.Text = action.ActionID[4:]
command.ChannelID = channel.ID
command.UserID = user.ID
err := CommandHandler.Handle(command)
if err != nil {
panic(err)
}
}
}
func modalEventHandler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
uri, _ := url.ParseQuery(request.Body)
payload := uri.Get("payload")
var action slack.InteractionCallback
err := json.Unmarshal([]byte(payload), &action)
if err != nil {
log.Printf("Could not parse action response JSON: %v", err)
}
if action.ActionCallback.BlockActions != nil {
for _, blockAction := range action.ActionCallback.BlockActions {
handleBlockAction(blockAction, action.User, action.Channel)
}
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
}, nil
}
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
if request.Path == "/slash" {
return slashCommandHandler(request)
} else if request.Path == "/modal" {
return modalEventHandler(request)
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
}, nil
}
func main() {
lambda.Start(handler)
}