botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Using user uploaded skype attachments

Open HKOLWD opened this issue 10 years ago • 7 comments

I am currently struggling to use the result of a Prompts.attachment() method. The prompt itself works fine. User selects a file and puts it in skype. The bot receives the file correctly, and the results show the correct output:

{"contentType":"image","contentUrl":"https://apis.skype.com/v2/attachments/SOME_ID/views/original","thumbnailUrl":"https://apis.skype.com/v2/attachments/SOME_ID/views/thumbnail"}

I am now looking for a way to actually pick up the file, convert it to a blob and upload it to an external database. I have tried using request/http to obtain the image outside the bot context, but run into authentication errors (403) on the skype endpoint.

response:{"statusCode":403,"body":"","headers":{"cache-control":"no-store, must-revalidate, no-cache","pragma":"no-cache","content-length":"0","contextid":"tcid=1565912047858859123,server=DB5SCH101100644","date":"Mon, 15 Aug 2016 15:27:02 GMT","connection":"close"}

Can someone give me an example of how to fetch the uploaded file and convert it to a blob? (or even better: upload it to a cloud storage service)

HKOLWD avatar Aug 15 '16 15:08 HKOLWD

You need to attach the bear auth token. You can get the token for example on the incoming request from the user. The Skype docs https://docs.botframework.com/en-us/skype/chat/#navtitle mentions this at the very end.

image

mingweiw avatar Aug 15 '16 17:08 mingweiw

Thanks for the response. A quick follow-up though:

You can get the token for example on the incoming request from the user.

What is the best way to obtain this token? Is there any variable inside the botbuilder/restify setup that I can pick up halfway a dialog? Somewhere inside the bot session? Could do with some tips on the best way to implement this..

HKOLWD avatar Aug 15 '16 18:08 HKOLWD

AFAIK the bear token is not directly accessible through the builder. There are a couple of options.

  1. In your controller you can save the Request.Headers.Authorization and use it when needed.
  2. If you have a HttpClient client you can do client.AddAPIAuthorization(...) (https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Microsoft.Bot.Connector/HttpClientEx.cs#L20).

mingweiw avatar Aug 15 '16 20:08 mingweiw

I think you're right. Token does not seem to be available in builder. I did manage to obtain the bearer token from the request now though, using the value from the restify (using node.js by the way) request. I am currently struggling to get skype to accept it though.

Using node request to call the endpoint, using:

            request.get("https://apis.skype.com/v2/attachments/<ID>/views/original", {
                  'auth': {
                        'bearer': <token obtained from restify>
                  }
                }, function (error, response, body) {

Skype won't have it though. I keep getting a 401, indicating invalid token. I have checked the token a 100 times now and it matches exactly that in the headers of the request I get from the user.

{"statusCode":401,"body":"","headers":{"cache-control":"no-store, must-revalidate, no-cache","pragma":"no-cache","content-length":"0","contextid":"tcid=<ID>,server=<ID>","date":"Mon, 15 Aug 2016 21:00:41 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"apis.skype.com","port":443,"hostname":"apis.skype.com","hash":null,"search":null,"query":null,"pathname":"/v2/attachments/<ID>/views/original","path":"/v2/attachments/<ID>/views/original","href":"https://apis.skype.com/v2/attachments/<ID>/views/original"},"method":"GET","headers":{"authorization":"Bearer <REDACTED TOKEN>"}}}

That's the response skype gives me. Removed all the ID / TOKEN stuff, but it's there and accurate (arguably). I do notice the /v2/ on the skype endpoint though, shouldn't this be /v3/? (endpoint still matches the contentUrl of the bot Attachment though)

HKOLWD avatar Aug 15 '16 21:08 HKOLWD

Is the something like BotConnector XXXX...? If so I think you need to strip off the BotConnector prefix.

mingweiw avatar Aug 16 '16 00:08 mingweiw

You can look at the code sample posted for #662 to see how you can access the skype attachments using bot's JwtToken.

msft-shahins avatar Aug 16 '16 00:08 msft-shahins

Thanks @msft-shahins I think that sample code is exactly what I need.

@mingweiw no, it was only the token, no BotConnector prefix. Looks like I simply used the wrong token/approach.

Thanks for all the help guys!

HKOLWD avatar Aug 16 '16 08:08 HKOLWD