From ebde2a093242729dec1a168f4d12723f39fab26c Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Fri, 19 Nov 2021 19:11:22 -0300 Subject: [PATCH] add support to send customer feedback template from send message with a specific protocol --- handlers/facebookapp/facebookapp.go | 74 +++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/handlers/facebookapp/facebookapp.go b/handlers/facebookapp/facebookapp.go index 0e40cefb9..0eddd8686 100644 --- a/handlers/facebookapp/facebookapp.go +++ b/handlers/facebookapp/facebookapp.go @@ -547,6 +547,80 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored) + isCustomerFeedbackTemplateMsg := strings.Contains(msg.Text(), "{customer_feedback_template}") + + if isCustomerFeedbackTemplateMsg { + if msg.Text() != "" { + text := strings.ReplaceAll(strings.ReplaceAll(msg.Text(), "\n", ""), "\t", "") + + splited := strings.Split(text, "|") + templateMap := make(map[string]string) + for i := 1; i < len(splited); i++ { + field := strings.Split(splited[i], ":") + templateMap[strings.TrimSpace(field[0])] = strings.TrimSpace(field[1]) + } + + payloadMap := map[string]interface{}{ + "recipient": map[string]string{ + "id": msg.URN().Path(), + }, + "message": map[string]interface{}{ + "attachment": map[string]interface{}{ + "type": "template", + "payload": map[string]interface{}{ + "template_type": "customer_feedback", + "title": templateMap["title"], + "subtitle": templateMap["subtitle"], + "button_title": templateMap["button_title"], + "feedback_screens": []map[string]interface{}{ + { + "questions": []map[string]interface{}{ + { + "id": templateMap["question_id"], + "type": templateMap["type"], + "title": templateMap["question_title"], + "score_label": templateMap["score_label"], + "score_option": templateMap["score_option"], + }, + }, + }, + }, + "business_privacy": map[string]string{ + "url": templateMap["business_privacy"], + }, + }, + }, + }, + } + + jsonBody, err := json.Marshal(payloadMap) + if err != nil { + return status, err + } + + msgURL, _ := url.Parse("https://graph.facebook.com/v12.0/me/messages") + query := url.Values{} + query.Set("access_token", accessToken) + msgURL.RawQuery = query.Encode() + + req, err := http.NewRequest(http.MethodPost, msgURL.String(), bytes.NewReader(jsonBody)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + rr, err := utils.MakeHTTPRequest(req) + + log := courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr).WithError("Message Send Error", err) + status.AddLog(log) + if err != nil { + return status, nil + } + status.SetStatus(courier.MsgWired) + } + return status, nil + } + msgParts := make([]string, 0) if msg.Text() != "" { msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)