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

Support sap sms 365 digital interconnect #85

Merged
merged 6 commits into from
Apr 23, 2021
Merged
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 @@ -18,6 +18,7 @@ import (
"github.com/messagebird/sachet/provider/otc"
"github.com/messagebird/sachet/provider/ovh"
"github.com/messagebird/sachet/provider/pushbullet"
"github.com/messagebird/sachet/provider/sap"
"github.com/messagebird/sachet/provider/sipgate"
"github.com/messagebird/sachet/provider/smsc"
"github.com/messagebird/sachet/provider/telegram"
Expand Down Expand Up @@ -59,6 +60,7 @@ var config struct {
Aliyun aliyun.Config
OVH ovh.Config
TencentCloud tencentcloud.Config
Sap sap.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 @@ -26,6 +26,7 @@ import (
"github.com/messagebird/sachet/provider/otc"
"github.com/messagebird/sachet/provider/ovh"
"github.com/messagebird/sachet/provider/pushbullet"
"github.com/messagebird/sachet/provider/sap"
"github.com/messagebird/sachet/provider/sipgate"
"github.com/messagebird/sachet/provider/smsc"
"github.com/messagebird/sachet/provider/telegram"
Expand Down Expand Up @@ -206,6 +207,8 @@ func providerByName(name string) (sachet.Provider, error) {
return ovh.NewOvh(config.Providers.OVH)
case "tencentcloud":
return tencentcloud.NewTencentCloud(config.Providers.TencentCloud)
case "sap":
return sap.NewSap(config.Providers.Sap), nil
}

return nil, fmt.Errorf("%s: Unknown provider", name)
Expand Down
8 changes: 8 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ providers:
sign_name: sign_name
template_code: 123233
truncate: true
sap:
url: "https://sms-pp.sapmobileservices.com/cmn/xxxxx/xxxxx.sms"
auth_hash: ""

templates:
- telegram.tmpl
Expand All @@ -101,3 +104,8 @@ receivers:
to:
- '332432432'
- '4242334534'

- name: 'sap'
provider: "sap"
to:
- '+336xxxxxxxx'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40
github.com/jmespath/go-jmespath v0.3.0 // indirect
github.com/messagebird/go-rest-api v5.3.0+incompatible
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014
github.com/prometheus/alertmanager v0.20.0
github.com/prometheus/client_golang v1.5.1
Expand Down
57 changes: 57 additions & 0 deletions provider/sap/sap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sap

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

// Config is the configuration struct for Sap provider
type Config struct {
URL string `yaml:"url"`
AuthHash string `yaml:"auth_hash"`
}

// Sap contains the necessary values for the Sap provider
type Sap struct {
Config
HTTPClient *http.Client // The HTTP client to send requests on
}

// NewSap creates and returns a new Sap struct
func NewSap(config Config) *Sap {
if config.URL == "" {
config.URL = "https://sms-pp.sapmobileservices.com/cmn/xxxxxxxxxx/xxxxxxxxxxx.sms"
}
return &Sap{
config,
&http.Client{Timeout: time.Second * 20},
}
}

// Send sends SMS to user registered in configuration
func (c *Sap) Send(message sachet.Message) error {

// No \n in Text tolerated
msg := strings.ReplaceAll(message.Text, "\n", " - ")
content := fmt.Sprintf("Version=2.0\nSubject=Alert\n[MSISDN]\nList=%s\n[MESSAGE]\nText=%s\n[SETUP]\nSplitText=yes\n[END]", strings.Join(message.To, ","), msg)

request, err := http.NewRequest("POST", c.URL, strings.NewReader(content))
if err != nil {
return err
}
request.Header.Set("Authorization", "Basic "+c.AuthHash)

response, err := c.HTTPClient.Do(request)
if err != nil {
return err
}

if response.StatusCode == http.StatusOK && err == nil {
return nil
}

return fmt.Errorf("Failed sending sms. statusCode: %d", response.StatusCode)
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ github.com/messagebird/go-rest-api/voicemessage
# github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
github.com/modern-go/concurrent
# github.com/modern-go/reflect2 v1.0.1
## explicit
github.com/modern-go/reflect2
# github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014
## explicit
Expand Down