microsoft-graph-api - 在 ChatMessage 中预览图片附件

标签 microsoft-graph-api microsoft-graph-sdks microsoft-graph-teams

我们正在使用 ms graph api 从内部桌面应用程序向团队 channel 发布消息。主要目的是将图像附加到消息中。我们将图片文件上传到 channel 的一盘文件夹中,如下图。


                var uploadProps = new DriveItemUploadableProperties
                {
                    ODataType = null,
                    AdditionalData = new Dictionary<string, object>
                    {
                        { "@microsoft.graph.conflictBehavior", "replace" }
                    }
                };


                var session = await graphClient.Drives[driveId]
                    .Items[parentId].ItemWithPath(fileName).CreateUploadSession(uploadProps).Request().PostAsync(token);

                int maxSliceSize = 320 * 1024;

                var fileUploadTask =
                    new LargeFileUploadTask<DriveItem>(session, fileStream, maxSliceSize);

                // Create a callback that is invoked after each slice is uploaded
                IProgress<long> progress = new Progress<long>(reportAsync);

                // Upload the file
                var uploadResult = await fileUploadTask.UploadAsync(progress);

                if (uploadResult.UploadSucceeded)
                {
                    return uploadResult.ItemResponse;
                }

然后我们向 channel 发送一条消息,并附上之前上传的图片作为引用附件。
var chatMsg = new ChatMessage();

chatMsg.Body = new ItemBody();
chatMsg.Body.ContentType = BodyType.Html;
chatMsg.Body.Content = msg + " " + string.Join(" ", attachments.Select(d => $"<attachment id=\"{parseEtag(d.ETag)}\"></attachment>"));

chatMsg.Attachments = attachments.Select(d => new ChatMessageAttachment()
{
   Id = parseEtag(d.ETag),
   ContentType = "reference",
   ContentUrl = d.WebUrl,
   Name = d.Name
});

return await this.graphClient.Teams[teamId].Channels[channelId].Messages
                .Request()
                .AddAsync(chatMsg, token);
问题是该消息只显示了附件的名称,而没有在底部的消息中看到的预览。我们希望在团队应用程序中附加文件时显示预览(顶部消息)。
Imgur
我们尝试将附件的缩略图 url 属性设置为从 ms-graph api 获取的缩略图 url,但没有成功。
我们已经使用团队应用程序(带预览)上传了一个文件,然后在我们的应用程序中使用相同的文件(相同的驱动器项 ID)创建了一条相同的消息(显示没有预览)。然后我们使用图形 api 获取了两条消息,除了消息 id 的 ofc 之外,无法辨别两者之间的任何差异。
我们搜索了这些论坛、ms 文档甚至建议页面,但一无所获。
我们已经能够在引用缩略图 URL 的消息正文和消息卡中分别显示预览,但理想情况下,我们希望直接在附件中进行预览。
编辑
缩略图 url 似乎在 24 小时后过期,因此不是一个很好的解决方案。

最佳答案

我们设法使用 Simple Upload Api 解决了这个问题。 ,加上 ?$expand=thumbnails查询参数。我还没有尝试过,但查询参数也应该适用于您正在使用的端点。
ThumbnailSet 中选择尺寸在上传响应中,并将其作为图像标签添加到您的消息正文中。见下文:

// channel, file, extractIdFromEtag, message omitted for brevity.

// PUT /groups/{group-id}/drive/items/{parent-id}:/{filename}:/content
const uploadUrl = `https://graph.microsoft.com/beta/groups/${channel.teamId}/drive/items/root:/${channel.displayName}/${file.name}:/content?$expand=thumbnails`;
const res = await this.http.put(uploadUrl, file).toPromise(); // FYI Using Angular http service

const attachment = {
  id: extractIdFromEtag(res.eTag),
  contentType: 'reference',
  contentUrl: res.webUrl,
  name: res.name,
  thumbnailUrl: res.webUrl
};

const postBody = {
  subject: null,
  body: {
    contentType: 'html',
    content: message
  },
};
// This is what makes the image show in the message as if posted from teams
postBody.body.content += `<br><br><img src="${res.thumbnails[0].large.url}" alt="${res.name}"/>`;

const messageUrl = `https://graph.microsoft.com/beta/teams/${channel.teamId}/channels/${channel.id}/messages`;
const result = await this.http.post(messageUrl, postBody).toPromise();
// Done
如果您希望将原始图像作为文件附加,以及在消息中显示图像预览,您也可以像之前一样继续添加附件。

关于microsoft-graph-api - 在 ChatMessage 中预览图片附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62650974/

相关文章:

c# - 如何在 C# 中使用 Microsoft Graph Api 上传到 OneDrive

microsoft-graph-api - 通过microsoft graph api从outlook.com联系人获取多个手机号码

microsoft-graph-api - 如何在 Microsoft Graph Explorer 中更改默认租户

java - [microsoft-graph-api]使用 Java 进行 Android 脱糖处理不适用于 API 24

java - 使用 msgraph-sdk-java 实现组 $filter 查询

android - 多作用域不起作用,请使用 Microsoft Graph API SDK

sharepoint - 使用 Graph Api 创建团队时,Microsoft Teams channel 电子邮件地址、SharePoint 和文件无法访问

microsoft-graph-api - 使用 Microsoft Teams 中的 Microsoft Graph API 列出私有(private)团队 channel 成员

javascript - checkin / checkout Sharepoint REST API

microsoft-graph-api - Planner 计划 URL 或在 Teams 中创建 Planner 选项卡