GithubHelp home page GithubHelp logo

matthidinger / contososcubabot Goto Github PK

View Code? Open in Web Editor NEW
40.0 9.0 36.0 5.29 MB

Sample bot built with rich Adaptive Cards

Home Page: https://contososcubademo.azurewebsites.net

License: MIT License

C# 75.87% HTML 24.13%

contososcubabot's Introduction

Contoso Dive Finder

Introduction

Most chat bot platforms have a single input box through which users type messages in order to interact with a bot. Adaptive Cards provide a new way to create custom and interactive cards, with rich visuals and controls, that adapt to the platform on which they are being displayed. In order to demonstrate the richness of Adaptive Cards in this sample, we've created a bot for a fictitious company that provides a scuba diving search and reservation service. Contoso Dive Finder demonstrates incorporating Adaptive Cards into a bot built using the Microsoft Bot Builder .NET SDK and the Adaptive Cards nuget package.

Walk Through

Getting Started

A published example of the Contoso Dive Finder is: ContosoScubaDemo.AzureWebsites.net

If you desire to run the sample yourself:

  • Download the source
  • Create a Bot Registration in the Azure Portal (Instructions can be found here: Quickstart Guide)
  • Retrieve your bot's Direct Line channel secret from the direct line configuration page and add it to the default.htm page
  • Add your bot's MicrosoftAppId and MicrosoftAppPassword to the appsettings.json
  • Publish your bot (make sure the messaging endpoint in bot settings is set to the published url: https://sitename.azurewebsites.net/api/messages)

Creating Adaptive Cards

Adaptive Cards not only visually conform to the layout and style of the platform they are displayed on, they are also easy for developers to work with. Microsoft has engineered the library to be flexible, extendable, and to provide developers with multiple methods for construction and manipulation. In this sample, we've chosen to load .json files containing Messages with nested cards.

Some Code

Loading a card is as simple as reading the .json file, and creating a reply with its contents:

 public static async Task<string> GetCardText(string cardName)
{
    return await File.ReadAllTextAsync($@".\Cards/{cardName}.JSON");
} 

The ScubaCardService source code is not shown here. It is responsible for loading the card providers via reflection, and calling each of them to determine which one is responsible for handling the incoming message activity. Shown here is the GetNextScubaMessage method, and GetCardReply, in the RootDialog. This code turns the adaptive card into a message that is sent back to the user.

private async Task<IMessageActivity> GetNextScubaMessage(ITurnContext context, Activity activity)
{
    var resultInfo = await new ScubaCardService().GetNextCardText(context, activity);
    if (!string.IsNullOrEmpty(resultInfo.ErrorMessage))
        return activity.CreateReply(resultInfo.ErrorMessage);

    return GetCardReply(activity, resultInfo.CardText);
}

public static Activity GetCardReply(Activity activity, string cardText)
{
    var reply = JsonConvert.DeserializeObject<Activity>(cardText);
    if (reply.Attachments == null)
        reply.Attachments = new List<Attachment>();

    var tempReply = activity.CreateReply("");
    reply.ChannelId = tempReply.ChannelId;
    reply.Timestamp = tempReply.Timestamp;
    reply.From = tempReply.From;
    reply.Conversation = tempReply.Conversation;
    reply.Recipient = tempReply.Recipient;
    reply.Id = tempReply.Id;
    reply.ReplyToId = tempReply.ReplyToId;
    if (reply.Type == null)
        reply.Type = ActivityTypes.Message;

    return reply;
}

Welcome Card

With columns, image support, text manipulation properties and more: Adaptive Cards are powerful and fun to use!

{
  "type": "message",
  "text": "Hi! I'm Contoso Dive Finder!\n\nI'll help you schedule a scuba getaway!",
  "attachments": [
    {
      "contentType": "application/vnd.microsoft.card.adaptive",
      "content": {
        "type": "AdaptiveCard",
        "body": [
          {
            "type": "ColumnSet",
            "columns": [
              {
                "type": "Column",
                "size": "auto",
                "items": [
                  {
                    "type": "Image",
                    "size": "medium",
                    "url": "http://contososcuba.azurewebsites.net/assets/Contoso_logo.jpg"
                  }
                ]
              },
              {
                "type": "Column",
                "items": [
                  {
                    "type": "TextBlock",
                    "text": "Start with something like:",
                    "size": "large",
                    "weight": "bolder"
                  },
                  {
                    "type": "TextBlock",
                    "text": "'Where can I go scuba diving?'",
                    "separation": "none"
                  },
                  {
                    "type": "TextBlock",
                    "text": "or",
                    "separation": "none"
                  },
                  {
                    "type": "TextBlock",
                    "text": "'Show me some local wildlife.'",
                    "separation": "none"
                  },
                  {
                    "type": "TextBlock",
                    "text": "You can also 'restart' anytime.",
                    "separation": "none"
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  ]
}

More Information

contososcubabot's People

Contributors

matthidinger avatar tfennel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

contososcubabot's Issues

Display 1 card at at time in Carousel

Is there a way to implement horizontal carousel that displays 1 card at at time? I want to show only 1 card at a time in the webchat window. Currently it displays all the cards at the same time that fit into the webchat window.

"Sorry, my bot code is having an issue." on https://contososcubabot.azurewebsites.net/

Type:
i go

and get a carousel of locations.

Click on the "This one" button

an answer card is shown.

Click again on "This one" button

result: "Sorry, my bot code is having an issue."

expected: if the user already clicked on a button, it should not be possible to click the button again.

it should be possible to update the card shown with the Action.Submit response e.g. remove buttons, show confirmation

Dynamically binding data to a card

How would you go about dynamically binding data to an adaptive card?

For example, in Schools.JSON you can assume this query would actually go look up schools on Google then dynamically build the card. Is there a way to do this without hacking together the card through code? Any data binding options?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.