0

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

1 回答 1

1

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

似乎站点地址https://textfiles.com/100/adventur.txt没有受信任的证书,这使 .NET 不高兴。当我去调查时,我发现这个Stack Overflow 答案建议使用下面的代码来克服这个问题,但强烈建议不要在生产中使用它。

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

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

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

于 2018-05-21T21:16:14.177 回答