Skip to content

Commit b295a09

Browse files
authored
Merge pull request #2 from Geraev/master
Add func MessageStatus
2 parents f59e9c6 + 5a188bb commit b295a09

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

sms.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,37 @@ type SMSMessage struct {
104104
Validity int `json:"validity,omitempty"` // Duration WAP Push is available in milliseconds
105105
}
106106

107+
// MessageStatusResponse defines information about a single message that was sent using SMS API.
108+
// See https://developer.nexmo.com/api/developer/messages for details.
109+
type MessageStatusResponse struct {
110+
MessageId string `json:"message-id,omitempty"`
111+
AccountId string `json:"account-id,omitempty"`
112+
Network string `json:"network,omitempty"`
113+
From string `json:"from,omitempty"`
114+
To string `json:"to,omitempty"`
115+
Body string `json:"body,omitempty"`
116+
Price string `json:"price,omitempty"`
117+
DateReceived string `json:"date-received,omitempty"`
118+
Status string `json:"status,omitempty"`
119+
FinalStatus string `json:"final-status,omitempty"`
120+
DateClosed string `json:"date-closed,omitempty"`
121+
Latency int `json:"latency,omitempty"`
122+
Type string `json:"type,omitempty"`
123+
ClientRef string `json:"client-ref,omitempty"`
124+
ErrorCode string `json:"error-code,omitempty"`
125+
ErrorCodeLabel string `json:"error-code-label,omitempty"`
126+
RawBody []byte
127+
HttpStatusCode int
128+
}
129+
130+
// MessageStatusRequest request for search message status
131+
// See https://developer.nexmo.com/api/developer/messages for details.
132+
type MessageStatusRequest struct {
133+
apiKey string
134+
apiSecret string
135+
ID string `json:"id"`
136+
}
137+
107138
// A ResponseCode will be returned
108139
// whenever an SMSMessage is sent.
109140
type ResponseCode int
@@ -244,3 +275,55 @@ func (c *SMS) Send(msg *SMSMessage) (*MessageResponse, error) {
244275
}
245276
return messageResponse, nil
246277
}
278+
279+
// MessageStatus retrieves information about a single message that was sent using SMS API or that was received on your number.
280+
// See https://developer.nexmo.com/api/developer/messages for details.
281+
func (c *SMS) MessageStatus(msg *MessageStatusRequest) (*MessageStatusResponse, error) {
282+
if len(msg.ID) == 0 {
283+
return nil, errors.New("ID should not be empty")
284+
}
285+
286+
if !c.client.useOauth {
287+
msg.apiKey = c.client.apiKey
288+
msg.apiSecret = c.client.apiSecret
289+
}
290+
291+
var (
292+
r *http.Request
293+
err error
294+
)
295+
r, err = http.NewRequest("GET", apiRoot+"/search/message", nil)
296+
if err != nil {
297+
return nil, err
298+
}
299+
300+
q := r.URL.Query()
301+
q.Add("api_key", msg.apiKey)
302+
q.Add("api_secret", msg.apiSecret)
303+
q.Add("id", msg.ID)
304+
r.URL.RawQuery = q.Encode()
305+
306+
r.Header.Add("Accept", "application/json")
307+
308+
var resp *http.Response
309+
if resp, err = c.client.HTTPClient.Do(r); err != nil {
310+
return nil, err
311+
}
312+
defer resp.Body.Close()
313+
314+
var body []byte
315+
if body, err = ioutil.ReadAll(resp.Body); err != nil {
316+
return nil, err
317+
}
318+
319+
var messageStatusResponse = &MessageStatusResponse{
320+
RawBody: body,
321+
HttpStatusCode: resp.StatusCode,
322+
}
323+
324+
err = json.Unmarshal(body, &messageStatusResponse)
325+
if err != nil {
326+
return messageStatusResponse, err
327+
}
328+
return messageStatusResponse, nil
329+
}

0 commit comments

Comments
 (0)