Skip to content

Commit

Permalink
add support to send customer feedback template from send message with…
Browse files Browse the repository at this point in the history
… a specific protocol
  • Loading branch information
rasoro committed Nov 22, 2021
1 parent d56e690 commit 79faf8f
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 79faf8f

Please sign in to comment.