-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.go
71 lines (61 loc) · 1.79 KB
/
sms.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package africastalking
import (
"bytes"
"context"
"encoding/json"
)
const (
smsApiPath = "messaging/bulk"
)
type (
// BulkSMSInput is passed to SendBulkSMS as a parameter.
BulkSMSInput struct {
Message string `json:"message"`
SenderID string `json:"senderId"`
PhoneNumbers []string `json:"phoneNumbers"`
}
// BulkSMSRecipient is returned as part of the BulkSMSResponse.
BulkSMSRecipient struct {
StatusCode uint `json:"statusCode"`
Number string `json:"number"`
Status string `json:"status"`
Cost string `json:"cost"`
MessageID string `json:"messageId"`
}
// BulkSMSResponse is returned by SendBulkSMS as a response.
BulkSMSResponse struct {
SMSMessageData struct {
Message string `json:"Message"`
Recipients []BulkSMSRecipient `json:"Recipients"`
} `json:"SMSMessageData"`
}
)
// SendBulkSMS makes a POST request to send bulk SMS's the Africa's Talking and returns a response.
// It uses opinionated defaults.
func (at *AtClient) SendBulkSMS(ctx context.Context, input BulkSMSInput) (BulkSMSResponse, error) {
var (
buf bytes.Buffer
bulkSMSResponse BulkSMSResponse
)
if err := json.NewEncoder(&buf).Encode(struct {
Username string `json:"username"`
Message string `json:"message"`
SenderID string `json:"senderId"`
PhoneNumbers []string `json:"phoneNumbers"`
}{
Username: at.username,
Message: input.Message,
SenderID: input.SenderID,
PhoneNumbers: input.PhoneNumbers,
}); err != nil {
return bulkSMSResponse, err
}
resp, err := at.postRequestWithCtx(ctx, at.endpoint+smsApiPath, &buf)
if err != nil {
return bulkSMSResponse, err
}
if err := parseResponse(resp, &bulkSMSResponse); err != nil {
return bulkSMSResponse, err
}
return bulkSMSResponse, nil
}