Created
September 1, 2017 19:24
-
-
Save nwhitmont/be33c0c90430b7f344a43219faa89c57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = require('botbuilder'); | |
var restify = require('restify'); | |
//Server setup | |
var server = restify.createServer(); | |
server.listen(process.env.port || process.env.PORT || 3978, function() { | |
console.log('%s listening to %s', server.name, server.url); | |
}); | |
//Get secrets from server environment | |
var connector = new builder.ChatConnector({ | |
appId: process.env.MICROSOFT_APP_ID, | |
appPassword: process.env.MICROSOFT_APP_PASSWORD | |
}); | |
//Create chat bot | |
var bot = new builder.UniversalBot(connector); | |
//Handle bot framework messages | |
server.post('/api/messages', connector.listen()); | |
// Welcome message | |
bot.on('conversationUpdate', function (message) { | |
if (message.membersAdded) { | |
message.membersAdded.forEach(function (identity) { | |
if (identity.id == message.address.bot.id) { | |
// Bot is joining conversation | |
// - For WebChat channel you'll get this on page load. | |
var reply = new builder.Message() | |
.address(message.address) | |
.text("Welcome to the Carousel of Cards demo. Type any key to start."); | |
bot.send(reply); | |
} else { | |
// User is joining conversation | |
// - For WebChat channel this will be sent when user sends first message. | |
// - When a user joins a conversation the address.user field is often for | |
// essentially a system account so to ensure we're targeting the right | |
// user we can tweek the address object to reference the joining user. | |
// - If we wanted to send a private message to teh joining user we could | |
// delete the address.conversation field from the cloned address. | |
var address = Object.create(message.address); | |
address.user = identity; | |
var reply = new builder.Message() | |
.address(address) | |
.text("Hello %s", identity.name); | |
bot.send(reply); | |
} | |
}); | |
} | |
}); | |
bot.dialog('/carousel', [ | |
function (session) { | |
session.send("You can pass a custom message to Prompts.choice() that will present the user with a carousel of cards to select from. Each card can even support multiple actions."); | |
// build the message object with the card attachments | |
var msg = new builder.Message(session) | |
.textFormat(builder.TextFormat.xml) | |
.attachmentLayout(builder.AttachmentLayout.carousel) | |
.attachments([ | |
new builder.HeroCard(session) | |
.title("Space Needle") | |
.text("The Space Needle is an observation tower in Seattle, Washington, a landmark of the Pacific Northwest, and an icon of Seattle.") | |
.images([ | |
builder.CardImage.create(session, "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/320px-Seattlenighttimequeenanne.jpg") | |
.tap(builder.CardAction.showImage(session, "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/800px-Seattlenighttimequeenanne.jpg")), | |
]) | |
.buttons([ | |
builder.CardAction.openUrl(session, "https://en.wikipedia.org/wiki/Space_Needle", "Wikipedia"), | |
builder.CardAction.imBack(session, "Space Needle", "Select") | |
]), | |
new builder.HeroCard(session) | |
.title("Pike Place Market") | |
.text("Pike Place Market is a public market overlooking the Elliott Bay waterfront in Seattle, Washington, United States.") | |
.images([ | |
builder.CardImage.create(session, "https://upload.wikimedia.org/wikipedia/en/thumb/2/2a/PikePlaceMarket.jpg/320px-PikePlaceMarket.jpg") | |
.tap(builder.CardAction.showImage(session, "https://upload.wikimedia.org/wikipedia/en/thumb/2/2a/PikePlaceMarket.jpg/800px-PikePlaceMarket.jpg")), | |
]) | |
.buttons([ | |
builder.CardAction.openUrl(session, "https://en.wikipedia.org/wiki/Pike_Place_Market", "Wikipedia"), | |
builder.CardAction.imBack(session, "Pike Place Market", "Select") | |
]), | |
new builder.HeroCard(session) | |
.title("EMP Museum") | |
.text("EMP Musem is a leading-edge nonprofit museum, dedicated to the ideas and risk-taking that fuel contemporary popular culture.") | |
.images([ | |
builder.CardImage.create(session, "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Night_Exterior_EMP.jpg/320px-Night_Exterior_EMP.jpg") | |
.tap(builder.CardAction.showImage(session, "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Night_Exterior_EMP.jpg/800px-Night_Exterior_EMP.jpg")) | |
]) | |
.buttons([ | |
builder.CardAction.openUrl(session, "https://en.wikipedia.org/wiki/EMP_Museum", "Wikipedia"), | |
builder.CardAction.imBack(session, "EMP Museum", "Select") | |
]) | |
]); | |
// Ask the user to select an item from a carousel. | |
session.send('Select a destination to add to your Seattle tour:') | |
builder.Prompts.choice(session, msg, "Space Needle|Pike Place Market|EMP Museum"); | |
}, | |
function (session, results) { | |
var selectedCard = results.response.entity; | |
session.endDialog(`You selected: ${selectedCard}`); | |
} | |
]); | |
/* Dialogs */ | |
bot.dialog('/',function(session) { | |
session.send('Starting /carousel dialog...'); | |
session.beginDialog('/carousel'); | |
}); | |
// END OF LINE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment