Trigger Botframework v4 on Facebook Messenger from an external website call - facebook

i am trying to open a specific messenger page and trigger a dialog to display carousels based on the parameters that my users entered on my website.
i am leveraging the m.me link to open my messenger page (where my botframeowrk bot is deployed). After opening the page I am not able to invoke a dialog.
How can I achieve this ???
I went through the Ms Botframework V4 docs and didn't find any mention of channel specific trigger.I want to pass 4 parameters to my Bot from the website and then further make an api call in the dialog that i will invoke to display results.
Thanks in Advance.

You need to enable Messaging Referrals in the Facebook Developer portal to use m.me links. In the portal, click on the settings blade on the left, scroll down to webhooks, and click edit events. A window should pop up where you can enable messaging_referrals.
Facebook only lets you add the ref parameter to the m.me link, so to send additional data to the bot using this link, you will have to encode the values in the ref parameter and parse the data on the bot side - I would recommend using a separator value like a comma or a slash to organize your data.
https://m.me/<PAGE_NAME>?ref=my,additional,data
The event will be send to the bot as a message activity with a referral attribute where you can grab your data.
m.me link bot activity
{
"type": "message",
"id": "CDbQi3u62J7",
"timestamp": "2019-03-19T19:30:11.219Z",
"serviceUrl": "https://facebook.botframework.com/",
"channelId": "facebook",
"from": {
"id": "2031650190235097",
"name": "TJ Durnford"
},
"conversation": {
"isGroup": false,
"id": "2031650190235097-218838049015546"
},
"recipient": {
"id": "218838049015546",
"name": "thdurn-all-channels"
},
"channelData": {
"sender": {
"id": "2031650190235097"
},
"recipient": {
"id": "218838049015546"
},
"timestamp": 1553023811219,
"referral": {
"ref": "my,additional,data",
"source": "SHORTLINK",
"type": "OPEN_THREAD"
}
}
Hope this helps!

Related

Does Facebook have a webhook that an app can subscribe to to detect when a Facebook event is added to a page?

Does Facebook have a webhook that an app can subscribe to to detect when a Facebook event is added to a page? Or do apps that are interested in this information just have to repeatedly poll /[page-id]?fields=events ?
For example if you have a page that creates some event, as long as you subscribe to feeds, Facebook Realtime API will send you json payload that looks similar to this:
{
"entry": [
{
"changes": [
{
"field": "feed",
"value": {
"story": "Some page added an event.",
"item": "event",
"event_id": "2277580782468248",
"post_id": "1442694315844896_2277580782468248",
"verb": "add",
"created_time": 1511651353,
"message": "test event"
}
}
],
"id": "1442694315844895",
"time": 1511651355
}
],
"object": "page"
}
Then you probably would like to pull the Graph API for additional informations about your event_id
The product you are trying to find is Facebook Webhooks https://developers.facebook.com/docs/graph-api/webhooks/. It allows you to subscribe to changes on the Facebook page and get alert only when change occures. You don't need to have regular calls with Graph API to see whether there are changes.
First you need to create facebook app, and then to add webhooks on that Facebook app. Next step is to subscribe that webhook to page you want and select "feed" as subscription. You will be informed about any change on your page.

How to deploy rich formatted link for facebook messenger bot in api.ai?

In api.ai,
Rich formatting is supported for skype pretty fine. Here's an example:-
Here's the code of custom payload of api.ai:-
{
"skype": {
"text": "OCAS means Online Credit Approval System.\n Click [here] (http://www.erainfotechbd.com/product/ocas-online-credit-approval-system/) for more information. ",
"attachments": [
{
"contentType": "image/png",
"contentUrl": "http://weknowyourdreams.com/images/smile/smile-07.jpg",
"name": "Profile-picture.png"
}
]
}
}
Now, I wanna do something like this in facebook messenger also. But in the api.ai doc, only audio, video and file is supported.
Is there any other way to do so for url also in messenger ?
Generally webhook responses can deliver any custom payload to most platforms that API.AI supports, so any feature supported by Facebook Messenger, Skype, Google Assistant, etc. can be utilized through API.AI. Unfortunately Facebook doesn't seem to support hyperlinked text. The closest analog I could find is a what Facebook calls a URL Button. Below is an example of how you could create a API.AI response that has a Facebook URL Button with Facebook's Button template:
{
"speech": "OCAS means Online Credit Approval System. Check your phone for more information.",
"displayText": "OCAS means Online Credit Approval System. Click here for more information: http://www.erainfotechbd.com/product/ocas-online-credit-approval-system/",
"data": {
"facebook": {
"attachment": {
"type": "template",
"payload": {
"template_type": "button",
"text": "OCAS means Online Credit Approval System",
"buttons": [
{
"type": "web_url",
"url": "http://www.erainfotechbd.com/product/ocas-online-credit-approval-system/",
"title": "Click here for more information"
}
]
}
}
}
}
}
You can use also use any other Facebook Messenger APIs by including any JSON inside the "message" attribute that you see from Facebook's Messenger documentation and it will be passed along to Facebook by API.AI
PS - the code above should produce a message that looks something like this:

Facebook: Link Request's sender to it's recipient?

When using the requests dialog, facebook issues a notification like this:
What I'm looking to achieve is for the user to click the request link shown above and to present a "Random User invited you to..." message on our App.
However, when clicking that link Facebook doesn't seem to pass through the id of "Random User" to the App. The url accessed by the link looks something like:
http://apps.facebook.com/randomcomp/?fb_source=notification&request_ids=350578327437399,350578327437399&ref=notif&app_request_type=user_to_user&notif_t=app_request
which doesn't contain any reference to the user who initiated the request.
From the App's side, there doesn't seem to be a way to get this information from Facebook. Sure, you can get a list of requests, but that list can contain information for many requests, including requests from other users in addition to the one we're interested in, so it's not useful in this case. For example, here's a snapshot of data:
{
"data": [
{
"id": "340083146057323_100003817986566",
"application": {
"name": "Random Competition",
"namespace": "randomcomp",
"id": "350578327437399"
},
"to": {
"name": "Hannah Smith",
"id": "100003817986566"
},
"from": {
"name": "Random User",
"id": "100002286042525"
},
"data": "100002286042525",
"message": "Use the app!",
"created_time": "2012-05-14T13:26:30+0000"
}, {
"id": "358318457550141_100003817986566",
"application": {
"name": "Random Competition",
"namespace": "randomcomp",
"id": "350578327437399"
},
"to": {
"name": "Hannah Smith",
"id": "100003817986566"
},
"from": {
"name": "Jane Young",
"id": "100003771838663"
},
"data": "100002286042525",
"message": "Use the app!",
"created_time": "2012-05-14T10:54:25+0000"
}],
}
}
As you can see, the data is being passed in, but there's still no way to join the click from the link mentioned above (for Random User) to the correct request in the list; the link passes through the ID for both requests, not just the one for Random User, and while the link states "Random User" it doesn't pass through an identifier.
Am I missing something? Is there a mechanism that isn't in the docs that will allow me to pick-up the "Random User" id so I can provide a nice "Random User invited you to..." message in the App when they click through?
Edit:
Turns out that this isn't possible - see my answer.
You can add some data to the request so that when you process the request you can differentiate that from other requests.
The data can be sent with app requests and users requests and the parameter name is "data".
For example, in the guide for the Requests Dialog you can see it in the properties table at the (almost) end of the document, it says:
Optional, additional data you may pass for tracking. This will be
stored as part of the request objects created. The maximum length is
255 characters
There's also some info about it in the Social Channels documentation, and a sample of (php) usage in the official blog post about Upgrade to Requests 2.0.
I hope that this is what you're looking for.
Edit
When you send the request you know who is sending it right? It's the logged in user, and so you can put the user id/name/etc in the data parameter of the request.
Then, when someone clicks on a request, get the request by the id that facebook passes to you, and from the data extract the user id of the sender.
As the documentation states, you have 255 characters to use, and with that you can do what ever you want, you can even serialize an object to that parameter and deserialize it later.
As it turns out, this is in fact not possible at all.
The link, as shown in the image below, can present multiple users:
which is why multiple request IDs are passed in, and the sender's ID isn't.

