This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslack.go
More file actions
157 lines (116 loc) · 3.48 KB
/
slack.go
File metadata and controls
157 lines (116 loc) · 3.48 KB
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
package slack
import (
"errors"
"fmt"
"github.com/hatchet-dev/hatchet-workflows/pkg/workflows/types"
"github.com/slack-go/slack"
)
type SlackIntegration struct {
api *slack.Client
teamId string
}
func NewSlackIntegration(authToken string, teamId string, debug bool) *SlackIntegration {
api := slack.New(authToken, slack.OptionDebug(debug))
return &SlackIntegration{
api: api,
teamId: teamId,
}
}
func (s *SlackIntegration) GetId() string {
return "slack"
}
func (s *SlackIntegration) Actions() []string {
return []string{
"create-channel",
"send-message",
"add-users-to-channel",
}
}
func (s *SlackIntegration) PerformAction(action types.Action, data map[string]interface{}) (map[string]interface{}, error) {
fmt.Println("GOT SLACK", action.String())
switch action.Verb {
case "create-channel":
return s.createChannel(data)
case "add-users-to-channel":
return s.addUsersToChannel(data)
case "send-message":
return s.sendMessageToChannel(data)
default:
return nil, fmt.Errorf("unsupported action: %s", action)
}
}
func (s *SlackIntegration) createChannel(data map[string]interface{}) (map[string]interface{}, error) {
dataName, ok := data["channelName"]
if !ok || dataName == nil {
return nil, errors.New("missing required field: name")
}
name, ok := dataName.(string)
if !ok {
return nil, errors.New("invalid type for field: name")
}
channel, err := s.api.CreateConversation(slack.CreateConversationParams{
IsPrivate: true,
ChannelName: name,
TeamID: s.teamId,
})
if err != nil {
return nil, fmt.Errorf("error creating slack channel: %w", err)
}
return map[string]interface{}{
"channelId": channel.ID,
}, nil
}
func (s *SlackIntegration) addUsersToChannel(data map[string]interface{}) (map[string]interface{}, error) {
channelId, ok := data["channelId"]
if !ok || channelId == nil {
return nil, errors.New("missing required field: channelId")
}
channelIdStr, ok := channelId.(string)
if !ok {
return nil, errors.New("invalid type for field: channelId")
}
userIds, ok := data["userIds"]
if !ok || userIds == nil {
return nil, errors.New("missing required field: userIds")
}
userIdsArr, ok := userIds.([]any)
if !ok {
return nil, errors.New("invalid type for field: userIds")
}
userIdsStrArr := make([]string, len(userIdsArr))
for i, userId := range userIdsArr {
userIdStr, ok := userId.(string)
if !ok {
return nil, errors.New("invalid type for field: userIds")
}
userIdsStrArr[i] = userIdStr
}
_, err := s.api.InviteUsersToConversation(channelIdStr, userIdsStrArr...)
if err != nil {
return nil, fmt.Errorf("error adding users to slack channel: %w", err)
}
return map[string]interface{}{}, nil
}
func (s *SlackIntegration) sendMessageToChannel(data map[string]interface{}) (map[string]interface{}, error) {
channelId, ok := data["channelId"]
if !ok || channelId == nil {
return nil, errors.New("missing required field: channelId")
}
channelIdStr, ok := channelId.(string)
if !ok {
return nil, errors.New("invalid type for field: channelId")
}
message, ok := data["message"]
if !ok || message == nil {
return nil, errors.New("missing required field: message")
}
messageStr, ok := message.(string)
if !ok {
return nil, errors.New("invalid type for field: message")
}
_, _, err := s.api.PostMessage(channelIdStr, slack.MsgOptionText(messageStr, false))
if err != nil {
return nil, fmt.Errorf("error sending message to slack channel: %w", err)
}
return map[string]interface{}{}, nil
}