首页 > 解决方案 > Azure Bot Framework - Direct Line 集成在用户键入内容之前不会显示介绍性消息

问题描述

我已经部署了一个 Azure 机器人,现在我正在尝试使其可用于测试。

我已按照此处的说明将机器人嵌入到单独的 Azure Web 应用程序(在 html 页面内) ,但在您键入内容之前不会显示介绍性消息。

所以要测试我的测试人员必须输入“go”(或类似的)来开始测试机器人。

使用模拟器测试 localhost 是可以的。

通过 Azure 门户进行测试也可以(但我的测试人员无法访问 Azure 门户)。

标签: azurebotframework

解决方案


在您的 Bot 文件中,添加以下代码:

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            try
            {
                foreach (var member in membersAdded)
                {
                    if (member.Id != turnContext.Activity.Recipient.Id)
                    {
                        var Userid=turnContext.Activity.From.Name;
                        UserProfile userProfile = await _botStateService.UserProfileAccessors.GetAsync(turnContext, () => new UserProfile());
                        userProfile.name = Userid;
                        await _botStateService.UserProfileAccessors.SetAsync(turnContext, userProfile);
                        await turnContext.SendActivityAsync(MessageFactory.Text($" Hi, bot here "), cancellationToken);
                    }
                }
            }

推荐阅读