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.
779 questions
Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,498 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,261 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,011 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Nivedipa-MSFT 2,891 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