Skip to content

Latest commit

 

History

History
133 lines (111 loc) · 6.79 KB

File metadata and controls

133 lines (111 loc) · 6.79 KB

ChannelData Bot Sample

A sample bot sending native metadata to Facebook using ChannelData.

Deploy to Azure

Prerequisites

The minimum prerequisites to run this sample are:

Code Highlights

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

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 Node and Attachments please review the following resources: