4

我有一个使用 Microsoft Bot Framework 开发的机器人,并且在 Debug 中正确运行

在 Skype 上安装后,上传图片后,我有一个这样的链接

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

代码非常简单,无需Skype即可运行

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

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

}
........

如何找到我发送的图片?

4

1 回答 1

3

根据此评论,要获取附件,GET 请求应包含机器人的JwtToken作为授权标头:

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 能够接受图像数据。

于 2016-09-01T23:54:42.883 回答