microsoft / BotFramework-Services

Microsoft Bot Framework Services
Creative Commons Attribution 4.0 International
38 stars 11 forks source link

Unable to download attachments via Copy/Paste in MSTeams bot #358

Open venugopalma-kore opened 11 months ago

venugopalma-kore commented 11 months ago

I tried configuring MS Teams bot and am able to exchanges messages, files via clip icon and emojis. But when i try to copy paste the image directly into the type section, i am unable to retrieve the imageUrl and is currently throwing 401 error.

Below is the attachment payload, am receiving as part of the event but unable to find any token for resolving this issue. Tried retrieving the contentUrl directly and also url parsed from HTML, but still unable to retrieve attachment information

"attachments": [
    {
        "contentType": "image/*",
        "contentUrl": "https://smba.trafficmanager.net/amer/v3/attachments/0-eus-d4-095421cef6f7d085f27ade8c7503fcc5/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-eus-d4-095421cef6f7d085f27ade8c7503fcc5/views/imgo\" itemscope=\"jpeg\" width=\"444.44444444444446\" height=\"250\" id=\"x_0-eus-d4-095421cef6f7d085f27ade8c7503fcc5\" itemid=\"0-eus-d4-095421cef6f7d085f27ade8c7503fcc5\" style=\"vertical-align:bottom\"></p>\r\n<p>&nbsp;</p>"
    }
],
luhan2017 commented 11 months ago

This is a teams channel specific issue, created an IcM for teams team, could you please share more information, for example your botid? Incident 452868450 : Unable to download attachments via Copy/Paste in MSTeams bot

venugopalma-kore commented 11 months ago

@luhan2017 , my issue is not related to bot but more like related to use case.

In my use case, the image is copy/pasted to the compose box. For example:

Open an image in Windows "Photos"app. Click on the trhee dots "..." Click "Copy" Paste the image inside Microsoft Team's compose box. Activity payload I've received is:

{ "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> </p>\r\n<p><img alt="\"image\"" width="\"444.44444444444446\"" height="\"250\"" style=""vertical-align: bottom\""></p>\r\n<p> </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" }

The URL inside contentUrl is not retrievable as I keep getting 401 error. I tried accessing both urls but neither are accessible.

  1. attachments[0].contentUrl - https://smba.trafficmanager.net/amer/v3/attachments/0-cus-d17-c53459922d66463fb94af32a61057191/views/original
  2. parsing html content and using img url inside it - https://us-api.asm.skype.com/v1/objects/0-cus-d17-c53459922d66463fb94af32a61057191/views/imgo
luhan2017 commented 11 months ago

Here is the replied from Teams Team, please check. This behavior is by design. Please refer to this Github issue for more details and sample code to resolve the issue. Bot cannot access images added by clipboard · Issue #1561 · MicrosoftDocs/msteams-docs (github.com) In short, MS Teams attachment URL is secured by JWT token, which means you need to pass the token from the bot to get access to it.

venugopalma-kore commented 11 months ago

@luhan2017 i understand that it's by design but what are the steps to generate the JWT Token ? I've trying to do the same using appId and appPassword, but it doesn't seem to be working. Can you share any documentation where example is provided for Node Js

luhan2017 commented 11 months ago

Could you please try this:

const { MicrosoftAppCredentials } = require('botframework-connector');  
const https = require('https');  
const fs = require('fs');  

if (turnContext.activity.attachments && turnContext.activity.attachments.length > 0 && turnContext.activity.attachments[0].contentType.startsWith('image/')) {  
  const attachment = turnContext.activity.attachments[0];  
  const token = await new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword).getToken();  
  const options = {  
    headers: {  
      'Authorization': `Bearer ${token}`,  
    },  
  };  
  https.get(attachment.contentUrl, options, (response) => {  
    const contentLengthBytes = response.headers['content-length'];  
    if (response.statusCode === 200) {  
      const imageFile = fs.createWriteStream('ImageFromUser.png');  
      response.pipe(imageFile);  
      turnContext.sendActivity(`Attachment of ${attachment.contentType} type and size of ${contentLengthBytes} bytes received.`);  
    }  
  });  
}  

Note that you'll need to replace process.env.MicrosoftAppId and process.env.MicrosoftAppPassword with your own Microsoft App ID and password.

venugopalma-kore commented 11 months ago

@luhan2017 , thanks for the code snippet but the response statusCode is coming up as 404. Attached screenshot for ref.

Token is getting generated as expected can you cross check the contentUrl format if the host or route url needs to be updated? Because, it seems GET request doesn't seem to be supported by this path. Attaching snippet for ref.

` const { MicrosoftAppCredentials } = require("botframework-connector"); const https = require("https"); const fs = require("fs");

const turnContext = { attachments: [ { contentType: "image/*", contentUrl: "https://smba.trafficmanager.net/amer/v3/attachments/0-cus-d17-c53459922d66463fb94af32a61057191/views/original", }, ], };

async function getAttachment() { if ( turnContext.attachments && turnContext.attachments.length > 0 && turnContext.attachments[0].contentType.startsWith("image/") ) { const attachment = turnContext.attachments[0]; const token = await new MicrosoftAppCredentials("", "").getToken(); const options = { headers: { Authorization: Bearer ${token}, }, }; https.get(attachment.contentUrl, options, (response) => { console.log("~ file: testAPI.js:33 ~ https.get ~ response:", response); const contentLengthBytes = response.headers["content-length"]; console.log("~ file: testAPI.js:37 ~ https.get ~ response.statusCode:", response.statusCode); if (response.statusCode === 200) { const imageFile = fs.createWriteStream("ImageFromUser.png"); response.pipe(imageFile); turnContext.sendActivity( Attachment of ${attachment.contentType} type and size of ${contentLengthBytes} bytes received. ); } }); } }

getAttachment(); ` Screenshot from 2024-01-02 09-58-12

luhan2017 commented 11 months ago

@venugopalma-kore Could you please try with a new uploaded url? the content will be deleted after a period of time.

luhan2017 commented 10 months ago

any update? @venugopalma-kore

venugopalma-kore commented 10 months ago

@luhan2017 , we will be validating the above mentioned approach and will get back to you if we face any issues. Please keep the ticket open till then for tracking purpose. thanks