-
Notifications
You must be signed in to change notification settings - Fork 2
/
sms.go
68 lines (55 loc) · 1.23 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
package telapi
import (
"encoding/json"
"errors"
)
type SmsMessage struct {
Sid string
Body string
Status string
From string
To string
Direction string
DateUpdated string
Price string
Uri string
AccountSid string
DateSent string
DateCreated string
ApiVersion string
}
func (helper TelapiHelper) SendSMS(to string, from string, body string) (*SmsMessage, error) {
if to == "" {
return nil, errors.New("Missing required To.")
}
data := map[string]string{
"To": to,
"Body": body,
"AllowMultiple": "true",
}
if from != "" {
data["From"] = from
}
resp, err := helper.PostRequest("/SMS/Messages", data)
if err != nil {
return nil, err
}
//Lets unmarshal our response
var f interface{}
err = json.Unmarshal(*resp, &f)
if err != nil {
return nil, err
}
data_map := f.(map[string]interface{})
response_list := data_map["sms_messages"]
messageList := response_list.([]interface{}) //array of interfaces
msg_data, err := json.Marshal(messageList[0].(map[string]interface{}))
if err != nil {
return nil, err
}
sms := new(SmsMessage)
if err = json.Unmarshal(msg_data, &sms); err != nil {
return nil, err
}
return sms, nil
}