* INFRA-2829 | Implemented transaction in create incident flow * INFRA-2829 | created util func for rollback * INFRA-2829 | removed redundant cod to create slack channel
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package slackbot
|
|
|
|
import (
|
|
"fmt"
|
|
"houston/logger"
|
|
"strings"
|
|
|
|
"github.com/slack-go/slack"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
NotInChannelError = "not_in_channel"
|
|
)
|
|
|
|
func (c *Client) FindParticipants(channelId string) ([]string, error) {
|
|
request := &slack.GetUsersInConversationParameters{
|
|
ChannelID: channelId,
|
|
Limit: 1000,
|
|
}
|
|
channelInfo, _, err := c.socketModeClient.GetUsersInConversation(request)
|
|
if err != nil {
|
|
logger.Error("find participants failed", zap.String("channel_id", channelId), zap.Error(err))
|
|
return nil, fmt.Errorf("fetch channel conversationInfo failed. err: %v", err)
|
|
}
|
|
|
|
return channelInfo, nil
|
|
}
|
|
|
|
func (c *Client) SetChannelTopic(channelId, topic string) (string, error) {
|
|
channel, err := c.socketModeClient.SetTopicOfConversation(channelId, topic)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), NotInChannelError) {
|
|
logger.Info("bot not in channel, inviting now", zap.String("channel_id", channelId))
|
|
_, _, _, inviteErr := c.JoinConversation(channelId)
|
|
if inviteErr != nil {
|
|
return "", inviteErr
|
|
}
|
|
//retry setting topic
|
|
channel, err = c.socketModeClient.SetTopicOfConversation(channelId, topic)
|
|
if err != nil {
|
|
logger.Error("set topic on slack channel failed", zap.String("channel_id", channelId), zap.Error(err))
|
|
return "", err
|
|
}
|
|
} else {
|
|
logger.Error("set topic on slack channel failed", zap.String("channel_id", channelId), zap.Error(err))
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
logger.Info("set topic on slack channel successful", zap.String("channel_id", channelId), zap.String("channel_id", channel.ID))
|
|
return "", nil
|
|
}
|
|
|
|
func (c *Client) InviteUsersToConversation(channelId string, userId ...string) {
|
|
_, err := c.socketModeClient.InviteUsersToConversation(channelId, userId...)
|
|
if err != nil {
|
|
logger.Error("invite users to conversation failed",
|
|
zap.String("channel_id", channelId), zap.Any("user_ids", userId), zap.Error(err))
|
|
return
|
|
}
|
|
|
|
logger.Info("successfully invite users to conversation", zap.String("channel_id", channelId), zap.Any("user_ids", userId))
|
|
}
|
|
|
|
func (c *Client) GetConversationInfo(channelId string) (*slack.Channel, error) {
|
|
channel, err := c.socketModeClient.GetConversationInfo(&slack.GetConversationInfoInput{
|
|
ChannelID: channelId,
|
|
})
|
|
if err != nil {
|
|
logger.Info("failed while fetching conversation info", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
return channel, nil
|
|
}
|
|
|
|
func (c *Client) JoinConversation(channelId string) (*slack.Channel, string, []string, error) {
|
|
channel, warning, warnings, err := c.socketModeClient.JoinConversation(channelId)
|
|
if err != nil {
|
|
logger.Info("failed while joining conversation", zap.Error(err))
|
|
}
|
|
return channel, warning, warnings, nil
|
|
}
|