mirror of
https://github.com/nikoksr/notify.git
synced 2024-11-24 08:22:18 +02:00
Merge pull request #2 from KrishanBhalla/slack
feat(service): Add slack support
This commit is contained in:
commit
86b00ee5de
2
go.mod
2
go.mod
@ -5,9 +5,9 @@ go 1.15
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.23.0
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/slack-go/slack v0.8.0
|
||||
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
|
||||
|
11
go.sum
11
go.sum
@ -1,14 +1,25 @@
|
||||
github.com/bwmarrin/discordgo v0.23.0 h1://ARp8qUrRZvDGMkfAjtcC20WOvsMtTgi+KrdKnl6eY=
|
||||
github.com/bwmarrin/discordgo v0.23.0/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
||||
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/slack-go/slack v0.8.0 h1:ANyLY5KHLV+MxLJDQum2IuHTLwbCbDtaWY405X1EU9U=
|
||||
github.com/slack-go/slack v0.8.0/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
|
||||
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
|
||||
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
|
52
service/slack/slack.go
Normal file
52
service/slack/slack.go
Normal file
@ -0,0 +1,52 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user