Microsoft Bot Service-设置问候语

时间:2018-07-18 09:28:02

标签: azure azure-bot-service

今天,我想在用户进入聊天时向我的聊天机器人实施问候消息。我查看了一些教程,发现这似乎很容易。当我在Microsoft Azure Bot Service的WebChat上运行时,聊天机器人可以工作。这是显示我所做的屏幕截图。在第一张图片中,您可以看到我将代码放入ConversationUpdate活动中,而当我在终端中运行build.cmd命令时,它显示了一个错误,您可以在第二张图片中看到该错误。请记住,我是Microsoft Bot Service的新手。谁能告诉我我在做什么错?谢谢

https://prnt.sc/k7vl8h https://prnt.sc/k7vl1l

1 个答案:

答案 0 :(得分:0)

从您的第一个图像错误消息中,它指出您的“活动”未定义。您应该声明您的活动message并替换为activity

更新您的HandleSystemMessage方法,使其如下所示:

private Activity HandleSystemMessage(Activity activity)
{
    if (activity.Type == ActivityTypes.DeleteUserData)
    {
        // Implement user deletion here
        // If we handle user deletion, return a real message
    }
    else if (activity.Type == ActivityTypes.ConversationUpdate)
    {
        // Handle conversation state changes, like members being added and removed

        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}!";
                    client.Conversations.ReplyToActivityAsync(reply);
                }
            }
        }
    }
    else if (activity.Type == ActivityTypes.ContactRelationUpdate)
    {
        // Handle add/remove from contact lists
    }
    else if (activity.Type == ActivityTypes.Typing)
    {
        // Handle knowing tha the user is typing
    }
    else if (activity.Type == ActivityTypes.Ping)
    {
    }

    return null;
}