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

前端 未结 3 502
花落未央
花落未央 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:54

    tdurnford's answer didn't work for me. I'm using a new v4 bot (no migration) and embedding iframe into Sharepoint. OnEventActivityAsync just doesn't trigger. I get a conversation update with my bot being a MembersAdded so I did this:

        protected override async Task OnConversationUpdateActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)
        {
            await base.OnConversationUpdateActivityAsync(turnContext, cancellationToken);
    
            if (turnContext.Activity.ChannelId == "webchat" && turnContext.Activity.MembersAdded?.FirstOrDefault(m => m?.Name == Configuration["BotName"]) != null)
            {
                await SendWelcomeMessageAsync(turnContext, cancellationToken);
            }
        }
    

    BotName is "Bot" for local development and the app name for azure deployed apps. SendWelcomeMessage is my own method. Also as above, I added a check in OnMembersAddedAsync:

    protected override async Task OnMembersAddedAsync(IList membersAdded, ITurnContext turnContext, CancellationToken cancellationToken)
        {
            if (turnContext.Activity.ChannelId == "webchat" || turnContext.Activity.ChannelId == "directline")
                return;
    
            foreach (var member in membersAdded)
            {
                // Greet anyone that was not the target (recipient) of this message.
                // To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await SendWelcomeMessageAsync(turnContext, cancellationToken);
                }
            }
        }
    

提交回复
热议问题