-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
327 lines (275 loc) · 10.2 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
)
func main() {
// here I am Loading the Env variables
godotenv.Load(".env")
token := os.Getenv("SLACK_AUTH_TOKEN")
appToken := os.Getenv("SLACK_APP_TOKEN")
// Creating a new client to slack by giving token
// Setting debug to true while developing
// Also adding a ApplicationToken option to the client
//One that uses the regular API and one for the websocket events
client := slack.New(token, slack.OptionDebug(true), slack.OptionAppLevelToken(appToken))
// Socket Mode, this allows the bot to connect via WebSocket.I need to search an alternative, Maybe a Public URL can help
socketClient := socketmode.New(
client,
socketmode.OptionDebug(true),
// This is the Option to set a custom logger
socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
args := os.Args[1:]
fmt.Println(args)
preText := "*Hello! My Jenkins build is Finished!*"
jenkinsURL := "*Build URL:* " + args[0]
buildResult := "*" + args[1] + "*"
buildNumber := "*" + args[2] + "*"
jobName := "*" + args[3] + "*"
if buildResult == "*SUCCESS*" {
buildResult = buildResult + " Wohoo:tada:"
} else {
buildResult = buildResult + ":x:"
}
//dividerSection1 := slack.NewDividerBlock()
jenkinsBuildDetails := jobName + " #" + buildNumber + " - " + buildResult + "\n" + jenkinsURL
preTextField := slack.NewTextBlockObject("mrkdwn", preText+"\n\n", false, false)
jenkinsBuildDetailsField := slack.NewTextBlockObject("mrkdwn", jenkinsBuildDetails+"\n\n", false, false)
jenkinsBuildDetailsSection := slack.NewSectionBlock(jenkinsBuildDetailsField, nil, nil)
preTextSection := slack.NewSectionBlock(preTextField, nil, nil)
msg := slack.MsgOptionBlocks(
preTextSection,
jenkinsBuildDetailsSection,
)
_, _, _, err := client.SendMessage(
os.Getenv("SLACK_CHANNEL_ID"),
msg,
)
if err != nil {
fmt.Print(err)
return
}
channelID, timestamp, err := client.PostMessage(
os.Getenv("SLACK_CHANNEL_ID"), slack.MsgOptionText("Hello World!", false),
)
if err != nil {
fmt.Print(err)
return
}
fmt.Printf("Message sent successfully to channel %s at %s", channelID, timestamp)
// Creating a context that can be used to cancel goroutine
ctx, cancel := context.WithCancel(context.Background())
// If we fail to cancel the context, the goroutine that WithCancel or WithTimeout created will be retained in memory indefinitely (until the program shuts down), causing a memory leak. If you do this a lot, your memory will balloon significantly. It's best practice to use a defer cancel() immediately after calling WithCancel() or WithTimeout()
defer cancel()
go func(ctx context.Context, client *slack.Client, socketClient *socketmode.Client) {
// Creating a for loop that selects either the context cancellation or the events incomming
for {
select {
// incase context cancel is called we exit the goroutine
case <-ctx.Done():
log.Println("🚸Shutting down socketmode listener")
return
case event := <-socketClient.Events:
// We have a new Events, let's type switch the event
switch event.Type {
// handle EventAPI events
case socketmode.EventTypeEventsAPI:
// The Event sent on the channel is not the same as the EventAPI events so we need to type cast it
eventsAPIEvent, ok := event.Data.(slackevents.EventsAPIEvent)
if !ok {
log.Printf("❎Could not type cast the event to the EventsAPIEvent: %v\n", event)
continue
}
// We need to send an Acknowledge to the slack server
socketClient.Ack(*event.Request)
// Now we have an Events API event, but this event type can in turn be many types, so we actually need another type switch
err := handleEventMessage(eventsAPIEvent, client)
if err != nil {
// Replacing with actual err handeling
log.Fatal(err)
}
// Handle Slash Commands
case socketmode.EventTypeSlashCommand:
command, ok := event.Data.(slack.SlashCommand)
if !ok {
log.Printf("Could not type cast the message to a SlashCommand: %v\n", command)
continue
}
// handleSlashCommand will take care of the command
payload, err := handleSlashCommand(command, client)
if err != nil {
log.Fatal(err)
}
// The payload is the response
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 a Interaction callback: %v\n", interaction)
continue
}
err := handleInteractionEvent(interaction, client)
if err != nil {
log.Fatal(err)
}
socketClient.Ack(*event.Request)
//end of switch
}
}
}
}(ctx, client, socketClient)
socketClient.Run()
}
// handleEventMessage will take an event and handle it properly based on the type of event
func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client) error {
switch event.Type {
// First we check if this is an CallbackEvent
case slackevents.CallbackEvent:
innerEvent := event.InnerEvent
// Yet Another Type switch on the actual Data to see if its an AppMentionEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
// The application has been mentioned since this Event is a Mention event
err := handleAppMentionEvent(ev, client)
if err != nil {
return err
}
}
default:
return errors.New("❌unsupported event type")
}
return nil
}
// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned
func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error {
// Grabing the user name based on the ID of the one who mentioned the bot
user, err := client.GetUserInfo(event.User)
if err != nil {
return err
}
// Checking if the user said Hello to the bot
text := strings.ToLower(event.Text)
// Creating the attachment and assigned based on the message
attachment := slack.Attachment{}
// Adding Some default context like user who mentioned the bot
attachment.Fields = []slack.AttachmentField{
{
Title: "Date⌛",
Value: time.Now().String(),
}, {
Title: "Caller📞",
Value: user.Name,
},
}
if strings.Contains(text, "hello") {
// Greet the user
attachment.Text = fmt.Sprintf("Hello %s", user.Name)
attachment.Pretext = "Greetings💎"
attachment.Color = "#4af030"
} else {
// Send a message to the user
attachment.Text = fmt.Sprintf("How can I help you %s?", user.Name)
attachment.Pretext = "Wohoo🎉What is my work today"
attachment.Color = "#3d3d3d"
}
// Sending the message to the channel
// The Channel value is in the event message
_, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment))
if err != nil {
return fmt.Errorf("📍failed to post message: %w", err)
}
return nil
}
// handleSlashCommand will take a slash command and route to the appropriate function
func handleSlashCommand(command slack.SlashCommand, client *slack.Client) (interface{}, error) {
// We need to switch depending on the command
switch command.Command {
case "/namaste":
// This was a hello command, so pass it along to the proper function
return nil, handleHelloCommand(command, client)
case "/red-pill-blue-pill":
return handleChoice(command, client)
}
return nil, nil
}
// handleHelloCommand will take care of /namaste submissions
func handleHelloCommand(command slack.SlashCommand, client *slack.Client) error {
// The Input is found in the text field so
// Creating the attachment and assigned based on the message
attachment := slack.Attachment{}
// Adding Some default context like user who mentioned the bot
attachment.Fields = []slack.AttachmentField{
{
Title: "Date⌛",
Value: time.Now().String(),
}, {
Title: "Caller📞",
Value: command.UserName,
},
}
// Greeting the user
attachment.Text = fmt.Sprintf("Bonjour😏 %s", command.Text)
attachment.Color = "#4af030"
// Sending the message to the channel
// The Channel is available in the command.ChannelID
_, _, err := client.PostMessage(command.ChannelID, slack.MsgOptionAttachments(attachment))
if err != nil {
return fmt.Errorf("📍failed to post message: %w", err)
}
return nil
}
// handleChoice will trigger a Yes or No question to the initializer
func handleChoice(command slack.SlashCommand, client *slack.Client) (interface{}, error) {
// Creating the attachment and assigned based on the message
attachment := slack.Attachment{}
// Creating the checkbox element
checkbox := slack.NewCheckboxGroupsBlockElement("answer",
slack.NewOptionBlockObject("yes", &slack.TextBlockObject{Text: "Yes", Type: slack.MarkdownType}, &slack.TextBlockObject{Text: "🎡Did you Enjoy it?", Type: slack.MarkdownType}),
slack.NewOptionBlockObject("no", &slack.TextBlockObject{Text: "No", Type: slack.MarkdownType}, &slack.TextBlockObject{Text: "🎭Did you Dislike it?", Type: slack.MarkdownType}),
)
// Creating the Accessory that will be included in the Block and add the checkbox to it
accessory := slack.NewAccessory(checkbox)
// Adding Blocks to the attachment
attachment.Blocks = slack.Blocks{
BlockSet: []slack.Block{
// Creating a new section block element and add some text and the accessory to it
slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: "Did you think I was Helpful🤖?",
},
nil,
accessory,
),
},
}
attachment.Text = "Rate the experience😊"
attachment.Color = "#4af030"
return attachment, nil
}
func handleInteractionEvent(interaction slack.InteractionCallback, client *slack.Client) error {
// This is where we would handle the interaction
// Switch depending on the Type
log.Printf("The action called is: %s\n", interaction.ActionID)
log.Printf("The response was of type: %s\n", interaction.Type)
switch interaction.Type {
case slack.InteractionTypeBlockActions:
// This is a block action, so we need to handle it
for _, action := range interaction.ActionCallback.BlockActions {
log.Printf("%v", action)
log.Println("Selected option: ", action.SelectedOptions)
}
default:
}
return nil
}