Search code examples
c#botsskypebotframeworkskype-bots

Upload image to Skype BOT


I have a bot develop with Microsoft Bot Framework, and in Debug run correctly

After the install on Skype, after the upload the image I have a link like this

https://df-apis.skype.com/v2/attachments/0-eus-d4-7e19a097c62f5fc21dd53eabfa19d85e/views/original

The code is very simply and run without skype

if ((activity.Attachments != null) && (activity.Attachments.Count > 0))
{

      analysisResult = await AnalyzeUrl(activity.Attachments[0].ContentUrl);

}
........

How do I find the picture that I sent?


Solution

  • According to this comment, to fetch an attachment, the GET request should contain JwtToken of the bot as authorization header:

    var attachment = activity.Attachments?.FirstOrDefault();
    if (attachment?.ContentUrl != null)
    {
        using (var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl)))
        {
            var token = await (connectorClient.Credentials as MicrosoftAppCredentials).GetTokenAsync();
            var uri = new Uri(attachment.ContentUrl);
            using (var httpClient = new HttpClient())
            {
                if (uri.Host.EndsWith("skype.com") && uri.Scheme == Uri.UriSchemeHttps)
                {
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
                }
                else
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(attachment.ContentType));
                }
    
                var attachmentData = await httpClient.GetByteArrayAsync(uri);
                analysisResult = await AnalyzeUrl(attachmentData);
            }
        }    
    }
    

    That means you have to change the AnalyzeUrl to accept the image data instead of URL. I believe CognitiveServices, you are using, are able to accept the image data.