Teams Bot Framework get file attachments ?

Benoit Dupont 66 Reputation points
2022-09-15T19:38:33.283+00:00

Hello,

I'm trying to build a Teams app to get file in a chat (compose box or message action) and send it to a webservice.

How can the bot read the file content ?

I can get the file contentUrl with the following code

const url = context.activity.value.messagePayload.attachments[0].contentUrl  
const response = await axios.get(url, { responseType: 'arraybuffer' });  

When I try to access it inside the bot with axios I have a HTTP 403 forbidden error.

This documentation states that I can have access to the file content when it's a personal file "The user can directly read from this URL to fetch its binary content."
https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bots-filesv4#code-sample

There should be another solution by using the Microsoft Graph.
I don't understand how ?
I have the attachment ID but how do I pass it to the Microsoft Graph to read it's content ?

Thanks

Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
896 questions
Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
10,890 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,068 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,563 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Benoit Dupont 66 Reputation points
    2022-09-20T10:52:36.723+00:00

    I found out another way to do it.

    https://graph.microsoft.com/v1.0/users/{from.aadObjectId}/drive/root:/{ATTACHMENTS.CONTENTURL.afterDocumentsPath}  
    

    With this, I have the reference to the object and a "@microsoft.graph.downloadUrl" link to use with axios and download its content.

          const att = await graphClient.api("/users/a841a46f-80f6-41cd-8717-7e41d6817192/drive/root:/Microsoft%20Teams%20Chat%20Files/PDF.PDF").get();  
          const response = await axios.get(att["@microsoft.graph.downloadUrl"], { responseType: 'arraybuffer'});  
      
          const buffer64 = Buffer.from(response.data, 'binary').toString('base64');  
    
    1 person found this answer helpful.

  2. Shivam Dhiman 6,071 Reputation points
    2022-09-16T05:06:58.113+00:00

    Hey @Benoit Dupont

    If you are looking a way to get attachment content you can use this https://graph.microsoft.com/v1.0/me/messages/{message-id}/attachments/{attachment-id}/$value Graph API endpoint. This will allow you to Get the raw contents of attachment. Please refer to this documentation for more details also please make sure you have right permissions before using this Graph API. Please refer to the below sample screenshot from Graph Explorer.

    241664-attachment1.png

    Also if you want to get the Code Snippets for this Graph API using Graph explorer.

    241647-attachment2.png

    Hope this helps.

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

    0 comments No comments

  3. Nivedipa-MSFT 3,411 Reputation points Microsoft Vendor
    2022-09-20T09:50:27.937+00:00

    You can get the message ID using below API:

    GET /me/messages  
    GET /users/{id | userPrincipalName}/messages  
    

    You can get the attachment ID using below API's:

    GET /groups/{id}/threads/{id}/posts/{id}/attachments  
    GET /groups/{id}/conversations/{id}/threads/{id}/posts/{id}/attachments  
    

    Ref Doc:
    https://learn.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http
    https://learn.microsoft.com/en-us/graph/api/post-list-attachments?view=graph-rest-1.0&tabs=http

    Thanks,
    Nivedipa


    If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.