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

前端 未结 3 511
花落未央
花落未央 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 07:00

    In v4 of the Bot Framework SDK this functionality is handled by making your bot class inherit from the ActivityHandler class then overriding the OnMembersAddedAsync method.

    How this looks in practice is:

    ...
    
    public class MyBot : ActivityHandler
    {
        ...
    
            protected override async Task OnMembersAddedAsync(IList membersAdded, ITurnContext turnContext, CancellationToken cancellationToken)
            {
                foreach (var member in membersAdded)
                {
                    // Greet anyone that was not the target (recipient) of this message.
                    if (member.Id != turnContext.Activity.Recipient.Id)
                    {
                        var welcomeMessage = "Hello and welcome!";
    
                        await turnContext.SendActivityAsync(welcomeMessage, cancellationToken);
                    }
                }
            }
        ...
    }
    

    There are examples of how to do this here, here, and here.

    You may have to update the version of the Microsoft.Bot.Builder package that you are using.

提交回复
热议问题