MicrosoftDocs / msteams-docs

Source for the Microsoft Teams developer platform documentation.
https://aka.ms/teamsdev
Creative Commons Attribution 4.0 International
277 stars 500 forks source link

ActivityNotFoundInConversation when updating Adaptive Card #6139

Open MajaEffenberg opened 2 years ago

MajaEffenberg commented 2 years ago

Hi there,

We got unusual errors from time to time when updating adaptive card in MS Teams:

{
    "error": {
        "code": "ActivityNotFoundInConversation",
        "message": "Message does not exist in the thread."
    }
}

We always read replyToId from TurnContext activity, and pass the same Id when we updating messages. In most cases, updating adaptive cards works, but it looks like errors appear randomly around every 1000 adaptive cards. And they appear for different users and adaptive cards. We also don't see any option for the user to be able to remove the card to be updated.

So we would like to know when and why this error may appear?

ghost commented 2 years ago

Hi MajaEffenberg! Thank you for bringing this issue to our attention. We will investigate and if we require further information we will reach out in one business day. Please use this link to escalate if you don't get replies.

Best regards, Teams Platform

Meghana-MSFT commented 2 years ago

Could you please share the repro steps and the sample if you are referring to any?

MajaEffenberg commented 2 years ago

Updating the adaptation card in steps:

  1. When message comes after clicking button in adaptive card, we read in TeamsActivityHandler.onMessage replyMessageId from TurnContext.activity.replyMessageId
  2. Then we build a new view of the updated adaptive tab using CardFactory.adaptiveCard()
  3. Next we create the attachmentActivity using MessageFactory.attachment() with this new adaptive card
  4. As the value of id we add in attachmentActivity the previously read replyMessageId attachmentActivity.id = replyMessageId
  5. In the end we updating activity by calling TurnContext.updateActivity(attachmentActivity)

Such a flow of updating AdaptiveCards works most of the time. The error appears randomly around every 1000 updated cards.

MajaEffenberg commented 2 years ago

Is there any update on this issue?

Meghana-MSFT commented 2 years ago

We are checking this, I will share an update soon.

Meghana-MSFT commented 2 years ago

@MajaEffenberg - We looped the above scenario for 1900 and 3000 cards and it worked fine. The cards got updated successfully and we did not get any error. Could you please share any other specific details so that we can have a local repro of this?

Meghana-MSFT commented 2 years ago

@MajaEffenberg - We looped the above scenario for 1900 and 3000 cards and it worked fine. The cards got updated successfully and we did not get any error. Could you please share any other specific details so that we can have a local repro of this?

sueess commented 2 years ago

Hi, I'm a coworker of @MajaEffenberg. We tried to analyze the problem more in detail. The error appears in different scenarios. Here a specific one. Have a look at our log messages:

Bildschirmfoto 2022-06-22 um 15 56 55

Comments to the logs: It appears that a user clicks twice on the same button (which is usually not possible, since the button is blocked after clicking it once). We can see that both messages have the same TurnContext.activity.value and they arrived on the server inbetween a couple of milliseconds. We have no idea how this scenario can be reproduced. How is it possible, that someone clicks twice on a button inbetween a couple of milliseconds?

After that strange behaviour also our two update calls fail with the error message:{"error":{"code":"ActivityNotFoundInConversation","message":"Message does not exist in the thread."}}. However you can see, that the ids are the same.

There is definitely something buggy...

Meghana-MSFT commented 2 years ago

Thanks for sharing the logs. We will take a look at it.

Meghana-MSFT commented 2 years ago

@sueess, @MajaEffenberg - Engineering team is checking the logs provided. Could you please help answering the below questions?

(1) is the time given in the log UTC time? (2) Is your bot id 4112cc9a-d683-4ffd-b0bc-2013af7c7497? (3) Does this issue happen in bot:user chat only?

How is it possible, that someone clicks twice on a button inbetween a couple of milliseconds? ==> Assuming it is the issue happening between a user and a bot, it is possible that a human user double clicks the button instead of a single click when one interacts with the adaptive card using their mouse.

sueess commented 2 years ago

@Meghana-MSFT

(1) Yes it is UTC time (2) Yes, correct (3) If by bot:user you mean personal scope of bot, then yes, because we only support personal scope (no group or teams chats)

How is it possible, that someone clicks twice on a button inbetween a couple of milliseconds? ==> Assuming it is the issue happening between a user and a bot, it is possible that a human user double clicks the button instead of a single click when one interacts with the adaptive card using their mouse.

We tried to do that many times, but we could not reproduce it. The button is blocked right after the first click (which is good!). If you know how to do that, let us know, so we can check again.

We get the error every day a couple of times. In case you need our support, feel free to organize an call. Thanks for your effort!

Meghana-MSFT commented 2 years ago

@sueess - Could you please help by sharing the video recording of the above mentioned issue, as we do not have a local repro of the same.

sueess commented 2 years ago

@Meghana-MSFT We cannot reproduce that problem ourself. We do not know how it happens. But also, what do you expect to see on that video? --> The card will not be updated.

We get everyday multiple errors on that. We see that in our logs and also in the Azure logs. But as mentioned before, it works 1000 times an maybe one time it does not. So it must be an internal Microsoft Teams problem.

MajaEffenberg commented 2 years ago

The ActivityNotFoundInConversation error reappears in our application when updating adaptive card in MS Teams using TurnContext.updateActivity().

Request information:

Our botId: 4112cc9a-d683-4ffd-b0bc-2013af7c7497

Meghana-MSFT commented 2 years ago

@MajaEffenberg - Thanks for the additional information. Team is looking into the issue.

