在Skype频道中尝试时,我无法使用Bot框架(Bot.Builder v3.11.0)将文本文件上传为附件。它虽然在Bot框架模拟器中工作。以下是上传文件并返回附件中上传文件URL的活动的代码。使用Skype频道时会抛出异常。或者,有没有其他方法可以在Skype频道中上传/附加文本文件,然后用户可以从客户端下载该文件?
public static async Task<Activity> GetTextAttachmentAsync(Activity message)
{
var reply = message.CreateReply("Here is a text attachment");
var serviceUrl = reply.ServiceUrl;
var conversationId = reply.Conversation.Id;
byte[] fileData = null;
using (var wc = new System.Net.WebClient())
fileData = wc.DownloadData("https://textfiles.com/100/adventur.txt");
using (var connector = new ConnectorClient(new Uri(serviceUrl)))
{
var attachments = new Attachments(connector);
var token = await (connector.Credentials as MicrosoftAppCredentials).GetTokenAsync();
connector.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
conversationId,
new AttachmentData
{
Name = "transcript.html",
OriginalBase64 = fileData,
Type = "text/html"
});
reply.Attachments = new List<Attachment>
{
new Attachment
{
Name = "transcript.html",
ContentType = "text/html",
ContentUrl = attachments.GetAttachmentUri(response.Id)
}
};
return reply;
}
}
上述UploadAttachmentAsync()
函数抛出异常:
Microsoft.Rest.HttpOperationException: Not Found
at Microsoft.Bot.Connector.ErrorHandling.<HandleErrorAsync>d__2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Connector.ConversationsExtensions.<UploadAttachmentAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Support.Services.Bot.Core.Utilities.AdaptiveCardsHelper.<GetTextAttachmentAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Support.Services.Bot.Core.Dialogs.BotDialog.<HandleMessageAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Support.Services.Bot.Core.Dialogs.DialogBase`1.<MessageReceivedAsync>d__8.MoveNext()
答案 0 :(得分:1)
在Visual Studio中启用公共语言运行时错误的情况下运行代码时。我收到以下错误:
网站地址https://textfiles.com/100/adventur.txt似乎没有受信任的证书,这使得.NET不满意。当我去调查时,我发现这个Stack Overflow answer建议使用下面的代码来克服这个问题,但强烈建议不要在生产中使用它。
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
在我运行您的代码之前,这将是我的原始建议:许多频道(包括Skype),限制您在使用base64和/或本地文件时能够从机器人发送的文件类型。例如,我知道您无法在Skype中将PDF文件作为Base64发送。如果内存正常,您只能使用Skype中的base64方法发送图像和视频文件(也可能是音频)。因此,即使你解决了这个错误,你也可能会遇到这种情况。解决方法是使用托管文件。我并不完全确定您要对机器人做什么,所以我不确定这是否适合您,但这是一个选项。
因此,如果您在解决证书问题后发现此代码无效,请尝试发送图像文件,看看是否有效,如果确实如此,但HTML文件仍然失败,您就会知道这就是原因。