将图像上传到 Skype BOT
Upload image to Skype BOT
我有一个使用 Microsoft Bot Framework 开发的机器人,并且在 Debug 运行 中正确
在 Skype 上安装后,上传图像后,我有一个 link 这样的
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);
}
........
如何找到我发送的图片?
根据this comment,要获取附件,GET 请求应包含机器人的 JwtToken 作为授权 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 能够接受图像数据。
我有一个使用 Microsoft Bot Framework 开发的机器人,并且在 Debug 运行 中正确
在 Skype 上安装后,上传图像后,我有一个 link 这样的
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);
}
........
如何找到我发送的图片?
根据this comment,要获取附件,GET 请求应包含机器人的 JwtToken 作为授权 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 能够接受图像数据。