From 62f63ef0457aebb7fcdf2ebdde588208245545b5 Mon Sep 17 00:00:00 2001 From: Christian Kelly Date: Wed, 8 May 2019 15:01:38 +0100 Subject: [PATCH] Adding clickatell support --- cmd/sachet/config.go | 2 + cmd/sachet/main.go | 3 ++ provider/clickatell/clickatell.go | 63 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 provider/clickatell/clickatell.go diff --git a/cmd/sachet/config.go b/cmd/sachet/config.go index 3c5fef9..bcb3538 100644 --- a/cmd/sachet/config.go +++ b/cmd/sachet/config.go @@ -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" @@ -44,6 +45,7 @@ var config struct { FreeMobile freemobile.Config AspSms aspsms.Config Sipgate sipgate.Config + Clickatell clickatell.ClickatellConfig } Receivers []ReceiverConf diff --git a/cmd/sachet/main.go b/cmd/sachet/main.go index 840489e..d1ebc3d 100644 --- a/cmd/sachet/main.go +++ b/cmd/sachet/main.go @@ -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" @@ -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) diff --git a/provider/clickatell/clickatell.go b/provider/clickatell/clickatell.go new file mode 100644 index 0000000..d5f9d05 --- /dev/null +++ b/provider/clickatell/clickatell.go @@ -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) +}