Teams Bot Framework get file attachments ?

Benoit Dupont 61 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.
747 questions
Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,077 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,595 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.
2,847 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Shivam Dhiman 5,946 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

  2. Benoit Dupont 61 Reputation points
    2022-09-16T11:22:43.683+00:00

    To better understand this.

    I need my bot to catch the messageId and attachmentId then call the Microsoft Graph to get the content.

    Does this work with messages/files from a group chat ?

    What about the permissions ?
    My Teams app will be on the Teams App Store.
    Does each user have to update its Azure permissions for my app to access the content ?

    Thanks

    0 comments No comments

  3. Benoit Dupont 61 Reputation points
    2022-09-16T19:41:04.84+00:00

    Also, I tried you exemple with the Microsoft Graph.
    It doesn't seem to be working.

    /messages/ are for a user's mailbox and not for Teams messages ?

    0 comments No comments

  4. Benoit Dupont 61 Reputation points
    2022-09-16T19:46:53.663+00:00

    This is the context activity

     {  
          name: 'composeExtension/fetchTask',  
          type: 'invoke',  
          timestamp: 2022-09-16T19:41:45.710Z,  
          localTimestamp: 2022-09-16T19:41:45.710Z,  
          id: 'f:07d4ae63-8070-7a80-38e2-a6ff47d2770c',  
          channelId: 'msteams',  
          serviceUrl: 'https://smba.trafficmanager.net/amer/',  
          from: {  
            id: '29:1JiEH1nLmzqH5YT7Do_1KRCs7ISDxUmVVKPbIzwM1pc884QpFbzi7GFznE58JZKe5sdjw65yzjAZtxyJt55B3mA',  
            name: 'Benoit Dupont',  
            aadObjectId: 'af24b8a2-cfe8-453a-a9b4-4a917b1534ae'  
          },  
          conversation: {  
            conversationType: 'personal',  
            tenantId: 'caafef10-9d12-4668-a0d6-268b71695e1a',  
            id: 'a:1ktMfOkUqtyhd4ub9GhZwaOdqpjjygWz5IZGvHZLTCnduBU1HmsFGO9vBAMZMQhqJOeiphjhE_k9G3HerhrBgeIf6uuA-FYRX93w_OsJYVTFX1lB36Q5xJgAGJtizZM1o'  
          },  
          recipient: {  
            id: '28:45fc30f1-94c5-48aa-b0df-ef0e41b65c6c',  
            name: 'otxmdemobot'  
          },  
          entities: [  
            {  
              locale: 'fr-CA',  
              country: 'CA',  
              platform: 'Web',  
              timezone: 'America/Halifax',  
              type: 'clientInfo'  
            }  
          ],  
          channelData: {  
            tenant: { id: 'caafef10-9d12-4668-a0d6-268b71695e1a' },  
            source: { name: 'compose' }  
          },  
          value: {  
            commandId: 'CustomForm',  
            commandContext: 'message',  
            requestId: 'cde1762b-3661-4464-b9a4-e8722b9d261b',  
            messagePayload: {  
              linkToMessage: 'https://teams.microsoft.com/l/message/19:af24b8a2-cfe8-453a-a9b4-4a917b1534ae_45fc30f1-94c5-48aa-b0df-ef0e41b65c6c@unq.gbl.spaces/1663345138603?context=%7B%22contextType%22:%22chat%22%7D',  
              id: '1663345138603',  
              replyToId: null,  
              createdDateTime: '2022-09-16T16:18:58.603Z',  
              lastModifiedDateTime: null,  
              deleted: false,  
              subject: null,  
              summary: null,  
              importance: 'normal',  
              locale: 'en-us',  
              body: [Object],  
              from: [Object],  
              reactions: [],  
              mentions: [],  
              attachments: [Array]  
            },  
            context: { theme: 'default' }  
          },  
          locale: 'fr-CA',  
          localTimezone: 'America/Halifax',  
          rawTimestamp: '2022-09-16T19:41:45.71Z',  
          rawLocalTimestamp: '2022-09-16T16:41:45.71-03:00',  
          callerId: 'urn:botframework:azure'  
        }  
    

    And this is the activity payload

    {  
      linkToMessage: 'https://teams.microsoft.com/l/message/19:af24b8a2-cfe8-453a-a9b4-4a917b1534ae_45fc30f1-94c5-48aa-b0df-ef0e41b65c6c@unq.gbl.spaces/1663344433978?context=%7B%22contextType%22:%22chat%22%7D',  
      id: '1663344433978',  
      replyToId: null,  
      createdDateTime: '2022-09-16T16:07:13.978Z',  
      lastModifiedDateTime: null,  
      deleted: false,  
      subject: null,  
      summary: null,  
      importance: 'normal',  
      locale: 'en-us',  
      body: {  
        contentType: 'html',  
        content: '<attachment id="47114f5e-b0f9-4208-9e15-b76f11ea51ab"></attachment>'  
      },  
      from: {  
        device: null,  
        conversation: null,  
        user: {  
          userIdentityType: 'aadUser',  
          id: 'af24b8a2-cfe8-453a-a9b4-4a917b1534ae',  
          displayName: 'Benoit Dupont'  
        },  
        application: null  
      },  
      reactions: [],  
      mentions: [],  
      attachments: [  
        {  
          id: '47114f5e-b0f9-4208-9e15-b76f11ea51ab',  
          contentType: 'reference',  
          contentUrl: 'https://xxwdq-my.sharepoint.com/personal/adm_xxwdq_onmicrosoft_com/Documents/Fichiers de conversation Microsoft Teams/ChannelUsageDaily.pdf',  
          content: null,  
          name: 'ChannelUsageDaily.pdf',  
          thumbnailUrl: null  
        }  
      ]  
    }  
    

    What should be the message ID and attachmentID to use with the microsoft graph ?

    Thanks

    0 comments No comments

  5. Nivedipa-MSFT 2,806 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