1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-12-16 10:29:42 +02:00
notify/service/slack/slack.go
KrishanBhalla f25c2a062f comments
2021-01-26 22:50:42 +00:00

53 lines
1.4 KiB
Go

package slack
import (
"github.com/pkg/errors"
"github.com/slack-go/slack"
)
// Slack struct holds necessary data to communicate with the Slack API.
type Slack struct {
client *slack.Client
channelIDs []string
}
// New returns a new instance of a Slack notification service.
// For more information about slack api token:
// -> https://pkg.go.dev/github.com/slack-go/slack#New
func New(apiToken string) (*Slack, error) {
client := slack.New(apiToken)
s := &Slack{
client: client,
channelIDs: []string{},
}
return s, nil
}
// AddReceivers takes Slack channel IDs and adds them to the internal channel ID list. The Send method will send
// a given message to all those channels.
func (s *Slack) AddReceivers(channelIDs ...string) {
s.channelIDs = append(s.channelIDs, channelIDs...)
}
// Send takes a message subject and a message body and sends them to all previously set channels.
// you will need a slack app with the chat:write.public and chat:write permissions.
// see https://api.slack.com/
func (s Slack) Send(subject, message string) error {
fullMessage := subject + "\n" + message // Treating subject as message title
for _, channelID := range s.channelIDs {
id, timestamp, err := s.client.PostMessage(
channelID,
slack.MsgOptionText(fullMessage, false),
)
if err != nil {
return errors.Wrapf(err, "failed to send message to Slack channel '%d' at time '%s'", id, timestamp)
}
}
return nil
}