From 0dfba41f5afa555d567b9037cd8031a71b1f0fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Pr=C3=A4hauser?= Date: Fri, 17 Jun 2022 10:04:50 +0200 Subject: [PATCH 1/3] Add provider for websms.com (SMS/text message gateway service) See https://www.websms.com/ and https://developer.websms.com/web-api/ --- cmd/sachet/config.go | 2 + cmd/sachet/main.go | 3 ++ examples/config.yaml | 7 ++++ provider/websms/README.md | 12 ++++++ provider/websms/websms.go | 85 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 provider/websms/README.md create mode 100644 provider/websms/websms.go diff --git a/cmd/sachet/config.go b/cmd/sachet/config.go index 747702af..f14aa437 100644 --- a/cmd/sachet/config.go +++ b/cmd/sachet/config.go @@ -34,6 +34,7 @@ import ( "github.com/messagebird/sachet/provider/textmagic" "github.com/messagebird/sachet/provider/turbosms" "github.com/messagebird/sachet/provider/twilio" + "github.com/messagebird/sachet/provider/websms" ) type ReceiverConf struct { @@ -75,6 +76,7 @@ var config struct { Ghasedak ghasedak.Config Sfr sfr.Config TextMagic textmagic.Config + WebSms websms.Config } Receivers []ReceiverConf diff --git a/cmd/sachet/main.go b/cmd/sachet/main.go index 60d9b9f8..46525c0b 100644 --- a/cmd/sachet/main.go +++ b/cmd/sachet/main.go @@ -42,6 +42,7 @@ import ( "github.com/messagebird/sachet/provider/textmagic" "github.com/messagebird/sachet/provider/turbosms" "github.com/messagebird/sachet/provider/twilio" + "github.com/messagebird/sachet/provider/websms" ) var ( @@ -147,6 +148,8 @@ func providerByName(name string) (sachet.Provider, error) { return sfr.NewSfr(config.Providers.Sfr), nil case "textmagic": return textmagic.NewTextMagic(config.Providers.TextMagic), nil + case "websms": + return websms.NewWebSms(config.Providers.WebSms), nil } return nil, fmt.Errorf("%s: Unknown provider", name) diff --git a/examples/config.yaml b/examples/config.yaml index 903a505f..eee229b7 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -104,6 +104,8 @@ providers: textmagic: username: 'username' api_key: 'TEXTMAGIC_API_KEY' + websms: + api_token: 'WEBSMS_API_TOKEN' templates: - telegram.tmpl @@ -152,3 +154,8 @@ receivers: to: - '09012345679' - '09123456789' + + - name: 'websms' + provider: 'websms' + to: + - '09012345679' diff --git a/provider/websms/README.md b/provider/websms/README.md new file mode 100644 index 00000000..04c602d7 --- /dev/null +++ b/provider/websms/README.md @@ -0,0 +1,12 @@ +# websms.com (Link Mobility) + +This sachet provider uses the *websms* SMS gateway service (websms.com). This service is popular in Austria and other European countries. + +See https://www.websms.com/ and https://developer.websms.com/web-api/ + + +## Configuration + +This provieder needs an API key to access the REST API. To create one login to your websms account got to "API administration" and "API access data". There you can create a new API token for use in this provider. + +Note: use a dedicate API token for access by sachet. **DONT use tokens created for productive systems and services**. \ No newline at end of file diff --git a/provider/websms/websms.go b/provider/websms/websms.go new file mode 100644 index 00000000..35c62b9c --- /dev/null +++ b/provider/websms/websms.go @@ -0,0 +1,85 @@ +package websms + +import ( + "bytes" + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httputil" + "time" + + "github.com/messagebird/sachet" +) + +const websmsBaseURL = "https://api.websms.com/rest/" + +var httpClient = &http.Client{Timeout: 20 * time.Second} + +// Config is the configuration struct for websms provider. +type Config struct { + Token string `yaml:"api_token"` +} + +var _ (sachet.Provider) = (*WebSms)(nil) + +// Websms contains the necessary values for the WebSms provider. +type WebSms struct { + Config +} + +// NewWebSms creates and returns a new Websms struct. +func NewWebSms(config Config) *WebSms { + return &WebSms{config} +} + +type TextSmsSendRequest struct { + SmsID string `json:"clientMessageId"` + RecipientList []string `json:"recipientAddressList"` + Message string `json:"messageContent"` +} + +// Send sends SMS to user registered in configuration. +func (c *WebSms) Send(message sachet.Message) error { + + // Prepare SMS request payload + smsReq := TextSmsSendRequest{ + RecipientList: message.To, + Message: message.Text, + } + + jsonSmsReq, err := json.Marshal(smsReq) + if err != nil { + return err + } + + // Create HTTP POST request + request, err := http.NewRequest("POST", websmsBaseURL + "smsmessaging/text", bytes.NewBuffer(jsonSmsReq)) + if err != nil { + return err + } + + //request.SetBasicAuth(c.Username, c.Password) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("User-Agent", "Sachet") + request.Header.Set("Authorization", "Bearer " + c.Token) + + reqDump, err := httputil.DumpRequest(request, true) + if err != nil { + return err + } + log.Println(fmt.Sprintf("REQUEST: %q", reqDump)) + + // Send HTTP request + response, err := httpClient.Do(request) + if err != nil { + return err + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + return fmt.Errorf("Failed to send sms. statusCode: %d", response.StatusCode) + } + + return nil +} From 95e8631539846036bb14d36efb4ba072d2cdd682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Pr=C3=A4hauser?= Date: Fri, 17 Jun 2022 10:16:13 +0200 Subject: [PATCH 2/3] Remove debug code --- provider/websms/README.md | 4 +++- provider/websms/websms.go | 13 +++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/provider/websms/README.md b/provider/websms/README.md index 04c602d7..fcb3a29a 100644 --- a/provider/websms/README.md +++ b/provider/websms/README.md @@ -9,4 +9,6 @@ See https://www.websms.com/ and https://developer.websms.com/web-api/ This provieder needs an API key to access the REST API. To create one login to your websms account got to "API administration" and "API access data". There you can create a new API token for use in this provider. -Note: use a dedicate API token for access by sachet. **DONT use tokens created for productive systems and services**. \ No newline at end of file +Note: use a dedicate API token for access by sachet. **DONT use tokens created for productive systems and services**. + +You can specifiy a list of target phone numbers which will reveive messages. You have to specificy those numbers in E.164/MSISDN format, e.g. "49123456789" (otherwise often written as "+49123456789" or "0049123456789"). See https://en.wikipedia.org/wiki/MSISDN \ No newline at end of file diff --git a/provider/websms/websms.go b/provider/websms/websms.go index 35c62b9c..b36152f7 100644 --- a/provider/websms/websms.go +++ b/provider/websms/websms.go @@ -6,7 +6,6 @@ import ( "fmt" "log" "net/http" - "net/http/httputil" "time" "github.com/messagebird/sachet" @@ -34,9 +33,9 @@ func NewWebSms(config Config) *WebSms { } type TextSmsSendRequest struct { - SmsID string `json:"clientMessageId"` + SmsID string `json:"clientMessageId"` RecipientList []string `json:"recipientAddressList"` - Message string `json:"messageContent"` + Message string `json:"messageContent"` } // Send sends SMS to user registered in configuration. @@ -59,17 +58,10 @@ func (c *WebSms) Send(message sachet.Message) error { return err } - //request.SetBasicAuth(c.Username, c.Password) request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "Sachet") request.Header.Set("Authorization", "Bearer " + c.Token) - reqDump, err := httputil.DumpRequest(request, true) - if err != nil { - return err - } - log.Println(fmt.Sprintf("REQUEST: %q", reqDump)) - // Send HTTP request response, err := httpClient.Do(request) if err != nil { @@ -81,5 +73,6 @@ func (c *WebSms) Send(message sachet.Message) error { return fmt.Errorf("Failed to send sms. statusCode: %d", response.StatusCode) } + // Messages successfully sent return nil } From d42425015921edce22dece51b17bb755c4d0736c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Pr=C3=A4hauser?= Date: Fri, 17 Jun 2022 11:08:03 +0200 Subject: [PATCH 3/3] Remove unused log pkg --- provider/websms/websms.go | 1 - 1 file changed, 1 deletion(-) diff --git a/provider/websms/websms.go b/provider/websms/websms.go index b36152f7..b6fa8dad 100644 --- a/provider/websms/websms.go +++ b/provider/websms/websms.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "log" "net/http" "time"