结束对话c#bot

时间:2017-09-15 14:31:13

标签: c# visual-studio botframework chatbot

我正在用C#构建一个ChatBot,我希望在一些消息之后会话停止,但我不知道该怎么做。我已经设置了邮件限制,我希望在达到此限制后不再发送邮件。有我的代码:

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

我删除了HeroCard部分,因为它在这里没用。

我想要的是在最后context.Wait之后,添加一个对话结束,这样用户就无法与聊天机器人交谈。

2 个答案:

答案 0 :(得分:3)

谢谢你我解决了我的问题。我发布代码,如果它可以帮助某人!

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

            }

        }

答案 1 :(得分:1)

您的问题有点含糊不清,结束对话是什么意思?您是否希望用户再也无法与聊天机器人交谈?您可以在else屏蔽中执行一项操作,可以拨打context.Done()并删除允许您的用户发送消息但未获得回复的context.Wait(this.MessageReceived),或者在下面的情况下,用户知道对话已经结束。

else
{ 

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