使用在 Skype 频道中不起作用的 Bot 框架上传文本文件作为附件

Upload a text file as attachment using Bot framework not working in Skype channel

在 Skype 频道中尝试时,我无法使用 Bot 框架 (Bot.Builder v3.11.0) 将文本文件作为附件上传。它虽然在 Bot 框架模拟器中工作。以下是上传文件并在附件中上传文件 URL 到 returns activity 的代码。使用 Skype 频道时会抛出异常。或者,是否有任何其他方法可以在 Skype 频道中实现 uploading/attaching 用户随后可以从客户端下载的文本文件?

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()

当 运行在 Visual Studio 中启用公共语言运行时错误的代码时。我收到以下错误:

网站地址 https://textfiles.com/100/adventur.txt does not have a trusted certificate and that makes .NET unhappy. When I went to investigate, I found this Stack Overflow answer 似乎建议使用下面的代码来解决这个问题,但强烈建议不要在生产中使用它。

ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;

在我 运行 你的代码之前,这将是我最初的建议:许多渠道(包括 Skype),限制你在使用 base64 时可以从机器人发送的文件类型 and/or 本地文件。例如,我知道您不能在 Skype 中将 PDF 文件作为 Base64 发送。如果内存正常,您只能在 Skype 中使用 base64 方法发送图像和视频文件(也可能是音频)。因此,即使您解决了此错误,您之后也可能 运行 进入此错误。解决方法是使用托管文件。我不太确定你想用你的机器人做什么,所以我不确定这是否是你的具体选择,但它是一个选择。

因此,如果您在解决证书问题后发现此代码不起作用,请尝试发送图像文件并查看是否有效,如果有效但您的 HTML 文件仍然失败,您就会知道这就是原因.