github.com/subosito/twilio@v0.0.1/sms.go (about) 1 package twilio 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "net/url" 8 ) 9 10 type SMSResponse struct { 11 AccountSid string `json:"account_sid"` 12 ApiVersion string `json:"api_version"` 13 Body string `json:"body"` 14 DateCreated Timestamp `json:"date_created,omitempty"` 15 DateSent Timestamp `json:"date_sent,omitempty"` 16 DateUpdated Timestamp `json:"date_updated,omitempty"` 17 Direction string `json:"direction"` 18 From string `json:"from"` 19 Price Price `json:"price,omitempty"` 20 Sid string `json:"sid"` 21 Status string `json:"status"` 22 To string `json:"to"` 23 Uri string `json:"uri"` 24 } 25 26 type SMSListResponse struct { 27 Pagination 28 SMSMessages []SMSResponse 29 } 30 31 type SMSParams struct { 32 StatusCallback string 33 ApplicationSid string 34 } 35 36 func (t *Twilio) smsEndpoint() string { 37 return fmt.Sprintf("%s/Accounts/%s/SMS/Messages", t.BaseUrl, t.AccountSid) 38 } 39 40 // Simple version of Send SMS with no optional parameters support. 41 func (t *Twilio) SimpleSendSMS(from, to, body string) (*SMSResponse, error) { 42 return t.SendSMS(from, to, body, SMSParams{}) 43 } 44 45 // Send SMS with more verbose options. It's support optional parameters. 46 // StatusCallback : A URL that Twilio will POST to when your message is processed. 47 // ApplicationSid : Twilio will POST `SMSSid` as well as other statuses to the URL in the `SMSStatusCallback` property of this application 48 func (t *Twilio) SendSMS(from, to, body string, p SMSParams) (s *SMSResponse, err error) { 49 endpoint := fmt.Sprintf("%s.%s", t.smsEndpoint(), apiFormat) 50 params := url.Values{} 51 params.Set("From", from) 52 params.Set("To", to) 53 params.Set("Body", body) 54 55 if p.StatusCallback != "" { 56 params.Set("StatusCallback", p.StatusCallback) 57 } 58 59 if p.ApplicationSid != "" { 60 params.Set("ApplicationSid", p.ApplicationSid) 61 } 62 63 b, status, err := t.request("POST", endpoint, params) 64 if err != nil { 65 return 66 } 67 68 if status != http.StatusCreated { 69 e := new(Exception) 70 err = json.Unmarshal(b, &e) 71 if err != nil { 72 return 73 } 74 75 return nil, e 76 } 77 78 err = json.Unmarshal(b, &s) 79 if err != nil { 80 return nil, err 81 } 82 83 return 84 } 85 86 func (t *Twilio) GetSMS(sid string) (s *SMSResponse, err error) { 87 endpoint := fmt.Sprintf("%s/%s.%s", t.smsEndpoint(), sid, apiFormat) 88 89 b, status, err := t.request("GET", endpoint, url.Values{}) 90 if err != nil { 91 return 92 } 93 94 if status != http.StatusOK { 95 e := new(Exception) 96 err = json.Unmarshal(b, &e) 97 if err != nil { 98 return 99 } 100 101 return nil, e 102 } 103 104 err = json.Unmarshal(b, &s) 105 if err != nil { 106 return nil, err 107 } 108 109 return 110 } 111 112 // Returns a list of SMS messages associates with your account. It's support list filters via `map[string]string`: 113 // "To" : Only show SMS messages to this phone number 114 // "From" : Only show SMS messages from this phone number 115 // "DateSent" : Only show SMS messages sent on this date (in GMT format), given as `YYYY-MM-DD`. 116 func (t *Twilio) ListSMS(filters map[string]string) (sl *SMSListResponse, err error) { 117 endpoint := fmt.Sprintf("%s.%s", t.smsEndpoint(), apiFormat) 118 params := url.Values{} 119 120 for key, value := range filters { 121 params.Set(key, value) 122 } 123 124 b, status, err := t.request("GET", endpoint, params) 125 if err != nil { 126 return 127 } 128 129 if status != http.StatusOK { 130 e := new(Exception) 131 err = json.Unmarshal(b, &e) 132 if err != nil { 133 return 134 } 135 136 return nil, e 137 } 138 139 err = json.Unmarshal(b, &sl) 140 if err != nil { 141 return nil, err 142 } 143 144 return 145 }