2

I'm building a ChatBot in C# and I want that after some messages the conversation stop, but I don't know how to do it. I have already set a limit of messages, and I want that after the reach of this limit no more messages can be send. There is my code:

private int NombreDeMessages;

protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
{
    var message = await item;
    NombreDeMessages += 1; 

    if (message.Text != null && NombreDeMessages < 3)
    {
         await base.MessageReceived(context, item);
    } 
    else
    { 
         var reply = context.MakeMessage();
         await context.PostAsync(reply);
         context.Wait(this.MessageReceived);
    }                
}

I deleted the HeroCard part because it is useless here.

The thing that I want is after the context.Wait at the end, add a end of conversation so the user can't talk more to the chatbot.

0

2 Answers 2

3

Thank you I have solved my problem. I post the code if it can help someone !

private int NombreDeMessages;
        protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
        {
            var message = await item;
            NombreDeMessages += 1;
            string code = EndOfConversationCodes.CompletedSuccessfully;
            CancellationToken cancellationToken = default(CancellationToken);


            if (message.Text != null && NombreDeMessages < 3)
            {
                await base.MessageReceived(context, item);

            }
            else if (message.Text != null && NombreDeMessages == 3)
            {
                AdaptiveCard card = new AdaptiveCard();
                card.Body.Add(new TextBlock()
                {
                    Text = "STOP FLOODING",
                    Weight = TextWeight.Bolder,
                    IsSubtle = true,
                    Wrap = true,
                    Size = TextSize.Large
                });

                card.Body.Add(new TextBlock()
                {
                    Text = "You have reach the limit of queries",
                    IsSubtle = false,
                    Wrap = true,
                    Size = TextSize.Normal
                });

                card.Body.Add(new Image()
                {
                    Url = "http://images.roadtrafficsigns.com/img/dp/lg/traffic-stop-sign.png",

                    HorizontalAlignment = HorizontalAlignment.Center,
                    Size = ImageSize.Stretch
                });

                Attachment attachment = new Attachment()
                {
                    ContentType = AdaptiveCard.ContentType,
                    Content = card
                };
                var flood = context.MakeMessage();
                flood.Attachments.Add(attachment);

                await context.PostAsync(flood);

            }
            else
            {

                var reply = context.MakeMessage();

                reply.Type = ActivityTypes.EndOfConversation;
                reply.AsEndOfConversationActivity().Code = code;

                await context.PostAsync(reply, cancellationToken);

            }

        }
Sign up to request clarification or add additional context in comments.

Comments

1

Your question is a little ambiguous,What do you mean by end conversation do you want the user to never be able to talk to the chatbot again? One thing you can do in the else block you have you can call context.Done() and remove your context.Wait(this.MessageReceived) that would allow your user to send messages but get no response, or in the case below let the user know the conversation has ended.

else
{ 

    var reply = context.MakeMessage();
    reply.Text = "conversation ended";
    await context.PostAsync(reply);
    context.Done(this);
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.