Forum Discussion
Paul_By
Mar 13, 2025Brass Contributor
Microsoft.Bot.Connector SendToConversationAsync Failed - 'Forbidden'
Hi.
We use Microsoft.Bot.Connector (4.22.9) SendToConversationAsync for sending an activity to a conversation.
Most of them are processed successfully, but some fail with 'Operation returned an invalid status code 'Forbidden'.
How can I handle it? What is the reason?
Also, I have a second question about "blocked conversation." If the user blocks the conversation, I think SendToConversationAsync will fail, too. How can I handle this, and what kind of error will be raised?
- Paul_ByBrass Contributor
So, the example shows retry mode.
But ... What is my mistake (code above, MessageActivity)?Most of them are processed successfully, but some fails with 'Operation returned an invalid status code 'Forbidden'.
How can I handle it? What is the reason?
Also, I have a second question about "block bot conversation."If the user blocks the conversation, I think SendToConversationAsync will fail, too. How can I handle this, and what kind of error will be raised?
- Sayali-MSFT
Microsoft
Could you please check your configurations and verify that your ServiceURL is still valid or changed.
The ServiceUrl in the ConversationReference can change. Always use the latest ServiceUrl provided in the incoming activity when responding to a user.
For further information on this please refer this sample:Microsoft-Teams-Samples/samples/bot-conversation/csharp/Bots/TeamsConversationBot.cs at main · OfficeDev/Microsoft-Teams-Samples (github.com)
- Sayali-MSFT
Microsoft
Hello @Paul_By, meantime, could you please refer the below sample-Microsoft-Teams-Samples/samples/bot-proactive-messaging/csharp/proactive-cmd/Program.cs at 439640d4e37184ff60d70661b1e4c58b4611fd81 · OfficeDev/Microsoft-Teams-Samples
- Paul_ByBrass Contributor
Thank you
Azure Function:
var appCredentials = new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword); var uri = new Uri(conversationReferenceServiceUrl); var connectorClient = new ConnectorClient(uri, appCredentials); var messageActivity = Activity.CreateMessageActivity() as Activity; messageActivity.Type = ActivityTypes.Message; messageActivity.Text = message; messageActivity.Timestamp = DateTimeOffset.Now; messageActivity.DeliveryMode = DeliveryModes.Notification; messageActivity.ServiceUrl = conversationReferenceServiceUrl; messageActivity.Conversation = new ConversationAccount() { Role = "Bot", Id = conversationReferenceConversationId, TenantId = conversationReferenceTenantId }; messageActivity.Recipient = new ChannelAccount() { Id = conversationReferenceUserId, Name = conversationReferenceUserName }; messageActivity.Attachments.Add(attachment); await connectorClient.Conversations.SendToConversationAsync(messageActivity);
Teams Client Version: What version of the Teams client are you using (--)
Documentation Links: Any specific documentation you followed that relates to your problem. (--) - Sayali-MSFT
Microsoft
Thank you for your inquiry about your Teams app development issue! To assist you better, could you please provide the following details?
- Reproduction Steps: Please share the steps you took to encounter the issue.
- Documentation Links: Any specific documentation you followed that relates to your problem.
- Teams Client Version: What version of the Teams client are you using?
- Code Snippets: Relevant code snippets that illustrate the issue.
- Manifest Package: If applicable, please share the manifest package for your app.
This information will help us better understand the situation and provide a more accurate response.