facebook-messenger-bot

Facebook Messenger Quick Replies


I am developing a bot to link to my NodeJS application and am using quick replies to receive the user's email address and telephone number.

However, the reply contains a text and payload value that are the same, which makes catching the response and processing it impossible.. So I must be doing something wrong.

Here's what I send:

response = {
    "text": "We need your phone number to match you with our records",
    "quick_replies":[
        {
        "content_type":"user_phone_number",
        "payload":"PHONE_NUMBER"
        }
    ]
}
callSendAPI(sender_psid, response);

But when the user clicks their Quick Reply button I get:

{  sender: { id: '<some value>' },
   recipient: { id: '<some value>' },
   timestamp: 1622370102305,
   message:
    { mid:
       '<some value>',
      text: 'me@example.com',
      quick_reply: { payload: 'me@exmaple.com' }
    }
}

How would I identify a specific Quick Reply response for processing? With text replies I can assign a payload, then listen out for that payload being returned.

If the payload of a quick reply is dynamic, I don't see a way to process the user response since if (response.message.quick_reply.payload === 'PHONE_NUMBER') can't work here like the rest of the script.


Solution

  • Unfortunately, according to the docs, that's just how it is.

    For an email/phone quick reply, the message.quick_reply.payload will either be the email or phone number as appropriate.

    However, while the quick replies are available, the user can still manually type in a different email or phone number to what they have registered with Facebook - it's just for convenience. Because they can send back any free form text they like, you should be parsing the message.text property anyway.

    parseResponseForEmailAndPhone(response) {
      const text = response.message.text;
    
      if (looksLikeAnEmail(text)) {
        return { email: text };
      } else if (looksLikeAPhoneNumber(text)) {
        return { phone: text };
      }
    
      // TODO: handle other message
      // unlikely, but could even be a sentence:
      //  - "my phone is +000000"
      //  - "my email is me@example.com"
      //  - "+000000 me@example.com"
    
      // You also need to handle non-consent
      //  - "you don't need it"
      //  - "I don't have one"
      //  - "skip"
    
      const result = {};
    
      // please use a library for these instead,
      // they are used here just as an example
      const phoneMatches = /phoneRegEx/.exec(text); 
      const emailMatches = /emailRegEx/.exec(text);
      
      if (phoneMatches) {
        result.phone = phoneMatches[1];
      }
      if (emailMatches) {
        result.email = emailMatches[1];
      }
    
      return result;
    }