Welcome message not visibile in Webchat,but work i

2019-07-05 05:39发布

问题:

IConversationUpdateActivity update = message;
        using (var scope = Microsoft.Bot.Builder.Dialogs.Internals.DialogModule.BeginLifetimeScope(Conversation.Container, message))
        {
            var client = scope.Resolve<IConnectorClient>();
            if (update.MembersAdded.Any())
            {
                foreach (var newMember in update.MembersAdded)
                {
                    if (newMember.Id != message.Recipient.Id)
                    {
                        var reply = message.CreateReply();
                        reply.Text = $"Welcome {newMember.Name}!";
                        client.Conversations.ReplyToActivityAsync(reply);
                    }
                }
            }
        }

I'm new to ChatBot development using Microsoft BotFramework.

I have register and deployed a simple bot that was working fine with emulator (i.e bot says welcome to my simple bot),but when i used WebChat no welcome greeting was displayed,instead when user type Hi or any text after that the greeting message is displayed. had gone to various tutorial and solution but not getting the exact cause. i'm using Microsoft.Bot.Builder v3.12

回答1:

I just tested your code and got the same behavior. The odd part is that there seems to only be one conversation update for when the bot joins, rather than one for the bot and one for the user. I am looking into this. The following code is working if you would like to give it a try:

IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
    ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

    foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
    {
        // if the bot is added, then
        if (member.Id == iConversationUpdated.Recipient.Id)
        {
            var reply = ((Activity)iConversationUpdated).CreateReply(
                $"Hi! I'm Botty McBotface and this is a welcome message");
            connector.Conversations.ReplyToActivityAsync(reply);
        }
    }
}