Skip to content

Commit

Permalink
add messaging feedback receive event to facebookapp handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Nov 18, 2021
1 parent 258b740 commit 797061e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
36 changes: 36 additions & 0 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,26 @@ type moPayload struct {
MIDs []string `json:"mids"`
Watermark int64 `json:"watermark"`
} `json:"delivery"`

MessagingFeedback *struct {
FeedbackScreens []struct {
ScreenID int `json:"screen_id"`
Questions map[string]FeedbackQuestion `json:"questions"`
} `json:"feedback_screens"`
} `json:"messaging_feedback"`
} `json:"messaging"`
} `json:"entry"`
}

type FeedbackQuestion struct {
Type string `json:"type"`
Payload string `json:"payload"`
FollowUp *struct {
Type string `json:"type"`
Payload string `json:"payload"`
} `json:"follow_up"`
}

// GetChannel returns the channel
func (h *handler) GetChannel(ctx context.Context, r *http.Request) (courier.Channel, error) {
if r.Method == http.MethodGet {
Expand Down Expand Up @@ -425,6 +441,26 @@ func (h *handler) receiveEvent(ctx context.Context, channel courier.Channel, w h
data = append(data, courier.NewStatusData(event))
}

} else if msg.MessagingFeedback != nil {

payloads := []string{}
for _, v := range msg.MessagingFeedback.FeedbackScreens[0].Questions {
payloads = append(payloads, v.Payload)
}
text := strings.Join(payloads[:], ", ")

ev := h.Backend().NewIncomingMsg(channel, urn, text).WithReceivedOn(date)
event := h.Backend().CheckExternalIDSeen(ev)

err := h.Backend().WriteMsg(ctx, event)
if err != nil {
return nil, err
}

h.Backend().WriteExternalIDSeen(event)
events = append(events, event)
data = append(data, courier.NewMsgReceiveData(event))

} else {
data = append(data, courier.NewInfoData("ignoring unknown entry type"))
}
Expand Down
39 changes: 39 additions & 0 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,41 @@ var unkownMessagingEntry = `{
}]
}`

var customerFeedbackResponse = `{
"object": "page",
"entry": [
{
"id": "1234",
"time": 1459991487970,
"messaging": [
{
"recipient": {
"id": "1234"
},
"timestamp": 1459991487970,
"sender": {
"id": "5678"
},
"messaging_feedback": {
"feedback_screens": [
{
"screen_id": 0,
"questions": {
"test_question": {
"type": "CSAT",
"payload": "4"
}
}
}
]
}
}
]
}
]
}
`

var notJSON = `blargh`

var testCases = []ChannelHandleTestCase{
Expand Down Expand Up @@ -477,6 +512,10 @@ var testCases = []ChannelHandleTestCase{
{Label: "Unknown Messaging Entry", URL: "/c/fba/receive", Data: unkownMessagingEntry, Status: 200, Response: "Handled", PrepRequest: addValidSignature},
{Label: "Not JSON", URL: "/c/fba/receive", Data: notJSON, Status: 400, Response: "Error", PrepRequest: addValidSignature},
{Label: "Invalid URN", URL: "/c/fba/receive", Data: invalidURN, Status: 400, Response: "invalid facebook id", PrepRequest: addValidSignature},

{Label: "Receive Customer Feedback Message", URL: "/c/fba/receive", Data: customerFeedbackResponse, Status: 200, Response: "Handled", NoQueueErrorCheck: true, NoInvalidChannelCheck: true,
Text: Sp("4"), URN: Sp("facebook:5678"), Date: Tp(time.Date(2016, 4, 7, 1, 11, 27, 970000000, time.UTC)),
PrepRequest: addValidSignature},
}

func addValidSignature(r *http.Request) {
Expand Down

0 comments on commit 797061e

Please sign in to comment.