forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
186 lines (151 loc) · 6.52 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package gotwilio
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// SmsResponse is returned after a text/sms message is posted to Twilio
type SmsResponse struct {
Sid string `json:"sid"`
DateCreated string `json:"date_created"`
DateUpdate string `json:"date_updated"`
DateSent string `json:"date_sent"`
AccountSid string `json:"account_sid"`
To string `json:"to"`
From string `json:"from"`
NumMedia string `json:"num_media"`
Body string `json:"body"`
Status string `json:"status"`
Direction string `json:"direction"`
ApiVersion string `json:"api_version"`
Price *string `json:"price,omitempty"`
Url string `json:"uri"`
}
// DateCreatedAsTime returns SmsResponse.DateCreated as a time.Time object
// instead of a string.
func (sms *SmsResponse) DateCreatedAsTime() (time.Time, error) {
return time.Parse(time.RFC1123Z, sms.DateCreated)
}
// DateUpdateAsTime returns SmsResponse.DateUpdate as a time.Time object
// instead of a string.
func (sms *SmsResponse) DateUpdateAsTime() (time.Time, error) {
return time.Parse(time.RFC1123Z, sms.DateUpdate)
}
// DateSentAsTime returns SmsResponse.DateSent as a time.Time object
// instead of a string.
func (sms *SmsResponse) DateSentAsTime() (time.Time, error) {
return time.Parse(time.RFC1123Z, sms.DateSent)
}
func whatsapp(phone string) string {
return "whatsapp:" + phone
}
// SendWhatsApp uses Twilio to send a WhatsApp message.
// See https://www.twilio.com/docs/sms/whatsapp/tutorial/send-and-receive-media-messages-whatsapp-python
func (twilio *Twilio) SendWhatsApp(from, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) {
return twilio.SendSMS(whatsapp(from), whatsapp(to), body, statusCallback, applicationSid)
}
// SendWhatsAppMedia uses Twilio to send a WhatsApp message with Media enabled.
// See https://www.twilio.com/docs/sms/whatsapp/tutorial/send-and-receive-media-messages-whatsapp-python
func (twilio *Twilio) SendWhatsAppMedia(from, to, body string, mediaURL []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) {
formValues := initFormValues(whatsapp(to), body, mediaURL, statusCallback, applicationSid)
formValues.Set("From", whatsapp(from))
return twilio.sendMessage(formValues)
}
// SendSMS uses Twilio to send a text message.
// See http://www.twilio.com/docs/api/rest/sending-sms for more information.
func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) {
formValues := initFormValues(to, body, nil, statusCallback, applicationSid)
formValues.Set("From", from)
smsResponse, exception, err = twilio.sendMessage(formValues)
return
}
// GetSMS uses Twilio to get information about a text message.
// See https://www.twilio.com/docs/api/rest/sms for more information.
func (twilio *Twilio) GetSMS(sid string) (smsResponse *SmsResponse, exception *Exception, err error) {
twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/SMS/Messages/" + sid + ".json"
res, err := twilio.get(twilioUrl)
if err != nil {
return smsResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return smsResponse, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return smsResponse, exception, err
}
smsResponse = new(SmsResponse)
err = json.Unmarshal(responseBody, smsResponse)
return smsResponse, exception, err
}
// SendSMSWithCopilot uses Twilio Copilot to send a text message.
// See https://www.twilio.com/docs/api/rest/sending-messages-copilot
func (twilio *Twilio) SendSMSWithCopilot(messagingServiceSid, to, body, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) {
formValues := initFormValues(to, body, nil, statusCallback, applicationSid)
formValues.Set("MessagingServiceSid", messagingServiceSid)
smsResponse, exception, err = twilio.sendMessage(formValues)
return
}
// SendMMS uses Twilio to send a multimedia message.
func (twilio *Twilio) SendMMS(from, to, body string, mediaUrl []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) {
formValues := initFormValues(to, body, mediaUrl, statusCallback, applicationSid)
formValues.Set("From", from)
smsResponse, exception, err = twilio.sendMessage(formValues)
return
}
// SendMMSWithCopilot uses Twilio Copilot to send a multimedia message.
// See https://www.twilio.com/docs/api/rest/sending-messages-copilot
func (twilio *Twilio) SendMMSWithCopilot(messagingServiceSid, to, body string, mediaUrl []string, statusCallback, applicationSid string) (smsResponse *SmsResponse, exception *Exception, err error) {
formValues := initFormValues(to, body, mediaUrl, statusCallback, applicationSid)
formValues.Set("MessagingServiceSid", messagingServiceSid)
smsResponse, exception, err = twilio.sendMessage(formValues)
return
}
// Core method to send message
func (twilio *Twilio) sendMessage(formValues url.Values) (smsResponse *SmsResponse, exception *Exception, err error) {
twilioUrl := twilio.BaseUrl + "/Accounts/" + twilio.AccountSid + "/Messages.json"
res, err := twilio.post(formValues, twilioUrl)
if err != nil {
return smsResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return smsResponse, exception, err
}
if res.StatusCode != http.StatusCreated {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return smsResponse, exception, err
}
smsResponse = new(SmsResponse)
err = json.Unmarshal(responseBody, smsResponse)
return smsResponse, exception, err
}
// Form values initialization
func initFormValues(to, body string, mediaUrl []string, statusCallback, applicationSid string) url.Values {
formValues := url.Values{}
formValues.Set("To", to)
formValues.Set("Body", body)
if len(mediaUrl) > 0 {
for _, value := range mediaUrl {
formValues.Add("MediaUrl", value)
}
}
if statusCallback != "" {
formValues.Set("StatusCallback", statusCallback)
}
if applicationSid != "" {
formValues.Set("ApplicationSid", applicationSid)
}
return formValues
}