Skip to content

Latest commit

 

History

History
108 lines (86 loc) · 5.75 KB

File metadata and controls

108 lines (86 loc) · 5.75 KB

ChannelData Bot Sample

A sample bot sending native metadata to Facebook using ChannelData.

[![Deploy to Azure][Deploy Button]][Deploy ChannelData/CSharp] [Deploy Button]: https://azuredeploy.net/deploybutton.png [Deploy ChannelData/CSharp]: https://azuredeploy.net

Prerequisites

The minimum prerequisites to run this sample are:

Code Highlights

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

Outcome

You will see the following in the Bot Framework Emulator when opening and running the sample solution.

Sample Outcome Emulator

On the other hand, you will see the following in your Facebook Messenger.

Sample Outcome Facebook

More Information

To get more information about how to get started in Bot Builder for .NET and ChannelData please review the following resources: