将图像上传到Skype BOT

时间:2016-07-28 10:25:21

标签: c# bots skype botframework skype-bots

我使用Microsoft Bot Framework开发bot,并在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);

}
........

如何找到我发送的图片?

1 个答案:

答案 0 :(得分:3)

根据this comment,要获取附件,GET请求应包含bot的 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以接受图片数据而不是网址。我相信您正在使用的CognitiveServices能够接受图像数据。