Facebook Messenger 快速回复

Facebook Messenger Quick Replies

我正在为我的 NodeJS 应用程序 link 开发一个机器人,并使用快速回复来接收用户的电子邮件地址和电话号码。

但是,回复包含相同的文本和有效负载值,这使得无法捕获响应并对其进行处理。所以我一定是做错了什么。

这是我发送的内容:

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);

但是当用户点击他们的快速回复按钮时,我得到:

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

我如何识别要处理的特定快速回复响应? 通过 text 回复,我可以分配一个有效负载,然后监听返回的有效负载。

如果快速回复的负载是动态的,我看不到处理用户响应的方法,因为 if (response.message.quick_reply.payload === 'PHONE_NUMBER') 不能像脚本的其余部分那样在此处工作。

不幸的是,根据 docs,事实就是如此。

对于 email/phone 快速回复,message.quick_reply.payload 将是电子邮件或 phone 号码,视情况而定。

然而,虽然可以使用快速回复,但用户仍然可以手动输入不同的电子邮件或 phone 号码来代替他们在 Facebook 注册的内容 - 这只是为了方便。因为他们可以发回他们喜欢的任何自由格式文本,所以无论如何您都应该解析 message.text 属性。

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;
}