Forum Discussion

Paul_By's avatar
Paul_By
Brass Contributor
Mar 13, 2025

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_By's avatar
    Paul_By
    Brass 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?

  • Paul_By's avatar
    Paul_By
    Brass 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. (--)

  • Paul_By

    Thank you for your inquiry about your Teams app development issue! To assist you better, could you please provide the following details? 

    1. Reproduction Steps: Please share the steps you took to encounter the issue. 
    2. Documentation Links: Any specific documentation you followed that relates to your problem. 
    3. Teams Client Version: What version of the Teams client are you using? 
    4. Code Snippets: Relevant code snippets that illustrate the issue. 
    5. 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. 

     

Resources