creating a link that will open facebook messenger and send a message

Adam Itshar picture Adam Itshar · Mar 8, 2017 · Viewed 26.3k times · Source

i'm creating a registration code for users to a facebook messenger bot i am writing. instead of asking them to open a chat with my bot, and type in the registration code, i would like to provide them a link that will open their facebook messenger on a chat window with my bot, and send the registration code automatically.

i saw a link that opens the chat window (https://m.facebook.com/messages/compose?ids=USER_ID) but can't get it to send a message too.

if you know how to do that, or have an alternative suggestion, i would appreciate the help.

Answer

Oranagwa Osmond Oscar picture Oranagwa Osmond Oscar · Mar 20, 2017

Check out the new referral webhook-reference. It gives you a way to link users directly to your bot with support for passing arbitrary parameters via the link.

So a possible approach would be:

  1. Make sure your bot is subscribed to the messaging_referral event.
  2. Craft your bot's m.me link to include the registration code in the ref param e.g http://m.me/mybot?ref=REGISTRATION_CODE. if you're interested in security you might consider encrypting the code
  3. Send the link(s) to the user. When the user click's the link, they're directed to your bot on messenger and once they initiate a session with your bot, you'll receive a messaging_referral event at your webhook with a payload similar to

{
  "sender":{
    "id":"USER_ID"
  },
  "recipient":{
    "id":"PAGE_ID"
  },
  "timestamp":1458692752478,
  "referral": {
    "ref": "REGISTRATION_CODE",
    "source": "SHORTLINK",
    "type": "OPEN_THREAD",
  }
}

  1. You can then evaluate the ref value and send your desired feedback to the user.