如何在Bot框架应用程序中解决错误“抱歉,我的机器人代码有问题”

时间:2017-08-29 06:36:52

标签: c# botframework

我收到错误“抱歉,我的机器人代码有问题”,当多个用户通过网络聊天频道中的c#使用我的机器人应用程序时。消息控制器:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)   
{
    try
    {
        if (activity.Type == ActivityTypes.Message)
        {
            StateClient sc = activity.GetStateClient();
            BotData userData = await sc.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
            userData.SetProperty<bool>("MyDetails",true);
            // Save BotUserData
            await sc.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
            await Conversation.SendAsync(activity, () => new RootDialog());
            //await Conversation.SendAsync(activity, () => new ExceptionHandlerDialog<object>(new RootDialog(),displayException:false));
        }
        else
        {
            HandleSystemMessage(activity);
        }
    }
    catch (HttpOperationException err)
    {
        // handle error with HTTP status code 412 Precondition Failed
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

//在root对话框中我们用来逐个调用多个对话框

enter code here

public class RootDialog : IDialog<object>
{

    private string name;

    public async Task StartAsync(IDialogContext context)
    {
        /* Wait until the first message is received from the conversation and call MessageReceviedAsync 
         *  to process that message. */
        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        /* When MessageReceivedAsync is called, it's passed an IAwaitable<IMessageActivity>. To get the message,
         *  await the result. */
        var message = await result;

        await this.SendWelcomeMessageAsync(context);
    }

    private async Task SendWelcomeMessageAsync(IDialogContext context)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<b>");
        sb.Append("<p style='Color:red; font-size:20px;'>");
        sb.Append("Hello").AppendLine().AppendLine();
        sb.Append("</p>");
        sb.Append("</b>");
        sb.Append("I am mBOT, I can help you select and Configure. ").AppendLine().AppendLine();

        await context.PostAsync(sb.ToString());

        context.Call(new NameDialog(), this.NameDialogResumeAfter);
    }

    private async Task NameDialogResumeAfter(IDialogContext context, IAwaitable<string> result)
    {
        try
        {
            this.name = await result;
            context.Call(new Vehicle(this.name), this.VehicleDialogResumeAfter);
        }
        catch (TooManyAttemptsException)
        {
            await context.PostAsync("I'm sorry, I'm having issues understanding you. Let's try again.");

            await this.SendWelcomeMessageAsync(context);
        }
    }
}

请建议如何解决我的问题。如何管理多个用户。

0 个答案:

没有答案