重要
本文基于 v3 Bot Framework SDK。 如果你要查找 SDK 版本 4.6 或更高版本的当前文档,请参阅 对话机器人 部分。
可通过两种方法向机器人发送文件或从机器人发送文件:
- 使用 Microsoft Graph API。 此方法适用于 Teams 中所有范围内的机器人:
personalchannelgroupchat
- 使用 Teams API。 它们仅支持一个上下文中的文件:
personal
使用 Microsoft Graph API
可以使用适用于 OneDrive 和 SharePoint 的 Microsoft Graph API 发布引用现有 SharePoint 文件的卡附件的邮件。 使用 Graph API 需要获取对用户的 OneDrive 文件夹 (personal 和 groupchat 文件) 的访问权限,或者对通过标准 OAuth 2.0 授权流) 的文件 (团队频道中的文件的 channel 访问权限。 此方法适用于所有 Teams 范围。
使用 Teams 机器人 API
注意
此方法仅在上下文中 personal 有效。 它在 OR groupchat 上下文中channel不起作用。
机器人可以使用 Teams API 直接在上下文 personal 中与用户发送和接收文件,也称为个人聊天。 这使您可以实现费用报告、图像识别、文件存档、电子签名以及其他涉及直接操作文件内容的场景。 在 Teams 中共享的 Files 通常显示为卡片,并允许丰富的应用内查看。
以下部分介绍如何执行此操作以通过直接用户交互(例如发送消息)发送文件内容。 此 API 作为 Microsoft Teams 机器人平台的一部分提供。
将机器人配置为支持文件
若要在机器人中发送和接收文件,必须将清单true中的属性设置为 supportsFiles 。 清单参考的 [bots]/microsoft-365/extensibility/schema/root-bots#supportsfiles) 部分对此属性进行了介绍。
定义将如下所示: "supportsFiles": true。 如果机器人未启用 supportsFiles,则以下功能将不起作用。
在个人聊天中接收文件
当用户将文件发送到机器人时,该文件会首先上传到用户的 OneDrive for Business 存储中。 然后,机器人将收到一个消息活动,通知你用户上传。 活动包含文件元数据,例如其名称和内容 URL。 可以直接从此 URL 读取以提取其二进制内容。
带文件附件的邮件活动示例
{
"attachments": [{
"contentType": "application/vnd.microsoft.teams.file.download.info",
"contentUrl": "https://contoso.sharepoint.com/personal/johnadams_contoso_com/Documents/Applications/file_example.txt",
"name": "file_example.txt",
"content": {
"downloadUrl" : "https://download.link",
"uniqueId": "1150D938-8870-4044-9F2C-5BBDEBA70C9D",
"fileType": "txt",
"etag": "123"
}
}]
}
下表描述了附件的内容属性:
| 属性 | 用途 |
|---|---|
downloadUrl |
用于提取文件内容的 OneDrive URL。 可以直接从此 URL 发出。HTTP GET |
uniqueId |
唯一文件 ID。 对于用户向机器人发送文件的情况,这将是 OneDrive 驱动器项 ID。 |
fileType |
文件扩展名类型,如 pdf 或 docx。 |
作为最佳做法,应通过发回消息给用户来确认文件上传。
将文件上传到个人聊天
将文件上传到用户涉及以下步骤:
- 向请求文件写入权限的用户发送消息。 此消息必须包含带有
FileConsentCard要上传的文件名称的附件。 - 如果用户接受文件下载,机器人将收到一个带有位置 URL 的 调用 活动。
- 为了传输文件,机器人会直接对提供的位置 URL 执行一个操作
HTTP POST。 - 或者,如果您不想允许用户接受同一文件的进一步上传,则可以移除原始同意卡。
请求上传权限的消息
此桌面消息包含一个简单的附件对象,请求用户权限上传文件:
此移动邮件包含一个附件对象,请求用户权限上传文件:
{
"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": {
}
}
}]
}
下表描述了附件的内容属性:
| 属性 | 用途 |
|---|---|
description |
文件的描述。 可以向用户显示以描述其目的或总结其内容。 |
sizeInBytes |
向用户提供文件大小及其在 OneDrive 中占用的空间量的估计值。 |
acceptContext |
当用户接受文件时,将静默传输到机器人的其他上下文。 |
declineContext |
当用户拒绝文件时,将无提示传输到机器人的其他上下文。 |
当用户接受文件时调用活动
如果用户接受文件,则会向机器人发送调用活动。 它包含 OneDrive for Business 占位符 URL,然后机器人可以向其发出 A PUT 来传输文件内容。 有关上传到 OneDrive URL 的信息,请阅读此文章: 将字节上传到上传会话。
以下示例显示了机器人接收的调用活动的删节版本:
{
...
"name": "fileConsent/invoke",
"value": {
"type": "fileUpload",
"action": "accept",
"context": {
},
"uploadInfo": {
"contentUrl": "https://contoso.sharepoint.com/personal/johnadams_contoso_com/Documents/Applications/file_example.txt",
"name": "file_example.txt",
"uploadUrl": "https://upload.link",
"uniqueId": "1150D938-8870-4044-9F2C-5BBDEBA70C8C",
"fileType": "txt",
"etag": "123"
}
}
}
同样,如果用户拒绝文件,机器人将收到具有相同总体活动名称的以下事件:
{
"name": "fileConsent/invoke",
"value": {
"type": "fileUpload",
"action": "decline",
"context": {
}
}
}
通知用户已上传的文件
将文件上传到用户的 OneDrive 后,无论是使用上述机制还是使用 OneDrive 用户委派的 API,都应向用户发送确认消息。 此邮件应包含用户可以 FileCard 选择的附件,可预览该附件、在 OneDrive 中打开附件或本地下载。
{
"attachments": [{
"contentType": "application/vnd.microsoft.teams.card.file.info",
"contentUrl": "https://contoso.sharepoint.com/personal/johnadams_contoso_com/Documents/Applications/file_example.txt",
"name": "file_example.txt",
"content": {
"uniqueId": "1150D938-8870-4044-9F2C-5BBDEBA70C8C",
"fileType": "txt",
}
}]
}
下表描述了附件的内容属性:
| 属性 | 用途 |
|---|---|
uniqueId |
OneDrive/SharePoint 驱动器项 ID。 |
fileType |
文件类型,例如 pdf 或 docx。 |
C 语言中的基本示例
以下示例演示如何在机器人的对话框中处理文件上传和发送文件同意请求:
// This sample dialog shows two simple flows:
// 1) A silly example of receiving a file from the user, processing the key elements,
// and then constructing the attachment and sending it back.
// 2) Creating a new file consent card requesting user permission to upload a file.
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var replyMessage = context.MakeMessage();
Attachment returnCard;
var message = await result as Activity;
// Check to see if the user is sending the bot a file.
if (message.Attachments != null && message.Attachments.Any())
{
var attachment = message.Attachments.First();
if (attachment.ContentType == FileDownloadInfo.ContentType)
{
FileDownloadInfo downloadInfo = (attachment.Content as JObject).ToObject<FileDownloadInfo>();
if (downloadInfo != null)
{
returnCard = CreateFileInfoAttachment(downloadInfo, attachment.Name, attachment.ContentUrl);
replyMessage.Attachments.Add(returnCard);
}
}
}
else
{
// Illustrates creating a file consent card.
returnCard = CreateFileConsentAttachment();
replyMessage.Attachments.Add(returnCard);
}
await context.PostAsync(replyMessage);
}
private static Attachment CreateFileInfoAttachment(FileDownloadInfo downloadInfo, string name, string contentUrl)
{
FileInfoCard card = new FileInfoCard()
{
FileType = downloadInfo.FileType,
UniqueId = downloadInfo.UniqueId
};
Attachment att = card.ToAttachment();
att.ContentUrl = contentUrl;
att.Name = name;
return att;
}
private static Attachment CreateFileConsentAttachment()
{
JObject acceptContext = new JObject();
// Fill in any additional context to be sent back when the user accepts the file.
JObject declineContext = new JObject();
// Fill in any additional context to be sent back when the user declines the file.
FileConsentCard card = new FileConsentCard()
{
AcceptContext = acceptContext,
DeclineContext = declineContext,
SizeInBytes = 102635,
Description = "File description"
};
Attachment att = card.ToAttachment();
att.Name = "Example file";
return att;
}