Skip to content

Commit

Permalink
Merge pull request #696 from nyaruka/sr-sends
Browse files Browse the repository at this point in the history
RC, RR, SQ, SC, ST, SC to use new send
  • Loading branch information
rowanseymour authored Feb 21, 2024
2 parents 34cdc2f + c10e554 commit f23d1ac
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 375 deletions.
22 changes: 7 additions & 15 deletions handlers/redrabbit/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package redrabbit

import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -36,19 +35,13 @@ func (h *handler) Initialize(s courier.Server) error {
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
// TODO convert functionality from legacy method below
return nil
}

func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if username == "" || password == "" {
return nil, fmt.Errorf("Missing username or password for RR channel")
return courier.ErrChannelConfig
}

text := handlers.GetTextAndAttachments(msg)
status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)
form := url.Values{
"LoginName": []string{username},
"Password": []string{password},
Expand All @@ -73,16 +66,15 @@ func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *cour
msgURL.RawQuery = form.Encode()
req, err := http.NewRequest(http.MethodGet, msgURL.String(), nil)
if err != nil {
return nil, err
return err
}

resp, _, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 != 2 {
return status, nil
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}

// all went well, set ourselves to wired
status.SetStatus(courier.MsgStatusWired)

return status, nil
return nil
}
172 changes: 101 additions & 71 deletions handlers/redrabbit/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,130 @@
package redrabbit

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

"github.com/nyaruka/courier"
. "github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/httpx"
)

// setSendURL takes care of setting the send_url to our test server host
func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.MsgOut) {
sendURL = s.URL
}

var defaultSendTestCases = []OutgoingTestCase{
{Label: "Plain Send",
MsgText: "Simple Message", MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "Simple Message",
"SenderName": "2020",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"Simple Message"},
"SenderName": {"2020"},
}}},
},
{Label: "Unicode Send",
MsgText: "☺", MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "☺",
"SenderName": "2020",
"MsgTyp": "9",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"☺"},
"SenderName": {"2020"},
"MsgTyp": {"9"},
},
}},
},
{Label: "Longer Unicode Send",
MsgText: "This is a message more than seventy characters with some unicode ☺ in them",
MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "This is a message more than seventy characters with some unicode ☺ in them",
"SenderName": "2020",
"MsgTyp": "10",
MsgText: "This is a message more than seventy characters with some unicode ☺ in them",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"This is a message more than seventy characters with some unicode ☺ in them"},
"SenderName": {"2020"},
"MsgTyp": {"10"},
}}},
},
{Label: "Long Send",
MsgText: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
"SenderName": "2020",
"MsgTyp": "5",
MsgText: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work"},
"SenderName": {"2020"},
"MsgTyp": {"5"},
}}},
},
{Label: "Send Attachment",
MsgText: "My pic!", MsgURN: "tel:+250788383383", MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
ExpectedMsgStatus: "W",
MockResponseBody: "SENT", MockResponseStatus: 200,
ExpectedURLParams: map[string]string{
"LoginName": "Username",
"Password": "Password",
"Tracking": "1",
"Mobtyp": "1",
"MessageRecipients": "250788383383",
"MessageBody": "My pic!\nhttps://foo.bar/image.jpg",
"SenderName": "2020",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
},
SendPrep: setSendURL},
ExpectedRequests: []ExpectedRequest{{
Params: url.Values{
"LoginName": {"Username"},
"Password": {"Password"},
"Tracking": {"1"},
"Mobtyp": {"1"},
"MessageRecipients": {"250788383383"},
"MessageBody": {"My pic!\nhttps://foo.bar/image.jpg"},
"SenderName": {"2020"},
}}},
},
{Label: "Error Sending",
MsgText: "Error Sending", MsgURN: "tel:+250788383383",
ExpectedMsgStatus: "E",
MockResponseBody: "Error", MockResponseStatus: 403,
SendPrep: setSendURL},
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(403, nil, []byte(`Error`)),
},
},
ExpectedError: courier.ErrResponseStatus,
},
{Label: "Connection Error",
MsgText: "Error Sending", MsgURN: "tel:+250788383383",
MockResponses: map[string][]*httpx.MockResponse{
"http://http1.javna.com/epicenter/GatewaySendG.asp*": {
httpx.NewMockResponse(500, nil, []byte(`Error`)),
},
},
ExpectedError: courier.ErrConnectionFailed,
},
}

func TestOutgoing(t *testing.T) {
Expand Down
44 changes: 20 additions & 24 deletions handlers/rocketchat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *handler) Initialize(s courier.Server) error {
return nil
}

type Attachment struct {
type RCAttachment struct {
Type string `json:"type"`
URL string `json:"url"`
}
Expand All @@ -52,8 +52,8 @@ type moPayload struct {
Username string `json:"username"`
FullName string `json:"full_name"`
} `json:"user" validate:"required"`
Text string `json:"text"`
Attachments []Attachment `json:"attachments"`
Text string `json:"text"`
Attachments []RCAttachment `json:"attachments"`
}

// receiveMessage is our HTTP handler function for incoming messages
Expand Down Expand Up @@ -99,54 +99,50 @@ func (h *handler) BuildAttachmentRequest(ctx context.Context, b courier.Backend,
var _ courier.AttachmentRequestBuilder = (*handler)(nil)

type mtPayload struct {
UserURN string `json:"user"`
BotUsername string `json:"bot"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
UserURN string `json:"user"`
BotUsername string `json:"bot"`
Text string `json:"text,omitempty"`
Attachments []RCAttachment `json:"attachments,omitempty"`
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
// TODO convert functionality from legacy method below
return nil
}

func (h *handler) SendLegacy(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
baseURL := msg.Channel().StringConfigForKey(configBaseURL, "")
secret := msg.Channel().StringConfigForKey(configSecret, "")
botUsername := msg.Channel().StringConfigForKey(configBotUsername, "")

// the status that will be written for this message
status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)

if baseURL == "" || secret == "" || botUsername == "" {
return courier.ErrChannelConfig
}
payload := &mtPayload{
UserURN: msg.URN().Path(),
BotUsername: botUsername,
Text: msg.Text(),
}
for _, attachment := range msg.Attachments() {
mimeType, url := handlers.SplitAttachment(attachment)
payload.Attachments = append(payload.Attachments, Attachment{mimeType, url})
payload.Attachments = append(payload.Attachments, RCAttachment{mimeType, url})
}

body := jsonx.MustMarshal(payload)

req, err := http.NewRequest(http.MethodPost, baseURL+"/message", bytes.NewReader(body))
if err != nil {
return status, err
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Token %s", secret))

resp, respBody, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 != 2 {
return status, nil
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}

msgID, err := jsonparser.GetString(respBody, "id")
if err == nil {
status.SetExternalID(msgID)
if err != nil {
return courier.ErrResponseUnexpected
}
res.AddExternalID(msgID)

status.SetStatus(courier.MsgStatusSent)
return status, nil
return nil
}
Loading

0 comments on commit f23d1ac

Please sign in to comment.