Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for chat_started event on tembachat handler #674

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions handlers/tembachat/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,38 @@
}

type receivePayload struct {
Type string `json:"type" validate:"required"`
Message struct {
Identifier string `json:"identifier" validate:"required"`
Text string `json:"text" validate:"required"`
} `json:"message"`
Type string `json:"type" validate:"required"`
Msg struct {
Identifier string `json:"identifier"`
Text string `json:"text"`
} `json:"msg"`
Chat struct {
Identifier string `json:"identifier"`
} `json:"chat"`
}

// receiveMessage is our HTTP handler function for incoming messages
func (h *handler) receiveMessage(ctx context.Context, c courier.Channel, w http.ResponseWriter, r *http.Request, payload *receivePayload, clog *courier.ChannelLog) ([]courier.Event, error) {
if payload.Type == "message" {
urn, err := urns.NewWebChatURN(payload.Message.Identifier)
if payload.Type == "msg_in" {
urn, err := urns.NewWebChatURN(payload.Msg.Identifier)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, c, w, r, err)
}

msg := h.Backend().NewIncomingMsg(c, urn, payload.Message.Text, "", clog)
msg := h.Backend().NewIncomingMsg(c, urn, payload.Msg.Text, "", clog)
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
} else if payload.Type == "chat_started" {
urn, err := urns.NewWebChatURN(payload.Chat.Identifier)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, c, w, r, err)
}

evt := h.Backend().NewChannelEvent(c, courier.EventTypeNewConversation, urn, clog)
err = h.Backend().WriteChannelEvent(ctx, evt, clog)
if err != nil {
return nil, err
}

Check warning on line 68 in handlers/tembachat/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/tembachat/handler.go#L67-L68

Added lines #L67 - L68 were not covered by tests
return []courier.Event{evt}, courier.WriteChannelEventSuccess(w, evt)
}
return nil, handlers.WriteAndLogRequestIgnored(ctx, h, c, w, r, "")
}
Expand Down
25 changes: 20 additions & 5 deletions handlers/tembachat/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,40 @@ var testChannels = []courier.Channel{

var handleTestCases = []IncomingTestCase{
{
Label: "Receive Valid Message",
Label: "Message with text",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"type": "message", "message": {"identifier": "65vbbDAQCdPdEWlEhDGy4utO", "text": "Join"}}`,
Data: `{"type": "msg_in", "msg": {"identifier": "65vbbDAQCdPdEWlEhDGy4utO", "text": "Join"}}`,
ExpectedRespStatus: 200,
ExpectedBodyContains: "Accepted",
ExpectedMsgText: Sp("Join"),
ExpectedURN: "webchat:65vbbDAQCdPdEWlEhDGy4utO",
},
{
Label: "Invalid URN",
Label: "Message with invalid URN",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"type": "message", "message": {"identifier": "xxxxx", "text": "Join"}}`,
Data: `{"type": "msg_in", "msg": {"identifier": "xxxxx", "text": "Join"}}`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "invalid webchat id: xxxxx",
},
{
Label: "Chat started event",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"type": "chat_started", "chat": {"identifier": "65vbbDAQCdPdEWlEhDGy4utO"}}`,
ExpectedRespStatus: 200,
ExpectedBodyContains: "Accepted",
ExpectedEvents: []ExpectedEvent{{Type: courier.EventTypeNewConversation, URN: "webchat:65vbbDAQCdPdEWlEhDGy4utO"}},
},
{
Label: "Chat started event with invalid URN",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"type": "chat_started", "chat": {"identifier": "xxxxx"}}`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "invalid webchat id: xxxxx",
},
{
Label: "Missing fields",
URL: "/c/twc/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive",
Data: `{"foo": "message"}`,
Data: `{"foo": "bar"}`,
ExpectedRespStatus: 400,
ExpectedBodyContains: "Field validation for 'Type' failed on the 'required' tag",
},
Expand Down
Loading