Upload a picture to Skype BOT

I have a bot with Microsoft Bot Framework and it works correctly in debug mode

After installing on Skype after uploading 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 simple and works without skype

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

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

}
........

How to find the picture I sent?

+4
source share
1 answer

According to this comment , in order to receive the attachment, the GET request must contain the JwtToken bot as the 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);
        }
    }    
}

, AnalyzeUrl, URL. , CognitiveServices, , .

+3

Source: https://habr.com/ru/post/1649384/


All Articles