End a conversation c# bot

故事扮演 提交于 2021-01-27 18:58:47

问题


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.


回答1:


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

            }

        }



回答2:


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


来源:https://stackoverflow.com/questions/46242016/end-a-conversation-c-sharp-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!