我正在用 C# 构建一个聊天机器人,我希望在一些消息之后对话停止,但我不知道该怎么做。我已经设置了消息限制,并且我希望在达到此限制后不能再发送消息。有我的代码:
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
最后,添加对话结束,以便用户无法与聊天机器人进行更多对话。
谢谢你我已经解决了我的问题。如果它可以帮助别人,我会发布代码!
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)