-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (77 loc) · 2.36 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
package main
import (
"context"
"log"
"os"
handlers "talaniz/slack-bot/lib"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
)
func main() {
godotenv.Load(".env")
token := os.Getenv("SLACK_BOT_TOKEN")
appToken := os.Getenv("SLACK_APP_TOKEN")
client := slack.New(token, slack.OptionDebug(true), slack.OptionAppLevelToken(appToken))
sockentClient := socketmode.New(
client,
socketmode.OptionDebug(true),
socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
// Implement a graceful shutdown here
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context, client *slack.Client, socketClient *socketmode.Client) {
for {
select {
case <-ctx.Done():
log.Println("Shutting down socketmode listener")
return
case event := <-socketClient.Events:
switch event.Type {
// Slack events
case socketmode.EventTypeEventsAPI:
eventsApiEvent, ok := event.Data.(slackevents.EventsAPIEvent)
if !ok {
log.Printf("Could not type case the event to the EventsAPIEvent: %v\n", event)
continue
}
log.Println("Received API event: ", event.Type)
socketClient.Ack(*event.Request)
err := handlers.HandleEventMessage(eventsApiEvent, client)
if err != nil {
log.Fatal(err)
}
// Slash command
case socketmode.EventTypeSlashCommand:
command, ok := event.Data.(slack.SlashCommand)
if !ok {
log.Printf("Could not type case the message to a SlashCommand: %v\n", command)
continue
}
// consider removing the client parameter as it's unused
payload, err := handlers.HandleSlashCommand(command, client)
if err != nil {
log.Fatal(err)
}
socketClient.Ack(*event.Request, payload)
case socketmode.EventTypeInteractive:
interaction, ok := event.Data.(slack.InteractionCallback)
if !ok {
log.Printf("Could not type cast the message to an Interaction callback: %v\n", interaction)
continue
}
err := handlers.HandleInteractionEvent(interaction, client)
if err != nil {
log.Fatal(err)
}
socketClient.Ack(*event.Request)
default:
log.Println("****** Received Event: ", event)
}
}
}
}(ctx, client, sockentClient)
sockentClient.Run()
}