Get attachment in Bot Framework REST API

Benoit Dupont 61 Reputation points
2023-01-27T13:20:49.1533333+00:00

Hello,

I'm developing a Teams bot that handle the attachments sent by the user in a conversation.

I don't use any Bot SDK, only REST calls to the Bot Framework REST API.

The documentation states that I can get attachment info and binary data by calling a specific endpoint URL.

GET /v3/attachments/{attachmentId}

The endpoint returns the following error when I call it with a valid token.

GET /v3/attachments/3e3c726a-95fd-4bcf-a817-eb9d0312e306

{"error":{"code":"BadArgument","message":"Invalid document content"}}

The ID is the one associated with the attachment in the activity I receive from Teams/Azure bot service.

What am I doing wrong ?

Thanks

Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
745 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,840 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Benoit Dupont 61 Reputation points
    2023-01-30T12:43:36.61+00:00

    I receive an Activity from Microsoft Teams.

    The file ID I use to make the API call is the one in value.attachments[0].id

    I know I have a downloadUrl in this activity but it's not always the case

    {
        "name": "composeExtension/fetchTask",
        "type": "invoke",
        "timestamp": "2023-01-30T12:42:48.946Z",
        "localTimestamp": "2023-01-30T08:42:48.946-04:00",
        "id": "f:2985c764-36f3-6ec4-116f-152f196fafd4",
        "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": "XM Fax"
        },
        "entities": [
            {
                "locale": "en-CA",
                "country": "CA",
                "platform": "Web",
                "timezone": "America/Halifax",
                "type": "clientInfo"
            }
        ],
        "channelData": {
            "tenant": {
                "id": "caafef10-9d12-4668-a0d6-268b71695e1a"
            },
            "source": {
                "name": "compose"
            }
        },
        "value": {
            "commandId": "share-by-fax",
            "commandContext": "message",
            "requestId": "a3071327-d6c0-48b2-9523-1cf19f92b5e9",
            "messagePayload": {
                "linkToMessage": "https://teams.microsoft.com/l/message/19:af24b8a2-cfe8-453a-a9b4-4a917b1534ae_45fc30f1-94c5-48aa-b0df-ef0e41b65c6c@unq.gbl.spaces/1675081945447?context=%7B%22contextType%22:%22chat%22%7D",
                "id": "1675081945447",
                "replyToId": null,
                "createdDateTime": "2023-01-30T12:32:25.447Z",
                "lastModifiedDateTime": null,
                "deleted": false,
                "subject": null,
                "summary": null,
                "importance": "normal",
                "locale": "en-us",
                "body": {
                    "contentType": "html",
                    "content": "<attachment id=\"666296e5-4242-4f06-a050-ee38b449b76a\"></attachment>"
                },
                "from": {
                    "device": null,
                    "conversation": null,
                    "user": {
                        "userIdentityType": "aadUser",
                        "id": "af24b8a2-cfe8-453a-a9b4-4a917b1534ae",
                        "displayName": "Benoit Dupont"
                    },
                    "application": null
                },
                "reactions": [],
                "mentions": [],
                "attachments": [
                    {
                        "id": "666296e5-4242-4f06-a050-ee38b449b76a",
                        "contentType": "reference",
                        "contentUrl": "https://xxwdq-my.sharepoint.com/personal/adm_xxwdq_onmicrosoft_com/Documents/Microsoft Teams Chat Files/PDF (1).PDF",
                        "content": null,
                        "name": "PDF (1).PDF",
                        "thumbnailUrl": null
                    }
                ]
            },
            "context": {
                "theme": "default"
            }
        },
        "locale": "en-CA",
        "localTimezone": "America/Halifax"
    }