我正在研究基于动作的消息传递扩展。我有代码从调用该操作的消息中获取附件列表,但是我始终无法获得附件文件。
我在这里找到了看起来不错的代码: https://github.com/microsoft/botframework-sdk/issues/4466
var attachment = turnContext.Activity.Attachments.First();
using (HttpClient httpClient = new HttpClient())
{
// Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if (turnContext.Activity.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || turnContext.Activity.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase))
{
var token = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
await turnContext.SendActivityAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
}
但是我不清楚获得令牌的路线。首先,MicrosoftAppCredentials没有空的构造函数。如果使用我的机器人应用程序的ID和密码进行尝试,则当尝试获取内容时,会出现401未经授权的错误。
我可以理解,要使用ContentUrl,我需要传递一个令牌,但是我不清楚-我需要哪种令牌以及如何在代码中获取它?
编辑:
通过单击包含文件的消息中的“更多操作”中的操作,我得到的有效负载是:
{
"value": {
"commandId": "doCommand",
"commandContext": "message",
"messagePayload": {
"id": "1571645922015",
"replyToId": null,
"createdDateTime": "2019-10-21T08:18:42.015Z",
"lastModifiedDateTime": null,
"deleted": false,
"subject": "",
"summary": null,
"importance": "normal",
"locale": "en-us",
"body": {
"contentType": "text",
"content": "Test with attachment<attachment id=\"4CBC1114-02B6-4FB5-87D4-63048312B0EC\"></attachment>"
},
"from": {
"device": null,
"conversation": null,
"user": {
"userIdentityType": "aadUser",
"id": "e291b04a-f0d8-43e8-829a-499e644005e1",
"displayName": "Amy Thorne"
},
"application": null
},
"reactions": [],
"mentions": [],
"attachments": [
{
"id": "581E8D22-2955-4DD4-8594-90F92F6737CB",
"contentType": "reference",
"contentUrl": "https://amythorneco.sharepoint.com/sites/AmysTeam/Shared Documents/General/Test.txt",
"content": null,
"name": "Test.txt",
"thumbnailUrl": null
}
]
},
"context": { "theme": "default" }
},
"conversation": { "id": "19:b7082f7e8a2547d49e0c730b9566fded@thread.skype;messageid=1571645922015" },
"name": "composeExtension/fetchTask",
"imdisplayname": "Amy Thorne"
}