-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (136 loc) · 4.74 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
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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/slack-go/slack/socketmode"
"github.com/slack-go/slack"
)
var HashToChannelMap = make(map[string]string)
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.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
slack.OptionAppLevelToken(appToken),
)
client := socketmode.New(
api,
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)
socketmodeHandler.Handle(socketmode.EventTypeHello, middlewareHello)
// Handle slashcommand
socketmodeHandler.Handle(socketmode.EventTypeSlashCommand, middlewareSlashCommand)
socketmodeHandler.HandleSlashCommand("/askanon", middlewareSlashCommand)
// Handle interactive elements
socketmodeHandler.Handle(socketmode.EventTypeInteractive, middlewareInteractive)
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 middlewareHello(evt *socketmode.Event, client *socketmode.Client) {
fmt.Println("Slack says Hello!")
}
func middlewareSlashCommand(evt *socketmode.Event, client *socketmode.Client) {
cmd, ok := evt.Data.(slack.SlashCommand)
if !ok {
return
}
switch cmd.Command {
case "/askanon":
modalRequest := generateModalRequest()
response, _ := client.Client.OpenView(cmd.TriggerID, modalRequest)
HashToChannelMap[response.Hash] = cmd.ChannelID
}
client.Ack(*evt.Request)
}
func middlewareInteractive(evt *socketmode.Event, client *socketmode.Client) {
callback, ok := evt.Data.(slack.InteractionCallback)
if !ok {
return
}
var payload interface{}
switch callback.Type {
case slack.InteractionTypeViewSubmission:
question := callback.View.State.Values["Question"]["question"].Value
questionBlock := generateQuestionBlocks(question)
channelId := HashToChannelMap[callback.View.Hash]
delete(HashToChannelMap, callback.View.Hash)
_, _, err := client.Client.PostMessage(channelId, slack.MsgOptionBlocks(questionBlock...))
if err != nil {
fmt.Print("could not post question")
}
}
client.Ack(*evt.Request, payload)
}
func generateQuestionBlocks(question string) []slack.Block {
blocks := []slack.Block{
slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: "*New Question Asked*",
},
nil,
nil,
),
slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: question,
},
nil,
nil),
}
return blocks
}
func generateModalRequest() slack.ModalViewRequest {
titleText := slack.NewTextBlockObject("plain_text", "Ask a question", false, false)
closeText := slack.NewTextBlockObject("plain_text", "Close", false, false)
submitText := slack.NewTextBlockObject("plain_text", "Submit", false, false)
headerText := slack.NewTextBlockObject("mrkdwn", "Please enter your question", false, false)
headerSection := slack.NewSectionBlock(headerText, nil, nil)
questionText := slack.NewTextBlockObject("plain_text", "Question", false, false)
questionHint := slack.NewTextBlockObject("plain_text", "Enter your question here", false, false)
questionPlaceholder := slack.NewTextBlockObject("plain_text", "Enter your question", false, false)
questionElement := slack.NewPlainTextInputBlockElement(questionPlaceholder, "question")
// Notice that blockID is a unique identifier for a block
question := slack.NewInputBlock("Question", questionText, questionHint, questionElement)
blocks := slack.Blocks{
BlockSet: []slack.Block{
headerSection,
question,
},
}
var modalRequest slack.ModalViewRequest
modalRequest.Type = slack.ViewType("modal")
modalRequest.Title = titleText
modalRequest.Close = closeText
modalRequest.Submit = submitText
modalRequest.Blocks = blocks
return modalRequest
}