Forum Discussion
MS Teams bot - how to download file that was uploaded in teams
I'm using teams bot api.
We want to support images - a user will upload images in teams bot conversation, and the image will be sent to our server and converted to base64.
The request that my server gets:
{"text":"send file","textFormat":"plain","attachments":[{"contentType":"application/vnd.microsoft.teams.file.download.info","content":{"downloadUrl":"https://*-my.sharepoint.com/personal/*/_layouts/15/download.aspx?UniqueId=2f***b&Translate=false&tempauth=*&ApiVersion=2.0","uniqueId":"*","fileType":"pdf"},"contentUrl":"https://*-my.sharepoint.com/personal/*/Documents/Microsoft Teams Chat Files/myFile.pdf","name":"myFile.pdf"},{"contentType":"text/html","content":"<p>send file</p>"}],"type":"message","timestamp":"2022-07-25T11:13:17.7731472Z"........}
This is my code:
if (message.attachments) {
const file = message.attachments.find(a => a.contentType === "application/vnd.microsoft.teams.file.download.info");
if (file) {
require('request').get({uri: file.content.downloadUrl, encoding: null}, function (err, res, body) {
if (!err && res.statusCode === 200) {
const base64Data = "data:" + res.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
} else console.log(err ? err : "statusCode: " + res.statusCode);
});
}
}
I'm getting "statusCode: 403".
From the docs it seems like the URL should be public (pre-auth) for a few minutes:
content.downloadUrl is a pre-authenticated link to download the file.
To fetch the contents of the file, send a GET request to the URL in content.downloadUrl. The URL is only valid for a few minutes, so you must fetch the file immediately.
Thanks in advance
- Sayali-MSFTMicrosoftchisr- We are looking into this we will get back to you soon.
- Sayali-MSFTMicrosoft
chisr -Your bot can directly send and receive files with users in the
personal
context, also known as personal chats, using Teams APIs. In order to send and receive files in your bot, you have to set thesupportsFiles
property in the manifest totrue
. This property is described in the bots section of the Manifest reference. The definition will look like this:"supportsFiles": true
. If your bot doesn't enablesupportsFiles
, this features won't work.
Reference Document- Send and receive files from a bot - Teams | Microsoft Docs
Sample Link-BotBuilder-Samples/samples/javascript_nodejs/56.teams-file-upload at 397672668ddc14a1adab7fc81068c6112d980fe7 · microsoft/BotBuilder-Samples (github.com)- chisrCopper Contributor
Thanks!
This is not a published application, the users talk to the bot through the link from the registered bot Portal. So I will have to add my app as described here ? - https://docs.microsoft.com/en-us/microsoftteams/platform/sbs-file-handling-in-bot?tabs=visualstudio&tutorial-step=5
- Sayali-MSFTMicrosoft
Please have look into this doc-Send and receive messages with a bot - Teams | Microsoft Docs