-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsms.go
More file actions
79 lines (68 loc) · 1.77 KB
/
sms.go
File metadata and controls
79 lines (68 loc) · 1.77 KB
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
72
73
74
75
76
77
78
79
package sipcentric
import (
"encoding/json"
"fmt"
"strings"
)
type SmsHistoryItem struct {
Uri string `json:"uri"`
Created string `json:"created"`
Direction string `json:"direction"`
From string `json:"from"`
To string `json:"to"`
Body string `json:"body"`
SendStatus string `json:"sendStatus,omitempty"`
DeliveryStatus int `json:"deliveryStatus,omitempty"`
Cost float32 `json:"cost"`
}
type SmsHistoryResult struct {
TotalItems int `json:"totalItems"`
PageSize int `json:"pageSize"`
Page int `json:"page"`
Items []SmsHistoryItem `json:"items"`
}
func (api *Api) SmsHistory(page int, pageSize int) (*SmsHistoryResult, error) {
resp, err := api.apiRequest("GET", fmt.Sprintf("/customers/me/sms?page=%d&pageSize=%d", page, pageSize), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// parse result
switch resp.StatusCode {
case 200: // OK
r := &SmsHistoryResult{}
err := json.NewDecoder(resp.Body).Decode(r)
if err != nil {
return nil, err
}
return r, err
default:
return nil, fmt.Errorf("Invalid response code %d", resp.StatusCode)
}
}
func (api *Api) SendSms(from string, to string, message string) error {
// build post data
post := `
{
"type": "smsmessage",
"from": "` + from + `",
"to": "` + to + `",
"body": "` + message + `"
}
`
// make request
s := strings.NewReader(post)
resp, err := api.apiRequest("POST", "/customers/me/sms", s)
if err != nil {
return err
}
defer resp.Body.Close()
// 201 = OK, other responses undocumented
switch resp.StatusCode {
case 201: // CREATED
return nil
default:
err := fmt.Errorf("SMS send failure; %d", resp.Status)
return err
}
}