Skip to content

Commit

Permalink
Merge e2f3f42 into b77a70e
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 10, 2018
2 parents b77a70e + e2f3f42 commit 76017a2
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
_ "github.com/nyaruka/courier/handlers/kannel"
_ "github.com/nyaruka/courier/handlers/nexmo"
_ "github.com/nyaruka/courier/handlers/shaqodoon"
_ "github.com/nyaruka/courier/handlers/start"
_ "github.com/nyaruka/courier/handlers/telegram"
_ "github.com/nyaruka/courier/handlers/twilio"
_ "github.com/nyaruka/courier/handlers/whatsapp"
Expand Down
25 changes: 25 additions & 0 deletions handlers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -152,6 +153,30 @@ func DecodeAndValidateJSON(envelope interface{}, r *http.Request) error {
return nil
}

// DecodeAndValidateXML takes the passed in envelope and tries to unmarshal it from the body
// of the passed in request, then validating it
func DecodeAndValidateXML(envelope interface{}, r *http.Request) error {
// read our body
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 100000))
defer r.Body.Close()
if err != nil {
return fmt.Errorf("unable to read request body: %s", err)
}

// try to decode our envelope
if err = xml.Unmarshal(body, envelope); err != nil {
return fmt.Errorf("unable to parse request XML: %s", err)
}

// check our input is valid
err = validate.Struct(envelope)
if err != nil {
return fmt.Errorf("request XML doesn't match required schema: %s", err)
}

return nil
}

/*
DecodePossibleBase64 detects and decodes a possibly base64 encoded messages by doing:
* check it's at least 60 characters
Expand Down
169 changes: 169 additions & 0 deletions handlers/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,172 @@ package start
POST /handlers/start/receive/uuid/
<message><service type='sms' timestamp='1493792274' auth='1auth42d6e1aa608b6038' request_id='40599627'/><from>380975831111</from><to>4224</to><body>Msg</body></message>
*/

import (
"bytes"
"github.com/nyaruka/courier/utils"
"strconv"
"time"
"context"
"encoding/xml"
"fmt"
"net/http"

"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/urns"
)

var sendURL = "http://bulk.startmobile.com.ua/clients.php"

func init() {
courier.RegisterHandler(NewHandler())
}

type handler struct {
handlers.BaseHandler
}

// NewHandler returns a new Zenvia handler
func NewHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("ST"), "Start Mobile")}
}

// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
s.AddHandlerRoute(h, "POST", "receive", h.ReceiveMessage)
return nil
}

type moMessage struct {
XMLName xml.Name `xml:"message"`
Service struct {
Timestamp string `xml:"timestamp,attr"`
RequestID string `xml:"request_id,attr"`
} `xml:"service"`
From string `xml:"from"`
To string `xml:"to"`
Body struct {
Text string `xml:",chardata"`
} `xml:"body"`
}


// ReceiveMessage is our HTTP handler function for incoming messages
func (h *handler) ReceiveMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
mo := &moMessage{}
err := handlers.DecodeAndValidateXML(mo, r)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, err)
}

if mo.Service.RequestID == "" || mo.From == "" {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, fmt.Errorf("missing parameters, must have 'request_id', 'to' and 'body'"))
}

// create our URN
urn := urns.NewTelURNForCountry(mo.From, channel.Country())
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, err)
}

// create our date from the timestamp
ts, err := strconv.ParseInt(mo.Service.Timestamp, 10, 64)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, fmt.Errorf("invalid timestamp: %s", mo.Service.Timestamp))
}
date := time.Unix(ts, 0).UTC()

// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, mo.Body.Text).WithReceivedOn(date)

// and write it
err = h.Backend().WriteMsg(ctx, msg)
if err != nil {
return nil, err
}

return []courier.Event{msg}, courier.WriteMsgSuccess(ctx, w, r, []courier.Msg{msg})
}

type body struct {
ContentType string `xml:"content-type,attr"`
Encoding string `xml:"encoding,attr"`
Text string `xml:",chardata"`
}

type service struct {
ID string `xml:"id,attr"`
Source string `xml:"source,attr"`
Validity string `xml:"validity,attr"`
}

type mtMessage struct {
XMLName xml.Name `xml:"message"`
Service service `xml:"service"`
To string `xml:"to"`
Body body `xml:"body"`
}

type stResponse struct {
XMLName xml.Name `xml:"status"`
ID string `xml:"id"`
State string `xml:"state"`
}

func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
if username == "" {
return nil, fmt.Errorf("no username set for IB channel")
}

password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if password == "" {
return nil, fmt.Errorf("no password set for IB channel")
}

stMsg := mtMessage{
Service: service{
ID: "single",
Source: msg.Channel().Address(),
Validity: "+12 hours",
},
To: msg.URN().Path(),
Body : body{
ContentType: "plain/text",
Encoding: "plain",
Text: courier.GetTextAndAttachments(msg),
},
}

requestBody := &bytes.Buffer{}
err := xml.NewEncoder(requestBody).Encode(stMsg)
if err != nil {
return nil, err
}

// build our request
req, err := http.NewRequest(http.MethodPost, sendURL, requestBody)
req.Header.Set("Content-Type", "application/xml; charset=utf8")
req.SetBasicAuth(username, password)
rr, err := utils.MakeHTTPRequest(req)

// record our status and log
status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored)
log := courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr)
status.AddLog(log)
if err != nil {
log.WithError("Message Send Error", err)
return status, nil
}

stResponse := &stResponse{}
err = xml.Unmarshal([]byte(rr.Body), stResponse)
if err == nil {
status.SetStatus(courier.MsgWired)
status.SetExternalID(stResponse.ID)
}

return status, nil
}
135 changes: 135 additions & 0 deletions handlers/start/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package start

