【发布时间】:2019-11-26 15:12:33
【问题描述】:
我已经使用 v3 (C#) SDK 创建了一个机器人,并且过去的欢迎消息可以正常工作,没有任何汗水。在生产中它仍然对我有用。代码在 HandleSystemMessage 中像这样处理 -
.. v3 代码为清楚起见删除了附加代码...
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
//Code to show Welcome Message
if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
{
var reply = message.CreateReply();
reply.Attachments = new List<Attachment>();
// Create the attachment.
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = AdaptiveCardHelper.GetOptionsCard()
};
reply.Attachments.Add(attachment);
ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
我使用的网络聊天版本是 BotFramework-WebChat-0.11.4,我在其中进行了某些自定义以实现带有评论的 facebook Like/Unlike 功能。
现在我正在将机器人迁移到 v4 SDK(C# + .Net Core Web 应用程序),并且我打算使用相同的旧版本的网络聊天。但是我挣扎了两天才能在同一个网络聊天中显示欢迎消息,而它在模拟器(鉴于这两个 ConversationUpdate)事件上运行良好。
我已尝试使用本文中提供的解决方案发送消息和事件,并尝试在不同的异步方法 OnEventAsync、OnEventActivityAsync、OnMessageActivityAsync 上在 Bot 中捕获它。
V4 代码如下所示:
protected override async Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.MembersAdded != null)
{
if (turnContext.Activity.MembersAdded.Any(m => m.Id != turnContext.Activity.Recipient?.Id))
{
//var welcomeCard = CreateAdaptiveCardAttachment();
//var response = CreateResponse(turnContext.Activity, welcomeCard);
//await turnContext.SendActivityAsync(response, cancellationToken);
await Utility.LogTraceAsync("Inside OnConversationUpdateActivityAsync");
var eventActivity = turnContext.Activity.AsConversationUpdateActivity();
ConnectorClient connector = new ConnectorClient(new Uri(eventActivity.ServiceUrl), Configuration.MicrosoftAppId, Configuration.MicrosoftAppPassword);
await Utility.LogTraceAsync("Service URL OnConversationUpdateActivityAsync" + eventActivity.ServiceUrl);
await Utility.LogTraceAsync("Recipient ID OnConversationUpdateActivityAsync" + turnContext.Activity.Recipient?.Id);
var welcomeCard = CreateAdaptiveCardAttachment();
var reply = ((Activity)eventActivity).CreateReply();
reply.Attachments.Add(welcomeCard);
//var response = CreateResponse(turnContext.Activity, welcomeCard);
await connector.Conversations.ReplyToActivityAsync(reply, cancellationToken);// turnContext.SendActivityAsync(response, cancellationToken);
await Utility.LogTraceAsync("OnConversationUpdateActivityAsync Response Returned.");
await Utility.LogTraceAsync("Exit OnConversationUpdateActivityAsync");
}
}
}
protected override async Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
await Utility.LogTraceAsync("Inside OnEventActivityAsync");
if (turnContext.Activity.Type == ActivityTypes.Event)
{
var eventActivity = turnContext.Activity.AsEventActivity();
await Utility.LogTraceAsync("Event Activity from WebChat matched.");
ConnectorClient connector = new ConnectorClient(new Uri(eventActivity.ServiceUrl), Configuration.MicrosoftAppId, Configuration.MicrosoftAppPassword);
await Utility.LogTraceAsync("Service URL " + eventActivity.ServiceUrl);
var welcomeCard = CreateAdaptiveCardAttachment();
var reply = ((Activity)eventActivity).CreateReply();
reply.Attachments.Add(welcomeCard);
var members = await connector.Conversations.GetConversationMembersAsync(eventActivity.Conversation.Id.ToString());
var membernames = "";
foreach (var member in members) {
membernames += member.Name + ",";
}
await Utility.LogTraceAsync(membernames);
await connector.Conversations.SendToConversationAsync(reply, cancellationToken);
await connector.Conversations.ReplyToActivityAsync(reply, cancellationToken);// turnContext.SendActivityAsync(response, cancellationToken);
await Utility.LogTraceAsync("Event Response Returned.");
}
await Utility.LogTraceAsync("Exit OnEventActivityAsync");
}
但它似乎根本不起作用。我正在拔头发,不知道如何为 .Net Core App 做些什么。我很高兴知道是否有人解决了这个问题。
更新 - 我在客户端使用了 @tdurnford 提供的 JS 代码,在 Bot Side 有以下两种方法 -
//Required to Show Welcome Message on Emulator
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded ?? Array.Empty<ChannelAccount>())
{
// Greet anyone that was not the target (recipient) of this message.
// To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
if (member.Id != turnContext.Activity.Recipient.Id)
{
Activity reply = ((Activity)turnContext.Activity).CreateReply();
AdaptiveCard card = AdaptiveCardHelper.GetWelcomeCard();
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
reply.Attachments.Add(attachment);
await turnContext.SendActivityAsync(reply, cancellationToken);
}
}
}
//Required to Show Welcome Message on Web Chat
protected override async Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.Name == "webchat/join")
{
Activity reply = ((Activity)turnContext.Activity).CreateReply();
AdaptiveCard card = AdaptiveCardHelper.GetWelcomeCard();
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
reply.Attachments.Add(attachment);
await turnContext.SendActivityAsync(reply, cancellationToken);
}
}
两种方法都会在聊天窗口中显示两条欢迎消息 -
Bot Window with two Welcome Messages
然后我在 C# 中注释了 OnEventActivityAsync 方法并再次部署。现在它只显示从 OnMembersAddedAsync 返回的一条欢迎消息,如窗口所示。
Bot Window with only one Welcome Message
如果我在网络聊天代码中注释以下代码行,即不发送后期活动 -
botConnection.postActivity({
from: {
id: 'myUserId',
name: 'myUserName'
},
type: 'event',
name: 'webchat/join',
value: {
locale: 'en-US'
}
}).subscribe(
id => console.log("Posted welcome event, assigned ID ", id),
error => console.log("Error posting activity", error)
);
在这种情况下,不会显示欢迎消息。 @tdurnford,请检查您是否能够复制此行为。
虽然这里存在另一个问题,即当用户在机器人中键入问题时,会再次显示欢迎消息。 Bot window with two welcome messages one on load and another after the first question
【问题讨论】:
-
您为什么使用网络聊天 v0.11.4?最新版本的网络聊天是高度可定制的,并且有一个sample,它展示了如何快速将反应按钮添加到消息中。我建议看一下 v4,因为不再支持旧版本。
-
如果您确实想继续使用当前版本,由于我不熟悉 v0.11.4,因此我需要向您获取更多信息。该版本是否仍使用 DirectLineJs 包装?如果您可以添加您的网络聊天代码,那将很有帮助。
-
@tdurnford,是的,它使用 DirectLineJs 包。这里的问题是,在 v3 机器人上它工作得很好,而不会引发额外的事件或类似的事情。这是该版本的源链接-github.com/microsoft/BotFramework-WebChat/releases/tag/v0.11.4
-
@tdurnford,您是否可以通过简单的 Core Bot + Web Chat 0.11.4 最终尝试一下。我非常确定您将能够看到问题的原因。
-
该版本的示例文件夹中有一个反向通道示例@amitc 您引用的博客文章只是使用反向通道向机器人发送请求以获取欢迎消息。 bot 中的处理方式已从 v3 更改为 v4,但 0.11.4 反向通道客户端代码是相同的。
标签: c# botframework