Unable to retrieve Attachments in Microsoft bot framework via Compose bar (Unauthorized)

59 views Asked by At

When i tried to copy/paste an image from my system and try to share via Microsoft teams by directly pasting it in the compose box (Not the Clip icon), I am unable to retrieve it on the other end. I keep receiving original.json file which has 401 error.

The activity payload I've received is as below:

{
    "text": " \r\n\r\n ",
    "textFormat": "plain",
    "attachments": [
        {
            "contentType": "image/*",
            "contentUrl": "https://smba.trafficmanager.net/amer/v3/attachments/0-cus-d17-c53459922d66463fb94af32a61057191/views/original"
        },
        {
            "contentType": "text/html",
            "content": "<p>&nbsp;</p>\r\n<p><img itemtype=\"http://schema.skype.com/AMSImage\" alt=\"image\" src=\"https://us-api.asm.skype.com/v1/objects/0-cus-d17-c53459922d66463fb94af32a61057191/views/imgo\" itemscope=\"jpeg\" width=\"444.44444444444446\" height=\"250\" aria-label=\"has context menu\" id=\"x_0-cus-d17-c53459922d66463fb94af32a61057191\" itemid=\"0-cus-d17-c53459922d66463fb94af32a61057191\" style=\"vertical-align:bottom\"></p>\r\n<p>&nbsp;</p>"
        }
    ],
    "type": "message",
    "timestamp": "2023-12-17T09:58:08.0629932Z",
    "localTimestamp": "2023-12-17T15:28:08.0629932+05:30",
    "id": "1702807088043",
    "channelId": "msteams",
    "serviceUrl": "https://smba.trafficmanager.net/amer/",
    "from": {
        "id": "29:18spOrxkvI_78J0qBmTjCWlqbCm3EGNuI6pKQFMFkZKqkiUWdvHc4b3VRbVWEItXL_80_H8owExaXxb9KlhPnMA",
        "name": "Praveen Pasam",
        "aadObjectId": "42038d1b-4021-47ab-a494-26d722d8348e"
    },
    "conversation": {
        "conversationType": "personal",
        "tenantId": "f8250d44-a7af-4d49-85bc-589e0e15d852",
        "id": "a:1M23gbESVfdmapfvZtRSod9paD5QRWsJ8L0JOeefNUGpuCymrvAdMBLXOrdYBCSmlMoBkxWf14p-__GEvsnr0LRKk8A2gpVqkwy-UEbSMx5R5SbN-4ioJuiNsUQVuCbya"
    },
    "recipient": {
        "id": "28:2241ccbc-44a8-4267-8898-5affc2159e97",
        "name": "SmartAssistLatestUAT"
    },
    "entities": [
        {
            "locale": "en-GB",
            "country": "GB",
            "platform": "Web",
            "timezone": "Asia/Calcutta",
            "type": "clientInfo"
        }
    ],
    "channelData": {
        "tenant": {
            "id": "f8250d44-a7af-4d49-85bc-589e0e15d852"
        }
    },
    "locale": "en-GB",
    "localTimezone": "Asia/Calcutta"
}

I tried adding bot authentication headers for retrieving using below block of code but it doesn't seem to work either. I want to retrieve the attachment from contentUrl and share it to the agent/bot.

const axios = require("axios");
const { MicrosoftAppCredentials } = require("botframework-connector");

async function getAttachment() {
  try {
    // const serviceUrl = 'https://smba.trafficmanager.net/amer/';
    // MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
    const contentUrl = "https://smba.trafficmanager.net/amer/v3/attachments/0-cus-d17-c53459922d66463fb94af32a61057191";
    // const contentUrl = "https://us-api.asm.skype.com/v1/objects/0-cus-d17-c53459922d66463fb94af32a61057191/views/imgo";
    var credentials = new MicrosoftAppCredentials("<appId>", "<appPassword>");
    const botToken = await credentials.getToken();
    const config = {
      headers: { Authorization: `Bearer ${botToken}` },
      responseType: "stream",
    };
    const response = await axios({ method: "GET", url: contentUrl, ...config });
    console.log(" ~ file: testAPI.js:16 ~ getToken ~ response:", response.data);
  } catch (error) {
    console.log(" ~ file: tets.js:24 ~ getToken ~ error:", error);
  }
}

getAttachment();
0

There are 0 answers