handling messages edited by user in Teams Bot
We have created a teams bot application using NodeJs SDK. Here user can reply to Bot's Questions. When the user edits his message in teams, none of the bot event is fired. How to handle editing user's message
Microsoft Teams
Microsoft Teams Development
-
LiweiTian-MSFT • 25,935 Reputation points • Microsoft External Staff
2024-06-14T01:30:06+00:00 Hi,
Teams tag is mainly focused on the general issue of Microsoft Teams troubleshooting. According to your description, your question is related to Teams Development which is not in our support scope. To better help you solve your problem, please add the Teams Development tag to your post. Thanks for your understanding and patience!
-
Prasad-MSFT • 8,851 Reputation points • Microsoft External Staff
2024-06-14T05:45:27.75+00:00 When you edit a message, the bot gets a notification of the edit message activity.
To get an edit message activity notification in a bot, you can override
OnTeamsMessageEditAsync
handler.Following is an example of an edit message activity notification using
OnTeamsMessageEditAsync
when a sent message is edited:protected override async Task OnTeamsMessageEditAsync(ITurnContext<IMessageUpdateActivity> turnContext, CancellationToken cancellationToken) { var replyActivity = MessageFactory.Text("message is updated"); await turnContext.SendActivityAsync(replyActivity, cancellationToken); }
-
RajeshPandian • 25 Reputation points
2024-06-14T09:48:55.12+00:00 If a user edits their message in the Bot's chat window (personal conversation), the edited message is not being updated in the Channel conversation. That is, the Channel conversation displays the original message sent by the user but not the updated message. We understand that the OnTeamsMessageEditAsync event is triggered when the message is updated by the user. However, we need to know how to obtain the message ID and how to update the message in the Channel conversation as well.
This is not related to bot's edit message but this is related to the scenario where the user edits his message.
Could you please provide the solution for this scenario
-
RajeshPandian • 25 Reputation points
2024-06-17T05:50:49.92+00:00 I want to update the user-edited message in the Bot's chat window to be reflected in the Channel conversation of the Team's Bot. Please reply to me as this is very urgent.
-
Prasad-MSFT • 8,851 Reputation points • Microsoft External Staff
2024-06-17T14:45:26.17+00:00 - Capture the Message ID: When the original message is sent to the channel, store the message ID returned by the Teams service.
- Handle the Edit Event: Implement the
OnTeamsMessageEditAsync
method in your bot to listen for edit events. - Update the Channel Message: Use the stored message ID to update the message in the channel using the
UpdateActivityAsync
method, passing in the new content.
-
RajeshPandian • 25 Reputation points
2024-06-17T15:02:14.07+00:00 The message is not being sent. The message is received by the bot from the user. The user may anytime edit any of the message that he sent to bot. So here the case of storing the messageID while sending the message does not occurs as we are not sending but receiving the message and any of the message may be edited. Please let me know how can I get the messageID and update the message in Channel when a message is edited in chat
-
RajeshPandian • 25 Reputation points
2024-06-18T11:20:34.4866667+00:00 This is the code I am using to capture and update the edited message of teams chat to get reflected in Teams channel. But I am getting 403 error. I could not store the previous messageID as I do not know which message the user would edit. I think that activity.id from the TurnContext could be the message ID. Please correct if the following code is wrong ==>
this.onMessageUpdate(async (context, next) => { appInsights.trackEvent("Message Start", { id: context.activity.from.aadObjectId }); const updateActivity = { type: 'message', id: context.activity.id, // Is this the ID of the message to update text: context.activity.text, channelId: context.activity.channelId, conversation: context.activity.conversation }; try { // Call updateActivity with the updated activity object await context.updateActivity(updateActivity); console.log('Message updated successfully.'); } catch (error) { console.error('Failed to update message:', error); } await next(); });
-
Prasad-MSFT • 8,851 Reputation points • Microsoft External Staff
2024-06-24T06:36:49.84+00:00 The
context.activity.id
should indeed be the ID of the message to update, but you need to ensure that the bot has the necessary permissions to edit messages in the channel. However, the 403 error indicates that there's a permissions issue.To resolve the permissions issue, make sure that:
- The bot is added as a member or owner of the channel where the message needs to be updated.
- The bot's app registration in Azure has the necessary API permissions for Microsoft Graph, such as
Chat.ReadWrite
for chats andChannelMessage.Edit
for channel messages. - You've granted consent for these permissions in the Azure portal.
Additionally, if you're trying to update a message in a different conversation from where the bot received the message update event, you'll need to use the conversation ID and service URL of the target channel. This might involve storing these details when the bot is first added to the channel or when the original message is sent.
Sign in to comment