diff --git a/handlers/facebookapp/facebookapp.go b/handlers/facebookapp/facebookapp.go index 09686c217..0e40cefb9 100644 --- a/handlers/facebookapp/facebookapp.go +++ b/handlers/facebookapp/facebookapp.go @@ -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 { @@ -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")) } diff --git a/handlers/facebookapp/facebookapp_test.go b/handlers/facebookapp/facebookapp_test.go index 79e27f7e8..10c727716 100644 --- a/handlers/facebookapp/facebookapp_test.go +++ b/handlers/facebookapp/facebookapp_test.go @@ -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{ @@ -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) {