Pages Menu
TwitterRssFacebook

Posted by on Jan 30, 2023 in Development, Microsoft Teams

How to: get notified when a user deletes, undeletes or edits a message to your bot

How to: get notified when a user deletes, undeletes or edits a message to your bot

The Microsoft Bot Framework has been around for a while now (since 2016 in fact!) and in that time there have been changes and additions to the systems it interacts with.

One of these in Microsoft Teams, which added the ability for users to delete, undelete and edit messages. These capabilities all came after the initial launch of Teams, and after Bot Framework started supporting Microsoft Teams conversations.

So, what happens when a user edits, deletes or undeletes a chat message sent to a bot?

By default, the answer is nothing. If you already have code in production which reacts to user messages and you haven’t done anything to support edits or deletes then nothing will happen when a user does this. Your code won’t break, but equally users might get confused.

If you do want to support these actions though, there is now a way to do this in Bot Framework. There are 3 new event handlers you can subscribe to, one for each of the actions:

  • user edits an existing message to the bot
  • user delete a message to the bot
  • user undeletes a previously deleted message to the bot

Using new event handlers for these actions is a clever implementation by the Bot Framework team because it’s entirely optional to implement, so it won’t break any existing code.

The three override event handlers are:

protected override async Task OnTeamsMessageEditAsync(ITurnContext<IMessageUpdateActivity> turnContext, CancellationToken cancellationToken)

protected override async Task OnTeamsMessageSoftDeleteAsync(ITurnContext<IMessageDeleteActivity> turnContext, CancellationToken cancellationToken) 

protected override async Task OnTeamsMessageUndeleteAsync(ITurnContext<IMessageUpdateActivity> turnContext, CancellationToken cancellationToken)

Exactly what you actually do within these methods is up to you. Depending on your bot you might need to update some external systems, or possibly rewind a conversation or revisit intent matching.

Do I really need to do this?

This is a good question, worth considering.

You might not previously thought about users editing and deleting message they send to bots, and your users might not have thought about it either. But, now that you HAVE thought about it, what you are going to do about it?

Testing to see what happens on edit/delete/undelete actions is a very good test to have in a battery of QA tests, and in all likelihood doing so might break your bot or cause unintended behavior.

I can also imagine the development meeting where developers throw their hands up at the amount of work that might be needed to respond to edits and deletes, which could very well be substantial. Is it worth the effort?

The answer in these cases: metrics. Let’s find out how often users actually do these things, before committing a load of development resource. If you already have good logging in your bot application (and you should, see my talk on this), then it would be trivial to override these three new event handlers and just log that they’ve been hit.

That will give the knowledge you need to understand whether this is a real-world problem or not, and whether it’s worth fixing. The good news: if it does need fixing, at least now you know how to fix it.

Ref: Update message

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.