-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendsms.go
57 lines (44 loc) · 1.1 KB
/
sendsms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package client
import (
"context"
"net/http"
"net/url"
"strings"
)
type SmsParams struct {
Originator string
Numbers string
Body string
Dummy string
}
// -d ORIGINATOR=84988
// -d NUMBERS=447111222333
// -d BODY=Welcome%20Home
// -d DUMMY=yes
func (smsParams *SmsParams) ToParams() string {
data := url.Values{}
data.Set("ORIGINATOR", smsParams.Originator)
data.Set("NUMBERS", smsParams.Numbers)
data.Set("BODY", smsParams.Body)
if strings.ToUpper(smsParams.Dummy) == "YES" {
data.Set("DUMMY", "yes")
}
return data.Encode()
}
func (client *Client) SendSms(ctx context.Context, smsParams *SmsParams) (*SuccessResponse, error) {
apiUrl, err := client.apiUrlPath(V2_SENDSMS)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", apiUrl, strings.NewReader(smsParams.ToParams()))
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response := SuccessResponseWrapper{}
if err = client.sendRequest(req, &response); err != nil {
return nil, err
}
return &response.SuccessData, nil
}