Skip to content

Commit

Permalink
Merge pull request #674 from nyaruka/tembachat_chat_started
Browse files Browse the repository at this point in the history
Add support for chat_started event on tembachat handler
  • Loading branch information
rowanseymour authored Jan 13, 2024
2 parents e80357e + 328bad4 commit e1151d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
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 @@ func (h *handler) Initialize(s courier.Server) error {
}

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
}
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

0 comments on commit e1151d6

Please sign in to comment.