A sample bot sending native metadata to Facebook using ChannelData.
The minimum prerequisites to run this sample are:
- The latest update of Visual Studio 2015. You can download the community version here for free.
- 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.
- 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.
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 ChannelData property.
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 ChannelData field. This allows you to do anything natively that Facebook supports via the attachment schema, such as Send a check-in reminder message.
Check out the ChannelDataDialog class where a new FacebookAttachment
instance is send to the Facebook API using the ChannelData property.
private static FacebookAttachment GetFlightAttachment()
{
return new FacebookAttachment()
{
Payload = new AirlineCheckIn()
{
IntroMessage = "Check-in is available now",
Locale = "en_US",
PnrNumber = "ABCDEF",
CheckInUrl = "http://www.airline.com/check_in",
FlightInfo = new[]
{
new FlightInfo()
{
FlightNumber = "F001",
DepartureAirport = new Airport()
{
AirportCode = "SFO",
City = "San Francisco",
Terminal = "T4",
Gate = "G8"
},
ArrivalAirport = new Airport()
{
AirportCode = "EZE",
City = "Buenos Aires",
Terminal = "C",
Gate = "A2"
},
FlightSchedule = new FlightSchedule()
{
BoardingTime = DateTime.Now.AddDays(1).ToString("yyyy-MM-ddTH:mm"),
DepartureTime = DateTime.Now.AddDays(1).AddHours(1.5).ToString("yyy-MM-ddTH:mm"),
ArrivalTime = DateTime.Now.AddDays(2).ToString("yyyy-MM-ddTH:mm")
}
}
}
}
};
}
Because the ChannelData
is a dynamic
property, it will hold any custom model class. In this sample, all Models classes were built to match the "attachment" JSON Schema described in the Facebook's Send API Reference.
Check out the ChannelDataDialog class using the ChannelData property to send the Airline Checkin Reminder. Additionally, the sample includes very little logic to render the attachment appropriately for different channels.
if (message.ChannelId != "facebook")
{
reply.Text = flightAttachment.ToString();
}
else
{
reply.ChannelData = new FacebookChannelData()
{
Attachment = flightAttachment
};
}
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 .NET and ChannelData please review the following resources: