在针对Azure AD进行身份验证后,我的机器人能够通过以下代码从Microsoft Graph检索当前用户照片,该代码将照片作为附件添加到响应消息中:
HttpClient client2 = new HttpClient();
client2.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
var response2 = await client2.GetByteArrayAsync("https://graph.microsoft.com/v1.0/me/photo/$value");
Activity replyToConversation = (Activity)context.MakeMessage();
replyToConversation.Type = "message";
replyToConversation.Attachments.Add(new Attachment()
{
Content = response2,
ContentType = "image/jpeg"
});
await context.PostAsync(replyToConversation);
context.Wait(MessageReceivedAsync);
一切都在Web聊天频道中按预期工作,但由于某种原因,图片未显示在Microsoft Teams频道中,并且机器人回答了默认错误消息:"抱歉,我的机器人代码出现问题。 "
请,任何想法?
答案 0 :(得分:0)
我首先要检查Attachments数组是否为null。通常它是,因此您可能会得到一个空引用异常。因此,在将附件添加到列表之前添加以下内容:
replyToConversation.Attachments = new List<Attachment>();
如果在那之后你还没有看到图像,你可以试着建立一个&#34; url&#34;将字节转换为base64字符串表示并将其设置为ContentUrl,而不是将字节数组设置为附件的内容。
string url = "data:image/jpeg;base64," + Convert.ToBase64String(response2)
var replyToConversation = context.MakeMessage();
replyToConversation.Attachments = new List<Attachment>();
replyToConversation.Attachments.Add(new Attachment()
{
ContentUrl = url,
ContentType = "image/jpeg"
});