Skip to content

Commit

Permalink
Merge pull request messagebird#111 from thiperson/add-sfr-dmc-provider
Browse files Browse the repository at this point in the history
Add sfr dmc provider
  • Loading branch information
marcel corso gonzalez committed Apr 21, 2022
2 parents 2881390 + 9f951ec commit 05b289f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/sachet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/messagebird/sachet/provider/ovh"
"github.com/messagebird/sachet/provider/pushbullet"
"github.com/messagebird/sachet/provider/sap"
"github.com/messagebird/sachet/provider/sfr"
"github.com/messagebird/sachet/provider/sipgate"
"github.com/messagebird/sachet/provider/sms77"
"github.com/messagebird/sachet/provider/smsc"
Expand Down Expand Up @@ -72,6 +73,7 @@ var config struct {
Esendex esendex.Config
Sms77 sms77.Config
Ghasedak ghasedak.Config
Sfr sfr.Config
TextMagic textmagic.Config
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/sachet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/messagebird/sachet/provider/ovh"
"github.com/messagebird/sachet/provider/pushbullet"
"github.com/messagebird/sachet/provider/sap"
"github.com/messagebird/sachet/provider/sfr"
"github.com/messagebird/sachet/provider/sipgate"
"github.com/messagebird/sachet/provider/sms77"
"github.com/messagebird/sachet/provider/smsc"
Expand Down Expand Up @@ -142,6 +143,8 @@ func providerByName(name string) (sachet.Provider, error) {
return sms77.NewSms77(config.Providers.Sms77), nil
case "ghasedak":
return ghasedak.NewGhasedak(config.Providers.Ghasedak), nil
case "sfr":
return sfr.NewSfr(config.Providers.Sfr), nil
case "textmagic":
return textmagic.NewTextMagic(config.Providers.TextMagic), nil
}
Expand Down
14 changes: 14 additions & 0 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ providers:
debug: false
ghasedak:
api_token: 'GHASEDAK_API_KEY'
sfr:
url: "https://www.dmc.sfr-sh.fr/DmcWS/1.5.7/JsonService/MessagesUnitairesWS/addSingleCall"
service_id: ""
service_password: ""
space_id: ""
lang: "fr_FR"
tpoa: "TPOA"
textmagic:
username: 'username'
api_key: 'TEXTMAGIC_API_KEY'
Expand Down Expand Up @@ -127,6 +134,13 @@ receivers:
provider: "sap"
to:
- '+336xxxxxxxx'

- name: 'sfr'
provider: "sfr"
to:
- '+336xxxxxxxx'
- '+336xxxxxxxx'

- name: 'kavenegar'
provider: 'kavenegar'
from: '10008663'
Expand Down
133 changes: 133 additions & 0 deletions provider/sfr/sfr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package sfr

import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/messagebird/sachet"
)

// Config is the configuration struct for Sfr provider.
type Config struct {
URL string `yaml:"url"`
SPACEID string `yaml:"space_id"`
SERVICEID string `yaml:"service_id"`
SERVICEPASSWORD string `yaml:"service_password"`
LANG string `yaml:"lang"`
TPOA string `yaml:"tpoa"`
}

type Authenticate struct {
ServiceId string `json:"serviceId"`
ServicePassword string `json:"servicePassword"`
SpaceId string `json:"spaceId"`
Lang string `json:"lang"`
}

type MessageUnitaire struct {
Media string `json:"media"`
TextMsg string `json:"textMsg"`
To string `json:"to"`
From string `json:"from"`
}

type ResponseBody struct {
Success bool `json:"success"`
ErrorCode string `json:"errorCode"`
ErrorDetail string `json:"errorDetail"`
Fatal bool `json:"fatal"`
InvalidParams bool `json:"invalidParams"`
Response int64 `json:"response"`
}

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

// NewSfr creates and returns a new Sfr struct.
func NewSfr(config Config) *Sfr {
if config.URL == "" {
config.URL = "https://www.dmc.sfr-sh.fr/DmcWS/1.5.7/JsonService/MessagesUnitairesWS/addSingleCall"
}
return &Sfr{
config,
&http.Client{Timeout: time.Second * 20},
}
}

// Send sends SMS to user registered in configuration.
func (c *Sfr) Send(message sachet.Message) error {
// No \n in Text tolerated.
msg := strings.ReplaceAll(message.Text, "\n", " - ")

errors := 0
for _, dest := range message.To {
request, err := http.NewRequest("GET", c.URL, nil)
if err != nil {
return err
}

authenticate := &Authenticate{
ServiceId: c.SERVICEID,
ServicePassword: c.SERVICEPASSWORD,
SpaceId: c.SPACEID,
Lang: c.LANG,
}

params := request.URL.Query()

messageUnitaire := &MessageUnitaire{
Media: "SMSLong",
TextMsg: msg,
To: dest,
From: c.TPOA,
}

a, err := json.Marshal(authenticate)
if err != nil {
return fmt.Errorf("error: %w", err)
}

mU, err := json.Marshal(messageUnitaire)
if err != nil {
return fmt.Errorf("error: %w", err)
}

params.Add("authenticate", string(a))
params.Add("messageUnitaire", string(mU))
request.URL.RawQuery = params.Encode()

response, err := c.HTTPClient.Do(request)
if err != nil {
errors += 1
fmt.Println(err)
}
defer response.Body.Close()
dec := json.NewDecoder(response.Body)

var responseBody ResponseBody
for dec.More() {
err := dec.Decode(&responseBody)
if err != nil {
fmt.Println("Can not decode JSON")
errors += 1
}
}

if !responseBody.Success {
fmt.Println("API error :", responseBody)
errors += 1
} else {
fmt.Println("Successfully sent alert to ", dest)
}
}
if errors > 0 {
return fmt.Errorf("Error with %d calls", errors)
}
return nil
}

0 comments on commit 05b289f

Please sign in to comment.