slack-bot/pkg/notify/slack.go

52 lines
1.0 KiB
Go

package notify
import (
"fmt"
"log"
"github.com/slack-go/slack"
)
const slackHookBaseURL = "hooks.slack.com/services"
//Notifier interface represent mean to send notification
type Notifier interface {
Notify(message string)
}
//Message store and represent alerting message
type Message struct {
ShouldNotify bool
Content string
}
type SlackNotifier struct {
BotID string
DestChannelID string
WebhookID string
}
//NewSlackNotifier create slack notifier
func NewSlackNotifier(BotID, DestChannelID, WebhookID string) *SlackNotifier {
return &SlackNotifier{
BotID: BotID,
DestChannelID: DestChannelID,
WebhookID: WebhookID,
}
}
//Notify send notification to channel
func (sn *SlackNotifier) Notify(message Message) {
if message.ShouldNotify {
msg := &slack.WebhookMessage{
Text: message.Content,
}
url := fmt.Sprintf("https://%s/%s/%s/%s", slackHookBaseURL, sn.BotID, sn.DestChannelID, sn.WebhookID)
err := slack.PostWebhook(url, msg)
if err != nil {
log.Printf("Send message error %+v", err)
}
}
}