From 884735a62bdffc6dfb3c1354a9adef972532556d Mon Sep 17 00:00:00 2001 From: Amir Emad Mahmoudpour Date: Thu, 22 Dec 2022 23:25:21 +0330 Subject: [PATCH] Added Mattermost as a new provider --- cmd/sachet/config.go | 2 + cmd/sachet/main.go | 3 ++ examples/config.yaml | 9 +++++ provider/mattermost/mattermost.go | 66 +++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 provider/mattermost/mattermost.go diff --git a/cmd/sachet/config.go b/cmd/sachet/config.go index bccb170..27d78d3 100644 --- a/cmd/sachet/config.go +++ b/cmd/sachet/config.go @@ -17,6 +17,7 @@ import ( "github.com/messagebird/sachet/provider/kannel" "github.com/messagebird/sachet/provider/kavenegar" "github.com/messagebird/sachet/provider/mailruim" + "github.com/messagebird/sachet/provider/mattermost" "github.com/messagebird/sachet/provider/mediaburst" "github.com/messagebird/sachet/provider/melipayamak" "github.com/messagebird/sachet/provider/messagebird" @@ -77,6 +78,7 @@ var config struct { Sfr sfr.Config TextMagic textmagic.Config Melipayamak melipayamak.Config + Mattermost mattermost.Config } Receivers []ReceiverConf diff --git a/cmd/sachet/main.go b/cmd/sachet/main.go index 9baaddd..37ba1a8 100644 --- a/cmd/sachet/main.go +++ b/cmd/sachet/main.go @@ -25,6 +25,7 @@ import ( "github.com/messagebird/sachet/provider/kannel" "github.com/messagebird/sachet/provider/kavenegar" "github.com/messagebird/sachet/provider/mailruim" + "github.com/messagebird/sachet/provider/mattermost" "github.com/messagebird/sachet/provider/mediaburst" "github.com/messagebird/sachet/provider/melipayamak" "github.com/messagebird/sachet/provider/messagebird" @@ -150,6 +151,8 @@ func providerByName(name string) (sachet.Provider, error) { return textmagic.NewTextMagic(config.Providers.TextMagic), nil case "melipayamak": return melipayamak.NewMelipayamak(config.Providers.Melipayamak), nil + case "mattermost": + return mattermost.NewMattermost(config.Providers.Mattermost), nil } return nil, fmt.Errorf("%s: Unknown provider", name) diff --git a/examples/config.yaml b/examples/config.yaml index db111ad..b729232 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -108,6 +108,10 @@ providers: username: '###' password: '###' endpoint: 'https://rest.payamak-panel.com/api/SendSMS/SendSMS' + mattermost: # For more information: https://developers.mattermost.com/integrate/webhooks/incoming/ + channel: 'alerts' # Put the name of the channel or if you want to send an alert as a direct message to a specific user you should use '@' like @tom + username: 'DoomBringer' # Defaults to the username set during webhook creation; if no username was set during creation, webhook is used. + icon_url: 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/198/freezing-face_1f976.png' templates: - telegram.tmpl @@ -161,3 +165,8 @@ receivers: to: - '09123456789' from: '50004000000000' + - name: 'mattermost' + provider: 'mattermost' + to: + - 'https://your-mattermost-server.com/hooks/xxx-generatedkey-xxx' # Treat this endpoint as a secret. Anyone who has it will be able to post messages to your Mattermost instance. + diff --git a/provider/mattermost/mattermost.go b/provider/mattermost/mattermost.go new file mode 100644 index 0000000..2e3f4ab --- /dev/null +++ b/provider/mattermost/mattermost.go @@ -0,0 +1,66 @@ +package mattermost + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" + + "github.com/messagebird/sachet" +) + +type Config struct { + Channel string `yaml:"channel"` + Username string `yaml:"username"` + IconURL string `yaml:"icon_url"` +} + +var _ (sachet.Provider) = (*Mattermost)(nil) + +type Mattermost struct { + Config + HTTPClient *http.Client +} + +func NewMattermost(config Config) *Mattermost { + return &Mattermost{ + config, + &http.Client{Timeout: time.Second * 20}, + } +} + +func (mm *Mattermost) Send(message sachet.Message) error { + + Payload := map[string]string{ + "channel": mm.Channel, + "username": mm.Username, + "icon_url": mm.IconURL, + "text": message.Text, + } + data, _ := json.Marshal(Payload) + for _, endpoint := range message.To { + request, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(data)) + if err != nil { + return err + } + request.Header.Set("content-type", "application/json") + request.Header.Set("User-Agent", "Sachet") + response, err := mm.HTTPClient.Do(request) + if err != nil { + return err + } + if response.StatusCode != http.StatusOK { + body, _ := ioutil.ReadAll(response.Body) + return fmt.Errorf( + "SMS sending failed. HTTP status code: %d, Response body: %s", + response.StatusCode, + body, + ) + } + response.Body.Close() + } + + return nil +}