From 7d92aaece27a9e208e93cf9acf8d227af3022a88 Mon Sep 17 00:00:00 2001 From: Domagoj Smoljanovic Date: Tue, 23 Nov 2021 00:29:55 +0100 Subject: [PATCH 1/2] Use v2 API for Infobip provider and support sending messages to multiple recipients --- provider/infobip/infobip.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/provider/infobip/infobip.go b/provider/infobip/infobip.go index 4f3ee1c..5063110 100644 --- a/provider/infobip/infobip.go +++ b/provider/infobip/infobip.go @@ -24,6 +24,20 @@ type Infobip struct { Config } +type InfobipDestination struct { + To string `json:"to"` +} + +type InfobipMessage struct { + From string `json:"from"` + Destinations []InfobipDestination `json:"destinations"` + Text string `json:"text"` +} + +type InfobipPayload struct { + Messages []InfobipMessage `json:"messages"` +} + //NewInfobip creates a new func NewInfobip(config Config) *Infobip { Infobip := &Infobip{config} @@ -32,13 +46,30 @@ func NewInfobip(config Config) *Infobip { //Send send sms to n number of people using bulk sms api func (c *Infobip) Send(message sachet.Message) (err error) { - smsURL := "https://api.infobip.com/sms/1/text/single" + smsURL := "https://api.infobip.com/sms/2/text/advanced" //smsURL = "http://requestb.in/pwf2ufpw" var request *http.Request var resp *http.Response - dataMap := map[string]string{"from": message.From, "to": message.To[0], "text": message.Text} - data, _ := json.Marshal(dataMap) + payload := InfobipPayload{} + payload.Messages = append(payload.Messages, InfobipMessage{}) + payload.Messages[0].From = message.From + payload.Messages[0].Text = message.Text + + for _, destination := range message.To { + payload.Messages[0].Destinations = append( + payload.Messages[0].Destinations, + InfobipDestination{ + To: destination, + }, + ) + } + + data, err := json.Marshal(payload) + if err != nil { + return err + } + //preparing the request request, err = http.NewRequest("POST", smsURL, bytes.NewBuffer(data)) if err != nil { From 48f50e266ff28e9072fb21f724ab4a7f24dbd387 Mon Sep 17 00:00:00 2001 From: Domagoj Smoljanovic Date: Tue, 23 Nov 2021 15:24:57 +0100 Subject: [PATCH 2/2] formatting fixups on infobip provider --- provider/infobip/infobip.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/provider/infobip/infobip.go b/provider/infobip/infobip.go index 5063110..41afae1 100644 --- a/provider/infobip/infobip.go +++ b/provider/infobip/infobip.go @@ -25,17 +25,17 @@ type Infobip struct { } type InfobipDestination struct { - To string `json:"to"` + To string `json:"to"` } type InfobipMessage struct { - From string `json:"from"` - Destinations []InfobipDestination `json:"destinations"` - Text string `json:"text"` + From string `json:"from"` + Destinations []InfobipDestination `json:"destinations"` + Text string `json:"text"` } type InfobipPayload struct { - Messages []InfobipMessage `json:"messages"` + Messages []InfobipMessage `json:"messages"` } //NewInfobip creates a new @@ -51,19 +51,19 @@ func (c *Infobip) Send(message sachet.Message) (err error) { var request *http.Request var resp *http.Response - payload := InfobipPayload{} - payload.Messages = append(payload.Messages, InfobipMessage{}) - payload.Messages[0].From = message.From - payload.Messages[0].Text = message.Text - - for _, destination := range message.To { - payload.Messages[0].Destinations = append( - payload.Messages[0].Destinations, - InfobipDestination{ - To: destination, - }, - ) - } + payload := InfobipPayload{} + payload.Messages = append(payload.Messages, InfobipMessage{}) + payload.Messages[0].From = message.From + payload.Messages[0].Text = message.Text + + for _, destination := range message.To { + payload.Messages[0].Destinations = append( + payload.Messages[0].Destinations, + InfobipDestination{ + To: destination, + }, + ) + } data, err := json.Marshal(payload) if err != nil {