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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clickatell provider #57

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -16,6 +16,7 @@ import (
"github.com/messagebird/sachet/provider/telegram"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"
"github.com/messagebird/sachet/provider/clickatell"

"github.com/prometheus/alertmanager/template"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -44,6 +45,7 @@ var config struct {
FreeMobile freemobile.Config
AspSms aspsms.Config
Sipgate sipgate.Config
Clickatell clickatell.ClickatellConfig
}

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 @@ -24,6 +24,7 @@ import (
"github.com/messagebird/sachet/provider/telegram"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"
"github.com/messagebird/sachet/provider/clickatell"

"github.com/prometheus/alertmanager/template"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -174,6 +175,8 @@ func providerByName(name string) (sachet.Provider, error) {
return aspsms.NewAspSms(config.Providers.AspSms), nil
case "sipgate":
return sipgate.NewSipgate(config.Providers.Sipgate), nil
case "clickatell":
return clickatell.NewClickatell(config.Providers.Clickatell), nil
}

return nil, fmt.Errorf("%s: Unknown provider", name)
Expand Down
63 changes: 63 additions & 0 deletions provider/clickatell/clickatell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package clickatell

import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/messagebird/sachet"
)

type ClickatellConfig struct {
User string `yaml:"user"`
Password string `yaml:"password"`
ApiId string `yaml:"api_id"`
}

const ClickatellRequestTimeout = time.Second * 60

type Clickatell struct {
User string
Password string
ApiId string
}

func NewClickatell(config ClickatellConfig) *Clickatell {
Clickatell := &Clickatell{User: config.User, Password: config.Password, ApiId: config.ApiId}
return Clickatell
}

func (c *Clickatell) Send(message sachet.Message) (err error) {
for _, number := range message.To {
err = c.SendOne(message, number)
if err != nil {
return fmt.Errorf("Failed to make API call to clickatell:%s", err)
}
}
return
}

func (c *Clickatell) SendOne(message sachet.Message, PhoneNumber string) (err error) {
fmt.Printf("ALERT : %s\n", message.Text)
encoded_message := url.QueryEscape(message.Text)
smsURL := fmt.Sprintf("http://api.clickatell.com/http/sendmsg?user=%s&password=%s&api_id=%s&to=%s&text=%s", c.User, c.Password, c.ApiId, PhoneNumber, encoded_message)
var request *http.Request
var resp *http.Response
request, err = http.NewRequest("GET", smsURL, nil)
if err != nil {
return
}
httpClient := &http.Client{}
httpClient.Timeout = ClickatellRequestTimeout
resp, err = httpClient.Do(request)
if err != nil {
return
}
defer resp.Body.Close()
var body []byte
resp.Body.Read(body)
if resp.StatusCode == http.StatusOK && err == nil {
return
}
return fmt.Errorf("Failed sending sms:Reason: %s, StatusCode : %d", string(body), resp.StatusCode)
}