我们可以让聊天机器人先说问候,而不仅仅是作为一种反应

我正在使用Microsoftt Bot Framework和LUIS认知服务开发一个聊天机器人。 我想要一条初步的欢迎信息,比如“你好用户你好!” 我的机器人一开始。

任何事都可以在MessageController中完成

public async Task Post([FromBody]Activity activity) { Trace.TraceInformation($"Type={activity.Type} Text={activity.Text}"); if (activity.Type == ActivityTypes.Message) { //await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () => new ContactOneDialog()); await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () => new ExceptionHandlerDialog(new ShuttleBusDialog(), displayException: true)); //await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () => new ShuttleBusDialog()); } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(System.Net.HttpStatusCode.OK); return response; } 

您可能希望探索作为ConversationUpdate事件的一部分发送消息。 更新HandleSystemMessage方法,使其如下所示:

  private async Task HandleSystemMessage(Activity message) { if (message.Type == ActivityTypes.DeleteUserData) { // Implement user deletion here // If we handle user deletion, return a real message } else if (message.Type == ActivityTypes.ConversationUpdate) { ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl)); var reply = message.CreateReply(); reply.Text = "Hello user how are you?" await client.Conversations.ReplyToActivityAsync(reply); } else if (message.Type == ActivityTypes.ContactRelationUpdate) { // Handle add/remove from contact lists // Activity.From + Activity.Action represent what happened } else if (message.Type == ActivityTypes.Typing) { // Handle knowing tha the user is typing } else if (message.Type == ActivityTypes.Ping) { } } 

在新版本中,HandleSystemMessage不再是异步,它返回一个Activity,所以这对我有用:

  private Activity HandleSystemMessage(Activity message) { if (message.Type == ActivityTypes.DeleteUserData) { // Implement user deletion here // If we handle user deletion, return a real message } else if (message.Type == ActivityTypes.ConversationUpdate) { if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id)) { ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl)); Activity reply = message.CreateReply("I am your service provider virtual assistant, How can I help you today? "); connector.Conversations.ReplyToActivityAsync(reply); } } else if (message.Type == ActivityTypes.ContactRelationUpdate) { // Handle add/remove from contact lists // Activity.From + Activity.Action represent what happened } else if (message.Type == ActivityTypes.Typing) { // Handle knowing tha the user is typing } else if (message.Type == ActivityTypes.Ping) { } return null; } 

请注意以下事项:

  1. 由于HandleSystemMessage不是异步,因此您无法“等待”回复。
  2. 使用收件人ID检查以避免重复的欢迎消息

为什么修改HandleSystemMessage方法,只需在Post方法中处理它?

因此,您不必乱用创建新connector并使用不熟悉的connector.Conversations.ReplyToActivityAsync(reply)方法。

您可以简单地启动根对话框 ,就像回复邮件时一样:

  public async Task Post([FromBody]Activity activity) { // ... some code ... // this will also handle the beginning of the conversation - // that is when activity.Type equals to ConversationUpdate if (activity.Type == ActivityTypes.Message || activity.Type == ActivityTypes.ConversationUpdate) { // Because ConversationUpdate activity is received twice // (once for the Bot being added to the conversation, and the 2nd time - // for the user), we have to filter one out, if we don't want the dialog // to get started twice. Otherwise the user will receive a duplicate message. if (activity.Type == ActivityTypes.ConversationUpdate && !activity.MembersAdded.Any(r => r.Name == "Bot")) return response; // start your root dialog here await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () => new ExceptionHandlerDialog(new ShuttleBusDialog(), displayException: true)); } // handle other types of activity here (other than Message and ConversationUpdate // activity types) else { HandleSystemMessage(activity); } var response = Request.CreateResponse(System.Net.HttpStatusCode.OK); return response; } 

当我使用“WebChat”频道时,下面的代码工作正常。 ChatBot能够发起问候消息,也没有重复!

 case ActivityTypes.ConversationUpdate: IConversationUpdateActivity update = activity; var client = new ConnectorClient(new Uri(activity.ServiceUrl), new MicrosoftAppCredentials()); if (update.MembersAdded != null && update.MembersAdded.Any()) { foreach (var newMember in update.MembersAdded) { if (newMember.Id == activity.Recipient.Id) { var reply = activity.CreateReply(); reply.Text = $"Welcome {newMember.Name}!"; await client.Conversations.ReplyToActivityAsync(reply); } } } break;