Skip to content

Commit

Permalink
Properly send unicode text type to Nexmo
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 1, 2017
1 parent d27f822 commit 8c26be3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions handlers/nexmo/nexmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/url"
"strings"

"github.com/nyaruka/courier/gsm7"

"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
Expand Down Expand Up @@ -146,15 +148,22 @@ func (h *handler) SendMsg(msg courier.Msg) (courier.MsgStatus, error) {
callbackDomain := msg.Channel().CallbackDomain(h.Server().Config().Domain)
callbackURL := fmt.Sprintf("https://%s/c/nx/%s/status", callbackDomain, msg.Channel().UUID())

text := courier.GetTextAndAttachments(msg)

textType := "text"
if !gsm7.IsGSM7(text) {
textType = "unicode"
}

form := url.Values{
"api_key": []string{nexmoAPIKey},
"api_secret": []string{nexmoAPISecret},
"from": []string{strings.TrimPrefix(msg.Channel().Address(), "+")},
"to": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"text": []string{courier.GetTextAndAttachments(msg)},
"text": []string{text},
"status-report-req": []string{"1"},
"callback": []string{callbackURL},
"type": []string{"text"},
"type": []string{textType},
}

encodedForm := form.Encode()
Expand Down
24 changes: 24 additions & 0 deletions handlers/nexmo/nexmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ var defaultSendTestCases = []ChannelSendTestCase{
URLParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Unicode Send",
Text: "Unicode ☺", URN: "tel:+250788383383",
Status: "W", ExternalID: "1002",
URLParams: map[string]string{"text": "Unicode ☺", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "unicode"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Send Attachment",
Text: "My pic!", URN: "tel:+250788383383", Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
Status: "W", ExternalID: "1002",
URLParams: map[string]string{"text": "My pic!\nhttps://foo.bar/image.jpg", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `{"messages":[{"status":"0","message-id":"1002"}]}`, ResponseStatus: 200,
SendPrep: setSendURL},
{Label: "Error Sending",
Text: "Error Message", URN: "tel:+250788383383",
Status: "E",
URLParams: map[string]string{"text": "Error Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: `Error`, ResponseStatus: 400,
SendPrep: setSendURL},
{Label: "Invalid Token",
Text: "Simple Message", URN: "tel:+250788383383",
Status: "E",
URLParams: map[string]string{"text": "Simple Message", "to": "250788383383", "from": "2020", "api_key": "nexmo-api-key", "api_secret": "nexmo-api-secret", "status-report-req": "1", "type": "text"},
ResponseBody: "Invalid API token", ResponseStatus: 401,
SendPrep: setSendURL},
}

func TestSending(t *testing.T) {
Expand Down

0 comments on commit 8c26be3

Please sign in to comment.