Meghana-MSFT commented 2 years ago

@MajaEffenberg - Please find the Engineering teams findings -

On the Teams Backend service side, we do not see the two messages that are mentioned in the log in the database. As such, when the bot tries to update those messages, the bot gets errors saying "no message found".

Teams Engineering team is trying to understand why those messages do not exist in the database as it seems the bot sent those messages from user's perspective.

Based on this data, Engineering team needs some more information. Could you please help us to identify when the bot sent these messages that failed to be updated (1657049372481 & 1657049692780) in the logs?

MajaEffenberg commented 2 years ago

@Meghana-MSFT Thanks for the information.

Timestamps in UTC when the bot sent these messages are:

MajaEffenberg commented 1 year ago

Is there any update on this issue?

Meghana-MSFT commented 1 year ago

@MajaEffenberg - Engineering team has been investigating the logs and has the below understanding of the scenario. Pasting below the observations made by the team. Could you please confirm if the understanding of bot scenario is correct?

Details: Per the understanding, this is the customer's bot scenario -

  1. The bot sends an adaptive card in 1:1 thread (in other words, the bot calls SendActivity API).
  2. A user clicks a button (this action sends an invoke request to the bot)
  3. Once the bot receives the invoke request, it extracts replyToId from the turn context, and uses it to update the adaptive card (in other words, the bot calls EditActivity API).

In the (1) step, when the bot calls SendActivity, the API returns message id as a response. This is the id of the adaptive card. This id needs to be the same as replyToid in the invoke request in (3) step.

But for 1657049372481 & 1657049692780 case, this is not true. For instance, the customer says that their bot sent 1657049372481 in 07-05-2022 19:25:19 in UTC. When I checked the log, the response id of this send Activity was 165704911967. This means the id of the adaptive card that the bot tried to update is 165704911967, not 1657049372481

AFAIK, replyToId in invoke request is stamped by the Teams Client. It is possible that a user's Teams Client somehow stamps an invalid message id due to a race condition.

MajaEffenberg commented 1 year ago

@Meghana-MSFT Thanks for the information.

Yes, your descriptions in the points about customer bot scenario are correct.

We based our adaptive card updates on the Teams documentation, where it says "To update the existing card on button selection, you can use ReplyToId of incoming activity." For this reason we use ReplyToId to update the adaptive card, not the id we get from response after sending SendActivity.

Two days ago this bug appears again and we were able to logged response message from Send Activity which includes message id:

  1. Bot sends a message with adaptive card to the user at 17.7.2022 - 21:23:34 (UTC) using SendActivity. As a response we got message id: 1658093014281
  2. A user clicks a button inside sent adaptive card
  3. Bot receives the invoke request 17.7.2022 - 21:31:31 (UTC) with replyToId 1658093446508 and uses this id to update the adaptive card. Unfortunately he gets an error during the update:
    {
    "error": {
        "code": "ActivityNotFoundInConversation",
        "message": "Message does not exist in the thread."
    }
    }

As you can see above, the id of the message we received in response after sending this message is different than the id we received in replyToId. And the user clicked the button exactly in this adaptive card we sent earlier and the response id after sending was 1658093014281.

Meghana-MSFT commented 1 year ago

Thank you for confirming. While the engineering team is investigating deeper into this, please note that

(1) The bot gets the exception as it is somehow getting an invalid message id from the invoke request, and therefore the bot is trying to update the messages that do not exist and

(2) Teams engineering team is currently investigating why an invalid message id is coming to the backend from the begin with.

MajaEffenberg commented 1 year ago

Any news on this issue?

Meghana-MSFT commented 1 year ago

The team is still investigating the issue. We will keep you posted on the updates.

sschoeb commented 7 months ago

@Meghana-MSFT We again have multiple occurrences of this issue. We are randomly not able to update activities. As you can imagine, this will always lead to a terrible user experience.

Is there any update on this? Do you need any further information from us?

Meghana-MSFT commented 7 months ago

@sschoeb - Could you please let us know if you are facing this issue in Teams classic or new Teams 2.1?

Meghana-MSFT commented 7 months ago

@sschoeb - Could you please let us know if you are facing this issue in Teams classic or new Teams 2.1?

sschoeb commented 7 months ago

@Meghana-MSFT Sorry, somehow I missed the first notification. I cannot say which Teams Client we have, we just get the exception back when trying to update the Activity. Is there any way I could get this information?

Meghana-MSFT commented 7 months ago

@sschoeb - Can you please share your Teams version with us? If the toggle button Try new Teams button is turned means you are using new Teams 2.1 else its Teams Classic. image

sschoeb commented 7 months ago

@Meghana-MSFT Thanks for your reply. Sorry, didn't expect that you want to know my teams version. I'm using the new Teams. But I'm quite sure that this issue is not related to my personal teams, as I cannot reproduce (and was never able to, in the old and the new teams) this issue locally. This happens randomly in our bot for any customer uf us. Therefore I cannot answer your questions on which Teams-Versions the issue happens, as I cannot know that. The only thing I have is this "ActivityNotFoundInConversation"-Exception I get back when we try to update an activity.

Let me know if I can provide you with any further information, but I think @MajaEffenberg did already a very good analysis of the problem.

Meghana-MSFT commented 7 months ago

Thank you, we will share this with engineering team.

sschoeb commented 7 months ago

@Meghana-MSFT The engineering team is investigating since August 2022. And we still have this issue?

This kind of issues does randomly destroy the user experience of a bot conversation within teams and therefore also the user experience of teams itself. I hope this does raise the priority of the issue as it seems there is currently no priority at all :/