Display Welcome Message in v4 Bot Framework Bot (C# + .Net Core Web Application)

前端 未结 3 483
花落未央
花落未央 2021-01-15 06:28

I had created a bot with v3 (C#) SDK and the welcome message used to work just fine without any sweat. And it still does for me in production. The code is handled in HandleS

3条回答
  •  借酒劲吻你
    2021-01-15 06:50

    Typically, the channel sends two conversation update events when the conversation is initialized - one for the bot and another for the user. The second one - the event for the user - is intended to trigger the welcome message. Unlike some other channels, Web Chat waits to send the second conversation update event until after the user messages the bot. Evidently, the welcome message won't be sent until after the first message. To workaround this issue, developers can send a back channel welcome event to the bot when the DirectLine connection is established and send a welcome message from the onEventAsync handler instead of onMembersAdded. For more details, take a look at the code snippets below.

    Bot Chat Code

    
    
      
        
        
    
      
      
        

    Bot Code - C#

    protected override async Task OnEventActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        if (turnContext.Activity.Name == "webchat/join") {
          await turnContext.SendActivityAsync("Welcome Message!");
        }
    }
    
    protected override async Task OnMembersAddedAsync(IList membersAdded, ITurnContext turnContext, CancellationToken cancellationToken)
    {
        if (turnContext.Activity.ChannelId != "webchat" && turnContext.Activity.ChannelId != "directline") {
    
            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync($"Hi there - {member.Name}. {WelcomeMessage}", cancellationToken: cancellationToken);
                    await turnContext.SendActivityAsync(InfoMessage, cancellationToken: cancellationToken);
                    await turnContext.SendActivityAsync(PatternMessage, cancellationToken: cancellationToken);
                }
            }
        }
    }
    

    Screen Capture

    Also note, Web Chat v0.11.4 is referred to as either Bot Chat or Web Chat v3. Sorry I got tripped up on that.

    Hope this helps!

提交回复
热议问题