import (
"net/http/httptest"
"time"
"testing"

"github.com/nyaruka/courier"
. "github.com/nyaruka/courier/handlers"

)

var testChannels = []courier.Channel{
courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "ST", "2020", "UA", map[string]interface{}{"username": "st-username", "password": "st-password"}),
}

var (
receiveURL = "/c/st/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive/"

notXML = "empty"

validReceive = `<message>
<service type="sms" timestamp="1450450974" auth="asdfasdf" request_id="msg1"/>
<from>+250788123123</from>
<to>1515</to>
<body content-type="content-type" encoding="utf8">Hello World</body>
</message>`

validReceiveEmptyText = `<message>
<service type="sms" timestamp="1450450974" auth="asdfasdf" request_id="msg1"/>
<from>+250788123123</from>
<to>1515</to>
<body content-type="content-type" encoding="utf8"></body>
</message>`

missingRequestID = `<message>
<service type="sms" timestamp="1450450974" auth="asdfasdf" />
<from>+250788123123</from>
<to>1515</to>
<body content-type="content-type" encoding="utf8">Hello World</body>
</message>`

missingFrom = `<message>
<service type="sms" timestamp="1450450974" auth="asdfasdf" request_id="msg1"/>
<to>1515</to>
<body content-type="content-type" encoding="utf8">Hello World</body>
</message>`

)

var testCases = []ChannelHandleTestCase{
{Label: "Receive Valid", URL: receiveURL, Data: validReceive, Status: 200, Response: "Message Accepted",
Text: Sp("Hello World"), URN: Sp("tel:+250788123123"), Date: Tp(time.Date(2015, 12, 18, 15, 02, 54, 0, time.UTC))},
{Label: "Receive Valid with empty Text", URL: receiveURL, Data: validReceiveEmptyText, Status: 200, Response: "Message Accepted",
Text: Sp(""), URN: Sp("tel:+250788123123")},

{Label: "Receive missing Request ID", URL: receiveURL, Data: missingRequestID, Status: 400, Response: "Error"},
{Label: "Receive missing From", URL: receiveURL, Data: missingFrom, Status: 400, Response: "Error"},
{Label: "Invalid XML", URL: receiveURL, Data: notXML, Status: 400, Response: "Error"},

}

func TestHandler(t *testing.T) {
RunChannelTestCases(t, testChannels, NewHandler(), testCases)
}

func BenchmarkHandler(b *testing.B) {
RunChannelBenchmarks(b, testChannels, NewHandler(), testCases)
}

// setSendURL takes care of setting the sendURL to call
func setSendURL(server *httptest.Server, channel courier.Channel, msg courier.Msg) {
sendURL = server.URL
}

var defaultSendTestCases = []ChannelSendTestCase{
{Label: "Plain Send",
Text: "Simple Message ☺",
URN: "tel:+250788383383",
Status: "W",
ExternalID: "380502535130309161501",
ResponseBody: `<status date='Wed, 25 May 2016 17:29:56 +0300'><id>380502535130309161501</id><state>Accepted</state></status>`,
ResponseStatus: 200,
Headers: map[string]string{
"Content-Type": "application/xml; charset=utf8",
"Authorization": "Basic VXNlcm5hbWU6UGFzc3dvcmQ=",
},
RequestBody: `<message><service id="single" source="2020" validity="+12 hours"></service><to>+250788383383</to><body content-type="plain/text" encoding="plain">Simple Message ☺</body></message>`,
SendPrep: setSendURL},
{Label: "Send Attachment",
Text: "My pic!",
Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
URN: "tel:+250788383383",
Status: "W",
ExternalID: "380502535130309161501",
ResponseBody: `<status date='Wed, 25 May 2016 17:29:56 +0300'><id>380502535130309161501</id><state>Accepted</state></status>`,
ResponseStatus: 200,
Headers: map[string]string{
"Content-Type": "application/xml; charset=utf8",
"Authorization": "Basic VXNlcm5hbWU6UGFzc3dvcmQ=",
},
RequestBody: `<message><service id="single" source="2020" validity="+12 hours"></service><to>+250788383383</to><body content-type="plain/text" encoding="plain">My pic!&#xA;https://foo.bar/image.jpg</body></message>`,
SendPrep: setSendURL},
{Label: "Error Response",
Text: "Simple Message ☺",
URN: "tel:+250788383383",
Status: "E",
ExternalID: "",
ResponseBody: `<error>This is an error</error>`,
ResponseStatus: 200,
Headers: map[string]string{
"Content-Type": "application/xml; charset=utf8",
"Authorization": "Basic VXNlcm5hbWU6UGFzc3dvcmQ=",
},
RequestBody: `<message><service id="single" source="2020" validity="+12 hours"></service><to>+250788383383</to><body content-type="plain/text" encoding="plain">Simple Message ☺</body></message>`,
SendPrep: setSendURL},
{Label: "Error Sending",
Text: "Error Message", URN: "tel:+250788383383",
Status: "E",
ResponseBody: `Error`, ResponseStatus: 401,
Headers: map[string]string{
"Content-Type": "application/xml; charset=utf8",
"Authorization": "Basic VXNlcm5hbWU6UGFzc3dvcmQ=",
},
RequestBody: `<message><service id="single" source="2020" validity="+12 hours"></service><to>+250788383383</to><body content-type="plain/text" encoding="plain">Error Message</body></message>`,
SendPrep: setSendURL},


}

func TestSending(t *testing.T) {
var defaultChannel = courier.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "ST", "2020", "UA", map[string]interface{}{"username": "Username", "password": "Password"})
RunChannelSendTestCases(t, defaultChannel, NewHandler(), defaultSendTestCases)
}

0 comments on commit 76017a2

Please sign in to comment.