Microsoft Graph GetPhoto 在 Microsoft Teams 机器人中不起作用
Microsoft Graph GetPhoto not working in Microsoft Teams bot
针对 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 Chat 频道中一切正常,但由于某种原因图片未显示在 Microsoft Teams 频道中并且机器人会回答默认错误消息:"Sorry, my bot code is having an issue."
拜托,有什么想法吗?
我首先要检查附件数组是否不为空。通常是这样,因此您可能会得到空引用异常。因此,在将附件添加到列表之前添加以下内容:
replyToConversation.Attachments = new List<Attachment>();
如果之后您仍然看不到图像,您可以尝试构建一个 "url" 将字节转换为 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"
});
针对 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 Chat 频道中一切正常,但由于某种原因图片未显示在 Microsoft Teams 频道中并且机器人会回答默认错误消息:"Sorry, my bot code is having an issue."
拜托,有什么想法吗?
我首先要检查附件数组是否不为空。通常是这样,因此您可能会得到空引用异常。因此,在将附件添加到列表之前添加以下内容:
replyToConversation.Attachments = new List<Attachment>();
如果之后您仍然看不到图像,您可以尝试构建一个 "url" 将字节转换为 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"
});