Programatically differentiate between facebook pages and profiles

I'm writing an app which requires me to determine whether I show the fb "like" button or not. fb profiles don't have "likes" but pages do. any recommendations on how I can programatically differentiate between a fb profile and a fb page? the only input i get from the user is the fb profile/page URL and based on that I need to determine whether its a page or a profile and then display the like button.
thx,
Yes, you can look at the type attribute of the returned JSON. For example, take a look at actual, redacted Graph API responses that I receive for a user and a page:
//This is JSON for a user
//Call to https://graph.facebook.com/123456789
{
"id": "123456789",
"name": "Sean Hill",
"first_name": "Sean",
"last_name": "Hill",
//more attributes
"type": "user" // <--- This one
}
vs
//This is JSON for a page
//Call to https://graph.facebook.com/thesolusean
{
"id": "323796444951",
"name": "Solusean",
"picture": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/50290_323796444951_3601170_s.jpg",
"link": "https://www.facebook.com/thesolusean",
"likes": 28,
// more attributes
"type": "page" // <---- This one
}
Without knowing which programming language you're using, this is the best answer I can give.
So far it seems the only quick way to know whether given profile is page or actual user is by looking at category attribute. If this attribute is present then its a page otherwise it could be user. FB reference docs doesn't seem to have any clear guidance (see https://developers.facebook.com/docs/graph-api/reference/profile). In additional most of the APIs that return profile as part of other response only includes partial information of id, name and category:
"from": {
"category": "Magazine",
"name": "Astronomy Magazine",
"id": "108218329601"
},
Note: I'm confused by #Sean Hill's answer. There is no type attribute in profile objects.

Facebook – Does a page exist for this URL?

I'm making a webpage/SEO checking page, where I can put in a URL and it will tell me various facts about the page.
I'd like to be able to tell if there is a Facebook page associated with the URL. Is this possible using the API, or by other means?
For instance, https://www.facebook.com/focalstrategy links to http://www.focalstrategy.com/ on the info tab – is there a way to go the other way and enter the URL and find the page(s)?
(Bonus points – anyone know of a way to do the same for Twitter accounts)
I don't think this is possible. Using the API, you can retrieve info about the url:
http://graph.facebook.com/?id=http://www.focalstrategy.com/
Would return:
{
"id": "http://www.focalstrategy.com/",
"shares": 38
}
Now I'm note sure if using the Open Graph Meta Tags would help return more data (still not what you need), but here is my website info without trailing slash:
http://graph.facebook.com/?id=http://www.masteringapi.com
Would return:
{
"id": "109784969102047",
"name": "MasteringAPI.com",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/276619_109784969102047_474156823_s.jpg",
"link": "http://www.masteringapi.com/",
"likes": 25,
"category": "Website",
"website": "http://www.masteringapi.com",
"description": "Master Facebook, Google and Twitter APIs! Learn Facebook Application Development Now!",
"can_post": true
}