How to add upload files features in a teams agent in Python

Amya Singhal 0 Reputation points
2025-10-17T21:12:20.74+00:00

I want to have a feature where the user can upload files in a teams agent that I am trying to build. How do I do that?

Microsoft Teams | Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Teddie-D 6,630 Reputation points Microsoft External Staff Moderator
    2025-10-18T02:15:24.4+00:00

    Hi @Amya Singhal 

    Thank you for posting your question in the Microsoft Q&A forum. 

    To add an upload feature in Teams agent using Python, you can follow these steps: 

    1.Enable file support in the manifest 
    To send and receive files in the bot, set the supportsFiles property in the manifest to true.
    For example:    

     "bots": [ 
            { 
                "botId": "your-bot-Id", 
                "scopes": [ 
                    "personal" 
                ], 
                "supportsFiles": true 
            } 
        ] 
    

    This allows your bot to receive file attachments in chats. 
    Please note that file upload is supported only in personal scope for Teams bots. It is not available in channels or group chats.
    Reference: Bots to Send and Receive Files - Teams | Microsoft Learn 

    2.When a user uploads a file, Teams sends an attachment object in the activity payload. 
    You can download the file using its contentUrl and a valid bot access token. 
    Here's an example implementation in Python:

    from botbuilder.core import ActivityHandler, TurnContext
    from botframework.connector.auth import MicrosoftAppCredentials
    import requests
    import os
    
    # Use environment variables for security
    APP_ID = os.getenv("MicrosoftAppId")
    APP_PASSWORD = os.getenv("MicrosoftAppPassword")
    
    class FileUploadBot(ActivityHandler):
        async def on_message_activity(self, turn_context: TurnContext):
            if turn_context.activity.attachments:
                # Get OAuth token using MicrosoftAppCredentials
                creds = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
                access_token = await creds.get_access_token()
    
                for attachment in turn_context.activity.attachments:
                    file_name = attachment.name
                    content_url = attachment.content_url
    
                    # Download file from Teams using contentUrl
                    headers = {"Authorization": f"Bearer {access_token}"}
                    response = requests.get(content_url, headers=headers)
    
                    if response.status_code == 200:
                        with open(file_name, "wb") as f:
                            f.write(response.content)
                    else:
                        await turn_context.send_activity(
                            f"Failed to download '{file_name}'. Status code: {response.status_code}"
                        )
    
                await turn_context.send_activity("File(s) received and saved successfully.")
            else:
                await turn_context.send_activity("Please upload a file.")
    

    Reference: Teams File Upload Bot - Code Samples | Microsoft Learn 

    I hope this helps. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".    

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


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.