Proactive notifications via MS Teams fails for user's who don't chat with the bot for a long time

30 Views Asked by At

We have built a bot using Microsoft Bot framework sdk v4. We have also implemented the feature of sending proactive notifications to users via MS Teams. Now as per the Microsoft recommendation for proactive notification, we are save the user's conversation ID in the database and whenever we need to trigger notifications we retrieve the conversation ID and then trigger notification.

Everything works fine except for some users who has not had a chat with the teams bot for a very long time say 6 -10 months.

When we investigated we found that the chat history also has disappeared from teams. So we asked these users to ping the bot again and on doing so a new conversation reference object gets added to the database due to the below code after which we are able to sent notifications again to those users.

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
    {
        foreach (var member in membersAdded)
        {
            if (member.Id != turnContext.Activity.Recipient.Id && !(turnContext.Activity.ChannelId == "directline"))
            {
                var userProfile = await _userStateAccessors.GetAsync(turnContext, () => new UserProfile());
                var reply = await GenerateWelcomeMessage(turnContext);
                await turnContext.SendActivityAsync(reply);

                if (turnContext.Activity.ChannelId == "msteams")
                {
                    //-- if the user email is not identified or its value is null, call AAD to set the email in the user profile
                    if (string.IsNullOrEmpty(userProfile.Email))
                    {
                        bool isAuthorized = await authorization.Authorize(turnContext?.Activity?.From?.AadObjectId, turnContext, null);
                    }
                    Task.Run(() => _pushNotification.AddConversationReferenceToDB(turnContext.Activity as Activity, userProfile.Email));
                }                   
            }
        }
    }

So what we wanted to understand is why the conversation id becomes inactive when a user doesn't chat with the bot for a long time? What is the exact time in days/hours after which the conversation ID becomes inactive? Is there a way to prevent this?

0

There are 0 best solutions below