Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mattermost as a new provider #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -77,6 +78,7 @@ var config struct {
Sfr sfr.Config
TextMagic textmagic.Config
Melipayamak melipayamak.Config
Mattermost mattermost.Config
}

Receivers []ReceiverConf
Expand Down
3 changes: 3 additions & 0 deletions cmd/sachet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

66 changes: 66 additions & 0 deletions provider/mattermost/mattermost.go
Original file line number Diff line number Diff line change
@@ -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
}