Skip to content

Commit

Permalink
Adjust weniwebchat handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Dec 19, 2024
1 parent 7c24c1c commit fa4c2d8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 81 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,3 @@ require (
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

//replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.42.7-weni
10 changes: 5 additions & 5 deletions handlers/weniwebchat/weniwebchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type handler struct {
}

func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("WWC"), "Weni Web Chat", nil)}
return &handler{handlers.NewBaseHandler(courier.ChannelType("WWC"), "Weni Web Chat")}
}

// Initialize is called by the engine once everything is loaded
Expand Down Expand Up @@ -70,7 +70,7 @@ func (h *handler) receiveEvent(ctx context.Context, channel courier.Channel, w h
}

// build urn
urn, err := urns.NewURNFromParts(urns.ExternalScheme, payload.From, "", "")
urn, err := urns.NewFromParts(urns.External.Prefix, payload.From, nil, "")
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
Expand Down Expand Up @@ -120,12 +120,12 @@ type moMessage struct {
QuickReplies []string `json:"quick_replies,omitempty"`
}

func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.ChannelLog) (courier.StatusUpdate, error) {
func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusSent, clog)

baseURL := msg.Channel().StringConfigForKey(courier.ConfigBaseURL, "")
if baseURL == "" {
return status, errors.New("blank base_url")
return errors.New("blank base_url")
}

sendURL := fmt.Sprintf("%s/send", baseURL)
Expand Down Expand Up @@ -219,7 +219,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch

}

return status, nil
return nil
}

func newOutgoingMessage(payType, to, from string, quickReplies []string) *moPayload {
Expand Down
135 changes: 61 additions & 74 deletions handlers/weniwebchat/weniwebchat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package weniwebchat

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

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

const channelUUID = "8eb23e93-5ecb-45ba-b726-3b064e0c568c"

var testChannels = []courier.Channel{
test.NewMockChannel(channelUUID, "WWC", "250788383383", "", map[string]interface{}{}),
test.NewMockChannel(channelUUID, "WWC", "250788383383", "", []string{}, map[string]any{}),
}

// ReceiveMsg test
Expand Down Expand Up @@ -166,90 +167,76 @@ func mockAttachmentURLs(mediaServer *httptest.Server, testCases []OutgoingTestCa

var sendTestCases = []OutgoingTestCase{
{
Label: "Plain Send",
MsgText: "Simple Message",
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusSent,
ExpectedRequestPath: "/send",
ExpectedHeaders: map[string]string{"Content-type": "application/json"},
ExpectedRequestBody: `{"type":"message","to":"371298371241","from":"250788383383","message":{"type":"text","timestamp":"1616700878","text":"Simple Message"}}`,
MockResponseStatus: 200,
SendPrep: setSendURL,
},
{
Label: "Unicode Send",
MsgText: "☺",
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusSent,
ExpectedRequestPath: "/send",
ExpectedHeaders: map[string]string{"Content-type": "application/json"},
ExpectedRequestBody: `{"type":"message","to":"371298371241","from":"250788383383","message":{"type":"text","timestamp":"1616700878","text":"☺"}}`,
MockResponseStatus: 200,
SendPrep: setSendURL,
},
{
Label: "invalid Text Send",
MsgText: "Error",
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusFailed,
ExpectedRequestPath: "/send",
ExpectedHeaders: map[string]string{"Content-type": "application/json"},
ExpectedRequestBody: `{"type":"message","to":"371298371241","from":"250788383383","message":{"type":"text","timestamp":"1616700878","text":"Error"}}`,
SendPrep: setSendURL,
},
{
Label: "Medias Send",
MsgText: "Medias",
MsgAttachments: []string{
"audio/mp3:https://foo.bar/audio.mp3",
"application/pdf:https://foo.bar/file.pdf",
"image/jpg:https://foo.bar/image.jpg",
"video/mp4:https://foo.bar/video.mp4",
Label: "Plain Send",
MsgText: "Simple Message",
MsgURN: "ext:371298371241",
MockResponses: map[string][]*httpx.MockResponse{
"*/send": {
httpx.NewMockResponse(200, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Body: `{"type":"message","to":"371298371241","from":"250788383383","message":{"type":"text","timestamp":"1616700878","text":"Simple Message"}}`,
Headers: map[string]string{"Content-type": "application/json"},
},
},
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusSent,
MockResponseStatus: 200,
SendPrep: setSendURL,
},
{
Label: "Invalid Media Type Send",
MsgText: "Medias",
MsgAttachments: []string{"foo/bar:https://foo.bar/foo.bar"},
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusFailed,
MockResponseStatus: 400,
SendPrep: setSendURL,
Label: "Unicode Send",
MsgText: "☺",
MsgURN: "ext:371298371241",
MockResponses: map[string][]*httpx.MockResponse{
"*/send": {
httpx.NewMockResponse(200, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Body: `{"type":"message","to":"371298371241","from":"250788383383","message":{"type":"text","timestamp":"1616700878","text":"☺"}}`,
Headers: map[string]string{"Content-type": "application/json"},
},
},
},
{
Label: "Invalid Media Send",
MsgText: "Medias",
MsgAttachments: []string{"image/png:https://foo.bar/image.png"},
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusFailed,
SendPrep: setSendURL,
Label: "invalid Text Send",
MsgText: "Error",
MsgURN: "ext:371298371241",
MockResponses: map[string][]*httpx.MockResponse{
"*/send": {
httpx.NewMockResponse(400, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Body: `{"type":"message","to":"371298371241","from":"250788383383","message":{"type":"text","timestamp":"1616700878","text":"Error"}}`,
Headers: map[string]string{"Content-type": "application/json"},
},
},
},
{
Label: "No Timestamp Prepare",
MsgText: "No prepare",
MsgURN: "ext:371298371241",
ExpectedMsgStatus: courier.MsgStatusSent,
MockResponseStatus: 200,
SendPrep: func(s *httptest.Server, h courier.ChannelHandler, c courier.Channel, m courier.MsgOut) {
c.(*test.MockChannel).SetConfig(courier.ConfigBaseURL, s.URL)
timestamp = ""
Label: "No Timestamp Prepare",
MsgText: "No prepare",
MsgURN: "ext:371298371241",
MockResponses: map[string][]*httpx.MockResponse{
"*/send": {
httpx.NewMockResponse(200, nil, []byte(``)),
},
},
ExpectedRequests: []ExpectedRequest{
{
Body: ``,
},
},
},
}

func TestOutgoing(t *testing.T) {
mediaServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
res.WriteHeader(200)
var defaultChannel = []courier.Channel{
test.NewMockChannel(channelUUID, "WWC", "250788383383", "", []string{urns.External.Prefix}, map[string]any{courier.ConfigBaseURL: "https://foo.bar"}),
}

res.Write([]byte("media bytes"))
}))
mockedSendTestCases := mockAttachmentURLs(mediaServer, sendTestCases)
mediaServer.Close()
timestamp = "1616700878"

RunOutgoingTestCases(t, testChannels[0], newHandler(), mockedSendTestCases, nil, nil)
RunOutgoingTestCases(t, defaultChannel[0], newHandler(), sendTestCases, nil, nil)
}

0 comments on commit fa4c2d8

Please sign in to comment.