forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketmode_handler.go
205 lines (164 loc) · 5.51 KB
/
socketmode_handler.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
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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"github.com/slack-go/slack"
)
func main() {
appToken := os.Getenv("SLACK_APP_TOKEN")
if appToken == "" {
panic("SLACK_APP_TOKEN must be set.\n")
}
if !strings.HasPrefix(appToken, "xapp-") {
panic("SLACK_APP_TOKEN must have the prefix \"xapp-\".")
}
botToken := os.Getenv("SLACK_BOT_TOKEN")
if botToken == "" {
panic("SLACK_BOT_TOKEN must be set.\n")
}
if !strings.HasPrefix(botToken, "xoxb-") {
panic("SLACK_BOT_TOKEN must have the prefix \"xoxb-\".")
}
api := slack.New(
botToken,
slack.OptionDebug(true),
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
slack.OptionAppLevelToken(appToken),
)
client := socketmode.New(
api,
socketmode.OptionDebug(true),
socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
socketmodeHandler := socketmode.NewSocketmodeHandler(client)
socketmodeHandler.Handle(socketmode.EventTypeConnecting, middlewareConnecting)
socketmodeHandler.Handle(socketmode.EventTypeConnectionError, middlewareConnectionError)
socketmodeHandler.Handle(socketmode.EventTypeConnected, middlewareConnected)
//\\ EventTypeEventsAPI //\\
// Handle all EventsAPI
socketmodeHandler.Handle(socketmode.EventTypeEventsAPI, middlewareEventsAPI)
// Handle a specific event from EventsAPI
socketmodeHandler.HandleEvents(slackevents.AppMention, middlewareAppMentionEvent)
//\\ EventTypeInteractive //\\
// Handle all Interactive Events
socketmodeHandler.Handle(socketmode.EventTypeInteractive, middlewareInteractive)
// Handle a specific Interaction
socketmodeHandler.HandleInteraction(slack.InteractionTypeBlockActions, middlewareInteractionTypeBlockActions)
// Handle all SlashCommand
socketmodeHandler.Handle(socketmode.EventTypeSlashCommand, middlewareSlashCommand)
socketmodeHandler.HandleSlashCommand("/rocket", middlewareSlashCommand)
// socketmodeHandler.HandleDefault(middlewareDefault)
socketmodeHandler.RunEventLoop()
}
func middlewareConnecting(evt *socketmode.Event, client *socketmode.Client) {
fmt.Println("Connecting to Slack with Socket Mode...")
}
func middlewareConnectionError(evt *socketmode.Event, client *socketmode.Client) {
fmt.Println("Connection failed. Retrying later...")
}
func middlewareConnected(evt *socketmode.Event, client *socketmode.Client) {
fmt.Println("Connected to Slack with Socket Mode.")
}
func middlewareEventsAPI(evt *socketmode.Event, client *socketmode.Client) {
fmt.Println("middlewareEventsAPI")
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
fmt.Printf("Ignored %+v\n", evt)
return
}
fmt.Printf("Event received: %+v\n", eventsAPIEvent)
client.Ack(*evt.Request)
switch eventsAPIEvent.Type {
case slackevents.CallbackEvent:
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
fmt.Printf("We have been mentionned in %v", ev.Channel)
_, _, err := client.Client.PostMessage(ev.Channel, slack.MsgOptionText("Yes, hello.", false))
if err != nil {
fmt.Printf("failed posting message: %v", err)
}
case *slackevents.MemberJoinedChannelEvent:
fmt.Printf("user %q joined to channel %q", ev.User, ev.Channel)
}
default:
client.Debugf("unsupported Events API event received")
}
}
func middlewareAppMentionEvent(evt *socketmode.Event, client *socketmode.Client) {
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
fmt.Printf("Ignored %+v\n", evt)
return
}
client.Ack(*evt.Request)
ev, ok := eventsAPIEvent.InnerEvent.Data.(*slackevents.AppMentionEvent)
if !ok {
fmt.Printf("Ignored %+v\n", ev)
return
}
fmt.Printf("We have been mentionned in %v\n", ev.Channel)
_, _, err := client.Client.PostMessage(ev.Channel, slack.MsgOptionText("Yes, hello.", false))
if err != nil {
fmt.Printf("failed posting message: %v", err)
}
}
func middlewareInteractive(evt *socketmode.Event, client *socketmode.Client) {
callback, ok := evt.Data.(slack.InteractionCallback)
if !ok {
fmt.Printf("Ignored %+v\n", evt)
return
}
fmt.Printf("Interaction received: %+v\n", callback)
var payload interface{}
switch callback.Type {
case slack.InteractionTypeBlockActions:
// See https://api.slack.com/apis/connections/socket-implement#button
client.Debugf("button clicked!")
case slack.InteractionTypeShortcut:
case slack.InteractionTypeViewSubmission:
// See https://api.slack.com/apis/connections/socket-implement#modal
case slack.InteractionTypeDialogSubmission:
default:
}
client.Ack(*evt.Request, payload)
}
func middlewareInteractionTypeBlockActions(evt *socketmode.Event, client *socketmode.Client) {
client.Debugf("button clicked!")
}
func middlewareSlashCommand(evt *socketmode.Event, client *socketmode.Client) {
cmd, ok := evt.Data.(slack.SlashCommand)
if !ok {
fmt.Printf("Ignored %+v\n", evt)
return
}
client.Debugf("Slash command received: %+v", cmd)
payload := map[string]interface{}{
"blocks": []slack.Block{
slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: "foo",
},
nil,
slack.NewAccessory(
slack.NewButtonBlockElement(
"",
"somevalue",
&slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "bar",
},
),
),
),
}}
client.Ack(*evt.Request, payload)
}
func middlewareDefault(evt *socketmode.Event, client *socketmode.Client) {
// fmt.Fprintf(os.Stderr, "Unexpected event type received: %s\n", evt.Type)
}