-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
135 lines (112 loc) · 4.86 KB
/
handlers.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
package standup
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/justmiles/standup-bot/lib/types"
"github.com/justmiles/standup-bot/lib/views"
logger "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
func handleSlashCommand(cmd slack.SlashCommand) {
switch cmd.Text {
case "":
openSettingsModal(cmd)
case "solicit":
// mock standup settings if none exist with user issuing the command else use the channels settings
settings := getStandupSettings(cmd.ChannelID, cmd.ChannelName)
if len(settings.Participants) == 0 {
settings.Participants = []string{cmd.UserID}
settings.Shame = true
}
getSolicitStandupFunc(settings)()
case "share":
// mock standup settings if none exist with user issuing the command else use the channels settings
settings := getStandupSettings(cmd.ChannelID, cmd.ChannelName)
if len(settings.Participants) == 0 {
settings.Participants = []string{cmd.UserID}
settings.Shame = true
}
getShareStandupFunc(settings)()
default:
socketMode.Debugf("Ignored: %v", cmd)
}
}
func handleSettingsModalActions(payload slack.InteractionCallback) {
for _, action := range payload.ActionCallback.BlockActions {
switch action.ActionID {
case views.ModalStandupChannelActionId:
handleSettingsModalSelectChannelAction(payload)
default:
socketMode.Debugf("Ignored: settings modal action with ActionID: %v", payload)
}
}
}
func openSettingsModal(cmd slack.SlashCommand) {
settings := getStandupSettings(cmd.ChannelID, cmd.ChannelName)
resp, err := webApi.OpenView(cmd.TriggerID, views.GetSettingsModal(settings))
if err != nil {
log.Printf("Failed to opemn a modal: %v", err)
}
socketMode.Debugf("views.open response: %v", resp)
}
func submitSettingsModal(payload slack.InteractionCallback) {
socketMode.Debugf("submit settings modal payload.View.State.Values: %v", payload.View.State.Values)
channelID := payload.View.State.Values[views.ModalStandupChannelBlockId][views.ModalStandupChannelActionId].SelectedChannel
participants := payload.View.State.Values[views.ModalParticipantsBlockId][views.ModalParticipantsActionId].SelectedUsers
solicitTime := strings.Split(payload.View.State.Values[views.ModalSolicitTimeBlockId][views.ModalSolicitTimeActionId].SelectedTime, ":")
shareTime := strings.Split(payload.View.State.Values[views.ModalShareTimeBlockId][views.ModalShareTimeActionId].SelectedTime, ":")
meetingDays := payload.View.State.Values[views.ModalMeetingDaysBlockId][views.ModalMeetingDaysActionId].SelectedOptions
shame, _ := strconv.ParseBool(payload.View.State.Values[views.ModalShameBlockId][views.ModalShameActionId].SelectedOption.Value)
solicitMsg := payload.View.State.Values[views.ModalSolicitMessgaeBlockId][views.ModalSolicitMessageActionId].Value
settings := types.StandupSettings{
ChannelID: channelID,
SolicitCronSpec: fmt.Sprintf("%s %s * * %s", solicitTime[1], solicitTime[0], meetingDaysToCron(meetingDays)),
ShareCronSpec: fmt.Sprintf("%s %s * * %s", shareTime[1], shareTime[0], meetingDaysToCron(meetingDays)),
SolicitMsg: solicitMsg,
Shame: shame,
Participants: participants,
}
socketMode.Debugf("standup submission settings: %v", settings)
err := RegisterStandup(settings)
if err != nil {
logger.Errorf("Error Registering Standup: %v", err)
return
}
// Save brain after successful submission
err = brain.writeToS3()
if err != nil {
logger.Errorf("Error to writing to s3: %v", err)
}
}
func handleSettingsModalSelectChannelAction(payload slack.InteractionCallback) {
socketMode.Debugf("select channel: %v", payload)
channelID := payload.View.State.Values[views.ModalStandupChannelBlockId][views.ModalStandupChannelActionId].SelectedChannel
channelInfo, _ := webApi.GetConversationInfo(channelID, false)
socketMode.Debugf("select channel action ID: %v", channelID)
socketMode.Debugf("selected name: %v", channelInfo.Name)
settings := getStandupSettings(channelID, channelInfo.Name)
// Only way to dynamically update view form values
// 1. Update the view without no blocks
// 2. Update the view with new blocks using the 'initial_value' config
// Update view by removing blocks - no other way to dynamically change view values.
resp, err := webApi.UpdateView(views.GetSettingsModal(types.StandupSettings{}), payload.View.ExternalID, payload.View.Hash, payload.View.ID)
if err != nil {
log.Printf("Failed to update a modal: %v", err)
}
socketMode.Debugf("views.update response: %v", resp)
// Update view with new initial values
resp, err = webApi.UpdateView(views.GetSettingsModal(settings), resp.ExternalID, resp.Hash, resp.ID)
if err != nil {
log.Printf("Failed to opemn a modal: %v", err)
}
socketMode.Debugf("views.update response: %v", resp)
}
func meetingDaysToCron(meetingDays []slack.OptionBlockObject) string {
values := []string{}
for _, day := range meetingDays {
values = append(values, day.Value)
}
return strings.Join(values, ",")
}