Share via

Sending a Message to the Last Thread in a Channel Using TeamsFx Bot Results in 'Forbidden' Error

abbas izadipour 210 Reputation points
2024-04-11T17:51:32.94+00:00

I have developed a Microsoft Teams bot application using TeamsFx. This bot communicates with users by sending messages to the channels in the teams where the bot is installed. Whenever the bot sends a message to a channel, the channel displays the new message as a new post. Now, I want the bot to be able to recognize the last thread message in the channel and send the new message to the last thread message. So far, I have written code that enables the bot to find the message ID of the last thread message. Then, I wrote the following code snippet to send the message to the last thread message:

const sendMessageResponse = await fetch(https://graph.microsoft.com/v1.0/teams/${teamId}/channels/${channel.info.id}/messages/${lastChatThread.id}/replies, {

                method: 'POST',

                headers: {

                    'Authorization': `Bearer ${token}`,

                    'Content-Type': 'application/json'

                },

                body: JSON.stringify({

                    body: {

                        contentType: 'html',

                        content: requestBody.text

                    }

                })

            });

However, every time I run the application to send a new message, I encounter the following error: 'Error processing task: Error: Failed to send message: Forbidden'.

Also, here are all the API permissions that I consented to my application:

Channel.Create Application Create channels Yes
Channel.Create Application Create channels Yes
Granted for 2cr0fw
Granted for 2cr0fw
Channel.ReadBasic.All Application Read the names and descriptions of all channels Yes
Channel.ReadBasic.All Application Read the names and descriptions of all channels Yes
Granted for 2cr0fw
Granted for 2cr0fw
ChannelMessage.Read.All Application Read all channel messages Yes
ChannelMessage.Read.All Application Read all channel messages Yes
Granted for 2cr0fw
Granted for 2cr0fw
ChannelMessage.Send Delegated Send channel messages No
ChannelMessage.Send Delegated Send channel messages No
Granted for 2cr0fw
Granted for 2cr0fw
ChannelSettings.Read.All Application Read the names, descriptions, and settings of all channels Yes
ChannelSettings.Read.All Application Read the names, descriptions, and settings of all channels Yes
Granted for 2cr0fw
Granted for 2cr0fw
Chat.ReadWrite.All Application Read and write all chat messages Yes
Chat.ReadWrite.All Application Read and write all chat messages Yes
Granted for 2cr0fw
Granted for 2cr0fw
Group.ReadWrite.All Delegated Read and write all groups Yes
Group.ReadWrite.All Delegated Read and write all groups Yes
Granted for 2cr0fw
Granted for 2cr0fw
Group.ReadWrite.All Application Read and write all groups Yes
Group.ReadWrite.All Application Read and write all groups Yes
Granted for 2cr0fw
Granted for 2cr0fw
Team.ReadBasic.All Application Get a list of all teams Yes
Team.ReadBasic.All Application Get a list of all teams Yes
Granted for 2cr0fw
Granted for 2cr0fw
TeamSettings.Read.All Application Read all teams' settings Yes
TeamSettings.Read.All Application Read all teams' settings Yes
Granted for 2cr0fw
Granted for 2cr0fw
TeamSettings.ReadWrite.All Application Read and change all teams' settings Yes
TeamSettings.ReadWrite.All Application Read and change all teams' settings Yes
Granted for 2cr0fw
Granted for 2cr0fw
TeamworkAppSettings.ReadWrite.All Application Read and write Teams app settings Yes
TeamworkAppSettings.ReadWrite.All Application Read and write Teams app settings Yes
Granted for 2cr0fw
Granted for 2cr0fw

Recently, I added two delegated permissions to my list, as shown, but these newly added permissions did not resolve the 'Forbidden' error issue.

Microsoft Teams | Development
Microsoft Teams | Development

Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.