mirror of
https://github.com/nikoksr/notify.git
synced 2025-01-20 02:59:56 +02:00
Merge branch 'main' into Remove_Pseudo
This commit is contained in:
commit
742d127319
3
.codacy.yml
Normal file
3
.codacy.yml
Normal file
@ -0,0 +1,3 @@
|
||||
---
|
||||
exclude_paths:
|
||||
- '**.md'
|
@ -62,8 +62,9 @@ _ = notifier.Send(
|
||||
|
||||
- *Discord*
|
||||
- *Email*
|
||||
- *Telegram*
|
||||
- *Microsoft Teams*
|
||||
- *Slack*
|
||||
- *Telegram*
|
||||
|
||||
## Roadmap <a id="roadmap"></a>
|
||||
|
||||
@ -74,8 +75,9 @@ _ = notifier.Send(
|
||||
|
||||
- Discord support: [bwmarrin/discordgo](https://github.com/bwmarrin/discordgo)
|
||||
- Email support: [jordan-wright/email](https://github.com/jordan-wright/email)
|
||||
- Telegram support: [go-telegram-bot-api/telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api)
|
||||
- Microsoft Teams support: [atc0005/go-teams-notify](https://github.com/atc0005/go-teams-notify)
|
||||
- Slack support: [slack-go/slack](https://github.com/slack-go/slack)
|
||||
- Telegram support: [go-telegram-bot-api/telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api)
|
||||
- Logo: [MariaLetta/free-gophers-pack](https://github.com/MariaLetta/free-gophers-pack)
|
||||
|
||||
## Author <a id="author"></a>
|
||||
|
7
go.mod
7
go.mod
@ -3,13 +3,14 @@ module github.com/nikoksr/notify
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.23.0
|
||||
github.com/atc0005/go-teams-notify/v2 v2.4.2
|
||||
github.com/bwmarrin/discordgo v0.23.1
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
|
||||
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/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // indirect
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
|
||||
)
|
||||
|
25
go.sum
25
go.sum
@ -1,5 +1,8 @@
|
||||
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/atc0005/go-teams-notify/v2 v2.4.2 h1:3KQ8e8LN4kwuWWHpnCNTXA15JdLRaNWcCS1VS0a4SO0=
|
||||
github.com/atc0005/go-teams-notify/v2 v2.4.2/go.mod h1:BSlh1HBcgWcGoNM3Abm36WMPcj+k8Wf0ZLZx6lBx2qk=
|
||||
github.com/bwmarrin/discordgo v0.23.1 h1:xlK4/69bpl/VSoCYaKe3BOc9j1HkNopoRdCppRYu8dk=
|
||||
github.com/bwmarrin/discordgo v0.23.1/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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=
|
||||
@ -18,19 +21,27 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
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=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
|
||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -13,7 +13,7 @@ type Notify struct {
|
||||
}
|
||||
|
||||
// ErrSendNotification signals that the notifier failed to send a notification.
|
||||
var ErrSendNotification = errors.New("Send notification")
|
||||
var ErrSendNotification = errors.New("send notification")
|
||||
|
||||
// New returns a new instance of Notify. Defaulting to being not disabled and using the pseudo notification
|
||||
// service under the hood.
|
||||
|
59
service/msteams/ms_teams.go
Normal file
59
service/msteams/ms_teams.go
Normal file
@ -0,0 +1,59 @@
|
||||
package msteams
|
||||
|
||||
import (
|
||||
goteamsnotify "github.com/atc0005/go-teams-notify/v2"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// MSTeams struct holds necessary data to communicate with the MSTeams API.
|
||||
type MSTeams struct {
|
||||
client goteamsnotify.API
|
||||
webHooks []string
|
||||
}
|
||||
|
||||
// New returns a new instance of a MSTeams notification service.
|
||||
// For more information about telegram api token:
|
||||
// -> https://github.com/atc0005/go-teams-notify#example-basic
|
||||
func New() *MSTeams {
|
||||
client := goteamsnotify.NewClient()
|
||||
|
||||
m := &MSTeams{
|
||||
client: client,
|
||||
webHooks: []string{},
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// DisableWebhookValidation disables the validation of webhook URLs, including the validation of known prefixes so that
|
||||
// custom/private webhook URL endpoints can be used (e.g., testing purposes).
|
||||
// For more information about telegram api token:
|
||||
// -> https://github.com/atc0005/go-teams-notify#example-disable-webhook-url-prefix-validation
|
||||
func (m *MSTeams) DisableWebhookValidation() {
|
||||
m.client.SkipWebhookURLValidationOnSend(true)
|
||||
}
|
||||
|
||||
// AddReceivers takes MSTeams channel web-hooks and adds them to the internal web-hook list. The Send method will send
|
||||
// a given message to all those chats.
|
||||
func (m *MSTeams) AddReceivers(webHooks ...string) {
|
||||
m.webHooks = append(m.webHooks, webHooks...)
|
||||
}
|
||||
|
||||
// Send accepts a subject and a message body and sends them to all previously specified channels. Message body supports
|
||||
// html as markup language.
|
||||
// For more information about telegram api token:
|
||||
// -> https://github.com/atc0005/go-teams-notify#example-basic
|
||||
func (m MSTeams) Send(subject, message string) error {
|
||||
msgCard := goteamsnotify.NewMessageCard()
|
||||
msgCard.Title = subject
|
||||
msgCard.Text = message
|
||||
|
||||
for _, webHook := range m.webHooks {
|
||||
err := m.client.Send(webHook, msgCard)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to send message to Microsoft Teams via webhook '%s'", webHook)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -14,7 +14,7 @@ type Slack struct {
|
||||
// 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) {
|
||||
func New(apiToken string) *Slack {
|
||||
client := slack.New(apiToken)
|
||||
|
||||
s := &Slack{
|
||||
@ -22,7 +22,7 @@ func New(apiToken string) (*Slack, error) {
|
||||
channelIDs: []string{},
|
||||
}
|
||||
|
||||
return s, nil
|
||||
return s
|
||||
}
|
||||
|
||||
// AddReceivers takes Slack channel IDs and adds them to the internal channel ID list. The Send method will send
|
||||
@ -38,13 +38,12 @@ 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 errors.Wrapf(err, "failed to send message to Slack channel '%s' at time '%s'", id, timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
|
60
service/slack/usage.md
Normal file
60
service/slack/usage.md
Normal file
@ -0,0 +1,60 @@
|
||||
# Slack Usage
|
||||
|
||||
Ensure that you have already navigated to your GOPATH and installed the following packages:
|
||||
|
||||
* `go get -u github.com/nikoksr/notify`
|
||||
* `go get github.com/slack-go/slack` - You might need this one too
|
||||
|
||||
## Steps for Slack App
|
||||
|
||||
These are general and very high level instructions
|
||||
|
||||
1. Create a new Slack App
|
||||
2. Give your bot/app the following OAuth permission scopes: `chat:write`, `chat:write.public`
|
||||
3. Copy your *Bot User OAuth Access Token* for usage below
|
||||
4. Copy the *Channel ID* of the channel you want to post a message to. You can grab the *Channel ID* by right clicking a channel and selecting `copy link`. Your *Channel ID* will be in that link.
|
||||
5. Now you should be good to use the code below
|
||||
|
||||
## Sample Code
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/nikoksr/notify"
|
||||
"github.com/nikoksr/notify/service/slack"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
notifier := notify.New()
|
||||
|
||||
// Provide your Slack OAuth Access Token
|
||||
slackService := slack.New("OAUTH_TOKEN")
|
||||
|
||||
// Passing a Slack channel id as receiver for our messages.
|
||||
// Where to send our messages.
|
||||
slackService.AddReceivers("CHANNEL_ID")
|
||||
|
||||
// Tell our notifier to use the Slack service. You can repeat the above process
|
||||
// for as many services as you like and just tell the notifier to use them.
|
||||
notifier.UseService(slackService)
|
||||
|
||||
// Send a message
|
||||
_ = notifier.Send(
|
||||
"Hello :wave:\n",
|
||||
"I am a bot written in Go!",
|
||||
)
|
||||
|
||||
// Code isn't working and need to debug? Use this code below:
|
||||
// x := notifier.Send(
|
||||
// "Hello :wave:\n",
|
||||
// "I am a bot written in Go!",
|
||||
// )
|
||||
|
||||
// if x != nil {
|
||||
// fmt.Println(x)
|
||||
// }
|
||||
|
||||
}
|
||||
```
|
@ -11,9 +11,8 @@ const defaultParseMode = tgbotapi.ModeHTML
|
||||
|
||||
// Telegram struct holds necessary data to communicate with the Telegram API.
|
||||
type Telegram struct {
|
||||
client *tgbotapi.BotAPI
|
||||
listener *tgbotapi.BotAPI
|
||||
chatIDs []int64
|
||||
client *tgbotapi.BotAPI
|
||||
chatIDs []int64
|
||||
}
|
||||
|
||||
// New returns a new instance of a Telegram notification service.
|
||||
|
Loading…
x
Reference in New Issue
Block a user