结束对话C#Bot [英] End a conversation c# bot
本文介绍了结束对话C#Bot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用C#构建一个ChatBot,我希望在一些消息结束后对话停止,但是我不知道该怎么做.我已经设置了邮件限制,我希望在达到此限制后不能再发送邮件.有我的代码:
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);
}
}
我删除了HeroCard部分,因为这里没有用.
I deleted the HeroCard part because it is useless here.
我想要的是在 context.Wait
之后,添加对话结束,以便用户无法与聊天机器人进行更多对话.
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.
推荐答案
谢谢,我已经解决了我的问题.我发布代码是否可以帮助某人!
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);
}
}
这篇关于结束对话C#Bot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文