Upload image to Skype BOT

喜欢而已 提交于 2019-12-06 05:56:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38633676/upload-image-to-skype-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!