简体   繁体   English

MS Bot Framework Sandwich Bot在命令行上显示JSON

[英]MS Bot Framework Sandwich Bot showing JSON on Command Line

I am using Microsoft's Bot Framework and following their tutorial (using C# for the .NET version) on building a Simple Sandwich Bot 我正在使用Microsoft的Bot Framework,并按照他们的教程(在.NET版本中使用C#)构建简单的三明治Bot

I have managed to get the simple version of the bot working, however, when I go to interact with the bot at any of the stages that are of type enum , the response from the bot is displayed in JSON format (unlike the plain English that the example shows) - can anyone help me on this one? 我已经设法获得了该机器人的简单版本,但是,当我在enum类型的任何阶段与该机器人进行交互时,该机器人的响应都以JSON格式显示(不同于普通的英语该示例显示)-有人可以帮我吗? I am using the Command Line version of the emulator as the ClickOnce emulator has issues with proxy authentication on the network I am on. 我使用的是命令行版本的模拟器,因为ClickOnce模拟器在我所在的网络上存在代理身份验证问题。

I am using the default code provided on the website:- 我正在使用网站上提供的默认代码:-

Sandwich.cs: Sandwich.cs:

using Microsoft.Bot.Builder.FormFlow;
using System;
using System.Collections.Generic;
#pragma warning disable 649
// The SandwichOrder is the simple form you want to fill out.  It must be serializable so the bot can be stateless.
// The order of fields defines the default order in which questions will be asked.
// Enumerations shows the legal options for each field in the SandwichOrder and the order is the order values will be presented 
// in a conversation.
namespace Microsoft.Bot.Sample.SimpleSandwichBot
{
    public enum SandwichOptions
    {
        BLT, BlackForestHam, BuffaloChicken, ChickenAndBaconRanchMelt, ColdCutCombo, MeatballMarinara,
        OvenRoastedChicken, RoastBeef, RotisserieStyleChicken, SpicyItalian, SteakAndCheese, SweetOnionTeriyaki, Tuna,
        TurkeyBreast, Veggie
    };
    public enum LengthOptions { SixInch, FootLong };
    public enum BreadOptions { NineGrainWheat, NineGrainHoneyOat, Italian, ItalianHerbsAndCheese, Flatbread };
    public enum CheeseOptions { American, MontereyCheddar, Pepperjack };
    public enum ToppingOptions
    {
        Avocado, BananaPeppers, Cucumbers, GreenBellPeppers, Jalapenos,
        Lettuce, Olives, Pickles, RedOnion, Spinach, Tomatoes
    };
    public enum SauceOptions
    {
        ChipotleSouthwest, HoneyMustard, LightMayonnaise, RegularMayonnaise,
        Mustard, Oil, Pepper, Ranch, SweetOnion, Vinegar
    };
    [Serializable]
    public class SandwichOrder
    {
        public SandwichOptions? Sandwich;
        public LengthOptions? Length;
        public BreadOptions? Bread;
        public CheeseOptions? Cheese;
        public List<ToppingOptions> Toppings;
        public List<SauceOptions> Sauce;
        public static IForm<SandwichOrder> BuildForm()
        {
            return new FormBuilder<SandwichOrder>()
                    .Message("Welcome to the simple sandwich order bot!")
                    .Build();
        }
    };
}

MessageController.cs: MessageController.cs:

using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;

namespace ProperBot
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        internal static IDialog<SandwichOrder> MakeRootDialog()
        {
            return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildForm))
                .Do(async (context, order) =>
                {
                    try
                    {
                        var completed = await order;
                        // Actually process the sandwich order...
                        await context.PostAsync("Processed your order!");
                    }
                    catch (FormCanceledException<SandwichOrder> e)
                    {
                        string reply;
                        if (e.InnerException == null)
                        {
                            reply = $"You quit on {e.Last}--maybe you can finish next time!";
                        }
                        else
                        {
                            reply = "Sorry, I've had a short circuit.  Please try again.";
                        }
                        await context.PostAsync(reply);
                    }
                });
        }
        [ResponseType(typeof(void))]
        public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
        {

            if (activity != null)
            {
                // one of these will have an interface and process it
                switch (activity.GetActivityType())
                {
                    case ActivityTypes.Message:
                        await Conversation.SendAsync(activity, MakeRootDialog);
                        break;
                    case ActivityTypes.ConversationUpdate:
                    case ActivityTypes.ContactRelationUpdate:           
                    case ActivityTypes.Typing:           
                    case ActivityTypes.DeleteUserData:               
                    default:
                        Trace.TraceError($"Unknown activity type ignored: {activity.GetActivityType()}");
                        break;
                }

            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

    }
}

The response I am receiving: 我收到的回复:

Bot1 said:
 Welcome to the sandwich order bot!
Bot1 said:

[
  {
    "contentType": "application/vnd.microsoft.card.hero",
    "content": {
      "text": "What kind of sandwich would you like, eh? ",
      "buttons": [
        {
          "type": "imBack",
          "title": "BLT",
          "value": "BLT"
        },
        {
          "type": "imBack",
          "title": "Black Forest Ham",
          "value": "Black Forest Ham"
        },
        {
          "type": "imBack",
          "title": "Buffalo Chicken",
          "value": "Buffalo Chicken"
        },
        {
          "type": "imBack",
          "title": "Chicken And Bacon Ranch Melt",
          "value": "Chicken And Bacon Ranch Melt"
        },
        {
          "type": "imBack",
          "title": "Cold Cut Combo",
          "value": "Cold Cut Combo"
        },
        {
          "type": "imBack",
          "title": "Meatball Marinara",
          "value": "Meatball Marinara"
        },
        {
          "type": "imBack",
          "title": "Oven Roasted Chicken",
          "value": "Oven Roasted Chicken"
        },
        {
          "type": "imBack",
          "title": "Roast Beef",
          "value": "Roast Beef"
        },
        {
          "type": "imBack",
          "title": "Rotisserie Style Chicken",
          "value": "Rotisserie Style Chicken"
        },
        {
          "type": "imBack",
          "title": "Spicy Italian",
          "value": "Spicy Italian"
        },
        {
          "type": "imBack",
          "title": "Steak And Cheese",
          "value": "Steak And Cheese"
        },
        {
          "type": "imBack",
          "title": "Sweet Onion Teriyaki",
          "value": "Sweet Onion Teriyaki"
        },
        {
          "type": "imBack",
          "title": "Tuna",
          "value": "Tuna"
        },
        {
          "type": "imBack",
          "title": "Turkey Breast",
          "value": "Turkey Breast"
        },
        {
          "type": "imBack",
          "title": "Veggie",
          "value": "Veggie"
        }
      ]
    }
  }
]

The response I should be receiving: 我应该收到的答复是:

Please select a sandwich
1. BLT
2. Black Forest Ham
3. Buffalo Chicken
4. Chicken And Bacon Ranch Melt
5. Cold Cut Combo
6. Meatball Marinara
7. Oven Roasted Chicken
8. Roast Beef
9. Rotisserie Style Chicken
10. Spicy Italian
11. Steak And Cheese
12. Sweet Onion Teriyaki
13. Tuna
14. Turkey Breast
15. Veggie
>

Please note that I only receive this type of response when the bot is dealing with an enum - if the bot is dealing with a List type or string , then the responses are in plain English. 请注意,当机器人正在处理enum时,我只会收到这种类型的响应-如果机器人正在处理List类型或string ,则响应以纯英语显示。

这是因为控制台无法读取这些类型的响应,因为它们是Microsoft类型的称为“卡”的响应-Skype,Facebook Messenger和E-mail等应用程序都可以读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM