1

I am getting error "Sorry, my bot code is having an issue", when multiple user use my bot application through c# in web chat channel. Message Controller :

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

// in root dialog we used to call multiple dialog one by one

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

Please suggest how to solve my issue. how to manage multiple user also.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60

0 Answers0