The following video demonstrates how a bot simplifies the process of sending and receiving files with ease and efficiency:
Use the Graph APIs
Post messages with card attachments that refer to existing SharePoint files, using the Graph APIs for OneDrive and SharePoint. To use the Graph APIs, obtain access to either of the following through the standard OAuth 2.0 authorization flow:
A user's OneDrive folder for personal and groupchat files.
Alternately, you can send files to and receive files from a bot using the Teams bot APIs.
Use the Teams bot APIs
Teams bot APIs work only in the personal context. They don't work in the channel or groupchat context.
Using Teams APIs, the bot can directly send and receive files with users in the personal context, also known as personal chats. Implement features, such as expense reporting, image recognition, file archival, and e-signatures involving the editing of file content. Files shared in Teams typically appear as cards and allow rich in-app viewing.
The next sections describe how to send file content as direct user interaction, like sending a message. This API is provided as part of the Teams bot platform.
Configure the bot to support files
To send and receive files in the bot, set the supportsFiles property in the manifest to true. This property is described in the bots section of the manifest reference.
The definition looks like this, "supportsFiles": true. If the bot does not enable supportsFiles, the features listed in this section do not work.
Receive files in personal chat
When a user sends a file to the bot, the file is first uploaded to the user's OneDrive for business storage. The bot then receives a message activity notifying the user about the user upload. The activity contains file metadata, such as its name and the content URL. The user can directly read from this URL to fetch its binary content.
Message activity with file attachment example
The following code shows an example of message activity with file attachment:
The following table describes the content properties of the attachment:
Property
Purpose
downloadUrl
OneDrive URL for fetching the content of the file. The user can issue an HTTP GET directly from this URL.
uniqueId
Unique file ID. This is the OneDrive drive item ID, in case the user sends a file to the bot.
fileType
Type of file, such as .pdf or .docx.
As a best practice, acknowledge the file upload by sending a message back to the user.
Upload files to personal chat
To upload a file to a user:
Send a message to the user requesting permission to write the file. This message must contain a FileConsentCard attachment with the name of the file to be uploaded.
If the user accepts the file download, the bot receives an invoke activity with a location URL.
To transfer the file, the bot performs an HTTP POST directly into the provided location URL.
Optionally, remove the original consent card if you do not want the user to accept further uploads of the same file.
Message requesting permission to upload
The following desktop message contains a simple attachment object requesting user permission to upload the file:
The following mobile message contains an attachment object requesting user permission to upload the file:
JSON
{
"attachments": [{
"contentType": "application/vnd.microsoft.teams.card.file.consent",
"name": "file_example.txt",
"content": {
"description": "<Purpose of the file, such as: this is your monthly expense report>",
"sizeInBytes": 1029393,
"acceptContext": {
},
"declineContext": {
}
}
}]
}
The following table describes the content properties of the attachment:
Property
Purpose
description
Describes the purpose of the file or summarizes its content.
sizeInBytes
Provides the user an estimate of the file size and the amount of space it takes in OneDrive.
acceptContext
Additional context that is silently transmitted to the bot when the user accepts the file.
declineContext
Additional context that is silently transmitted to the bot when the user declines the file.
Invoke activity when the user accepts the file
An invoke activity is sent to the bot if and when the user accepts the file. It contains the OneDrive for Business placeholder URL that the bot can then issue a PUT to transfer the file contents. For information on uploading to the OneDrive URL, see upload bytes to the upload session.
The following code shows an example of a concise version of the invoke activity that the bot receives:
After uploading a file to the user's OneDrive, send a confirmation message to the user. The message must contain the following FileCard attachment that the user can select, either to preview or open it in OneDrive, or download locally:
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Platform Docs feedback
Platform Docs is an open source project. Select a link to provide feedback:
Demonstrate skills to plan, deploy, configure, and manage Microsoft Teams to focus on efficient and effective collaboration and communication in a Microsoft 365 environment.