A sample bot sending native metadata to Facebook using ChannelData.
[![Deploy to Azure][Deploy Button]][Deploy ChannelData/Node] [Deploy Button]: https://azuredeploy.net/deploybutton.png [Deploy ChannelData/Node]: https://azuredeploy.net
The minimum prerequisites to run this sample are:
- Latest Node.js with NPM. Download it from here.
- The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
- [Recommended] Visual Studio Code for IntelliSense and debugging, download it from here for free.
- To fully test this sample you must:
- Register you bot in Microsoft Bot Framework Portal. Please refer to this for the instructions. Once you complete the registration, update the Bot's Web.config file with the registered config values (Bot Id, MicrosoftAppId and MicrosoftAppPassword).
- Enable the Facebook Messenger Channel. Refer to this for more information on how to configure channels.
- Publish your bot, for example to Azure or use Ngrok to interact with your local bot in the cloud.
Many messaging channels provide the ability to attach richer objects. Bot Builder lets you express these attachments in a cross channel way and connectors will do their best to render the attachments using the channels native constructs.
If you want to be able to take advantage of special features or concepts for a channel we provide a way for you to send native metadata to that channel giving you much deeper control over how your bot interacts on a channel. The way you do this is to pass extra properties via the Message.sourceEvent method.
NOTE: You do not need to use this feature unless you feel the need to access functionality not provided by the normal Activity.
The Facebook adapter supports sending full attachments via the sourceEvent method. This allows you to do anything natively that Facebook supports via the attachment schema, such as Send a check-in reminder message.
Check out app.js where a new AirlineCheckin
instance is send to the Facebook API using the channel's native construct.
var checkin = new FacebookDataModels.AirlineCheckin(
'Check-in is available now',
'en_US',
'ABCDED',
'http://www.airline.com/check_in',
[
new FacebookDataModels.FlightInfo(
'F001',
new FacebookDataModels.Airport(
'SFO',
'San Francisco',
'T4',
'G8'
),
new FacebookDataModels.Airport(
'EZE',
'Buenos Aires',
'C',
'A2'
),
new FacebookDataModels.FlightSchedule(
now.addDays(1),
now.addDays(1).addHours(1.5),
now.addDays(2)
))
]);
Alternativly, you can provide JSON object instead of using the AirlineCheckin object.
var checkin = {
"type": "template",
"payload": {
"template_type": "airline_checkin",
"intro_message": "Check-in is available now",
"locale": "en_US",
"pnr_number": "ABCDED",
"checkin_url": "http://www.airline.com/check_in",
"flight_info": [
{
"flight_number": "F001",
"departure_airport": {
"airport_code": "SFO",
"city": "San Francisco",
"terminal": "T4",
"gate": "G8"
},
"arrival_airport": {
"airport_code": "EZE",
"city": "Buenos Aires",
"terminal": "C",
"gate": "A2"
},
"flight_schedule": {
"boarding_time": formatDate(now.addDays(1)),
"departure_time": formatDate(now.addDays(1).addHours(1.5)),
"arrival_time": formatDate(now.addDays(2))
}
}
]
}
};
Because sourceEvent
accepts any object, it will hold any custom model we pass to it. In this sample, all Models classes were built to match "attachment" JSON Schema described in the Facebook's Send API Reference.
Additionally, the sample includes a little logic to render the attachment appropriately for different channels.
var reply = new builder.Message(session);
if (session.message.address.channelId === 'facebook') {
reply.sourceEvent({
facebook: {
attachment: checkin
}
});
} else {
reply.text(checkin.toString());
}
session.send(reply);
You will see the following in the Bot Framework Emulator when opening and running the sample solution.
On the other hand, you will see the following in your Facebook Messenger.
To get more information about how to get started in Bot Builder for Node and Attachments please review the following resources: