Bot Framework V3 - 如何定义HandleSystemMessage

时间:2016-08-02 09:28:46

标签: bots botframework

我正在努力使用Microsoft Bot Framework将我的机器人从V1移植到V3。

如果我找不到任何完整的下载示例,那么我遇到的HandleSystemMessage功能与前一个略有不同,并且无法找到解释如何定义它的示例代码或文档。

我知道应该是这样的:

private async void HandleSystemMessage(Activity message)
{
}

但是里面的代码怎么样?如何管理过去的事情?

由于

2 个答案:

答案 0 :(得分:3)

请参阅以下文档,了解将机器人从V1升级到V3 https://docs.botframework.com/en-us/support/upgrade-to-v3/#navtitle

Microsoft People's提供开发自己机器人的样本,请参阅以下链接包含一些样本,如Echo Bot,Sand Witch Bot和Pizza Bot等。 https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples

与V1 bot类似,V3 bot具有如下所示的活动类型

enter image description here

如果您想了解有关活动类型的更多信息,请参阅以下链接

https://docs.botframework.com/en-us/csharp/builder/sdkreference/activities.html

HandleSystemMessage(活动消息)方法的示例代码。

  private async Task<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)
        {
            // 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
            IConversationUpdateActivity conversationupdate = message;
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
            {
                var client = scope.Resolve<IConnectorClient>();
                if (conversationupdate.MembersAdded.Any())
                {
                    var reply = message.CreateReply();
                    foreach (var newMember in conversationupdate.MembersAdded)
                    {
                        if (newMember.Id != message.Recipient.Id)
                        {
                            reply.Text = $"Welcome {newMember.Name}! ";
                        }
                        else
                        {
                            reply.Text = $"Welcome {message.From.Name}";
                        }
                        await client.Conversations.ReplyToActivityAsync(message);
                    }
                }
            }
        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
            IContactRelationUpdateActivity update = message;
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
            {
                var client = scope.Resolve<IConnectorClient>();
                if (update.Action.ToLower() == "add")
                {
                    var reply = message.CreateReply();
                    reply.Text = $"Welcome to the XXXX bot! To start an conversation with this bot send **XXXX** or **XXXX** command.\r \n if you need help, send the **Help** command.";
                    await client.Conversations.ReplyToActivityAsync(reply);
                }
            }
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }

        return null;
    }

希望对你有所帮助

-Kishore

答案 1 :(得分:0)

它位于入门模板中,您可以下载here。为方便起见,我在这里复制了它。

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