Skip to content

Commit

Permalink
feat: fix twilio template error
Browse files Browse the repository at this point in the history
  • Loading branch information
UsherFall committed Oct 11, 2023
1 parent daf1cdf commit f9f1d1b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
5 changes: 3 additions & 2 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func (a *AmazonSNSClient) SendMessage(param map[string]string, targetPhoneNumber

for i := 1; i < len(targetPhoneNumber); i++ {
_, err := a.svc.Publish(&sns.PublishInput{
Message: &a.template,
PhoneNumber: &targetPhoneNumber[i],
Message: &a.template,
PhoneNumber: &targetPhoneNumber[i],
MessageAttributes: messageAttributes,
})

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewSmsClient(provider string, accessId string, accessKey string, sign strin
case GCCPAY:
return GetGCCPAYClient(accessId, accessKey, template)
case Infobip:
return GetInfobipClient(accessId, accessKey, other)
return GetInfobipClient(accessId, accessKey, template, other)
case SUBMAIL:
return GetSubmailClient(accessId, accessKey, template)
case SmsBao:
Expand Down
19 changes: 11 additions & 8 deletions infobip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
)

type InfobipClient struct {
baseUrl string
sender string
apiKey string
baseUrl string
sender string
apiKey string
template string
}

type InfobipConfigService struct {
Expand All @@ -52,15 +53,16 @@ type Destination struct {
To string `json:"to"`
}

func GetInfobipClient(sender string, apiKey string, baseUrl []string) (*InfobipClient, error) {
func GetInfobipClient(sender string, apiKey string, template string, baseUrl []string) (*InfobipClient, error) {
if len(baseUrl) < 1 {
return nil, fmt.Errorf("missing parameter: baseUrl")
}

infobipClient := &InfobipClient{
baseUrl: baseUrl[0],
sender: sender,
apiKey: apiKey,
baseUrl: baseUrl[0],
sender: sender,
apiKey: apiKey,
template: template,
}

return infobipClient, nil
Expand All @@ -86,6 +88,7 @@ func (c *InfobipClient) SendMessage(param map[string]string, targetPhoneNumber .
}

endpoint := fmt.Sprintf("%s/sms/2/text/advanced", c.baseUrl)
text := fmt.Sprintf(c.template, code)

messageData := MessageData{
Messages: []Message{
Expand All @@ -96,7 +99,7 @@ func (c *InfobipClient) SendMessage(param map[string]string, targetPhoneNumber .
To: mobile,
},
},
Text: code,
Text: text,
},
},
}
Expand Down
13 changes: 6 additions & 7 deletions twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ package go_sms_sender

import (
"fmt"
"strings"

"github.com/twilio/twilio-go"
openapi "github.com/twilio/twilio-go/rest/api/v2010"

)

type TwilioClient struct {
Expand All @@ -44,12 +41,14 @@ func GetTwilioClient(accessId string, accessKey string, template string) (*Twili

// SendMessage targetPhoneNumber[0] is the sender's number, so targetPhoneNumber should have at least two parameters
func (c *TwilioClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
code, ok := param["code"]
if !ok {
return fmt.Errorf("missing parameter: msg code")
}

var err error
bodyContent := c.template

for k, v := range param {
bodyContent = strings.Replace(bodyContent, "${"+k+"}", v, -1)
}
bodyContent := fmt.Sprintf(c.template, code)

if len(targetPhoneNumber) < 2 {
return fmt.Errorf("bad parameter: targetPhoneNumber")
Expand Down

0 comments on commit f9f1d1b

Please sign in to comment.