MS Bot框架三明治机器人在命令行上显示JSON

时间:2016-10-19 07:50:47

标签: c# json frameworks bots botframework

我正在使用Microsoft的Bot Framework并按照他们的教程(使用C#for .NET版本)构建Simple Sandwich Bot

我设法让机器人的简单版本工作,但是,当我在类型为enum的任何阶段与机器人进行交互时,机器人的响应以JSON显示格式(与示例所示的普通英语不同) - 任何人都可以帮我这个吗?我正在使用模拟器的命令行版本,因为ClickOnce模拟器在我所在的网络上存在代理身份验证问题。

我使用网站上提供的默认代码: -

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:

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

    }
}

我收到的回复:

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"
        }
      ]
    }
  }
]

我应该收到的回复:

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
>

请注意,当机器人处理enum时,我才会收到此类响应 - 如果机器人正在处理List类型或string,那么回复是用简单的英语。

1 个答案:

答案 0 :(得分:0)

这是因为控制台无法读取这些类型的响应,因为它们是名为“卡片”的Microsoft类型响应 - Skype,Facebook Messenger和电子邮件等应用程序可以读取它。