-
Notifications
You must be signed in to change notification settings - Fork 126
/
slack.go
109 lines (85 loc) · 2.38 KB
/
slack.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
package slack
import (
"context"
"fmt"
"github.com/slack-go/slack"
"github.com/hatchet-dev/hatchet/pkg/integrations"
)
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) ActionHandler(action string) any {
switch action {
case "create-channel":
return s.createChannel
case "add-users-to-channel":
return s.addUsersToChannel
case "send-message":
return s.sendMessageToChannel
default:
return nil
}
}
func (s *SlackIntegration) GetWebhooks() []integrations.IntegrationWebhook {
return []integrations.IntegrationWebhook{}
}
type CreateChannelData struct {
ChannelName string `json:"channelName"`
}
type CreateChannelOutput struct {
ChannelId string `json:"channelId"`
}
func (s *SlackIntegration) createChannel(ctx context.Context, data *CreateChannelData) (*CreateChannelOutput, error) {
channel, err := s.api.CreateConversation(slack.CreateConversationParams{
IsPrivate: true,
ChannelName: data.ChannelName,
TeamID: s.teamId,
})
if err != nil {
return nil, fmt.Errorf("error creating slack channel: %w", err)
}
return &CreateChannelOutput{
ChannelId: channel.ID,
}, nil
}
type AddUsersToChannelData struct {
ChannelID string `json:"channelId"`
UserIDs []string `json:"userIds"`
}
type AddUsersToChannelOutput struct{}
func (s *SlackIntegration) addUsersToChannel(ctx context.Context, data *AddUsersToChannelData) error {
_, err := s.api.InviteUsersToConversation(data.ChannelID, data.UserIDs...)
if err != nil {
return fmt.Errorf("error adding users to slack channel: %w", err)
}
return nil
}
type SendMessageToChannelData struct {
ChannelID string `json:"channelId"`
Message string `json:"message"`
}
func (s *SlackIntegration) sendMessageToChannel(ctx context.Context, data *SendMessageToChannelData) error {
_, _, err := s.api.PostMessage(data.ChannelID, slack.MsgOptionText(data.Message, false))
if err != nil {
return fmt.Errorf("error sending message to slack channel: %w", err)
}
return nil
}