【问题标题】:Bot Builder SDK 4 How to work with attachments?Bot Builder SDK 4 如何使用附件?
【发布时间】:2018-12-03 07:47:24
【问题描述】:

Bot Builder SDK 4 (dotnet) 如何处理附件?我尝试使用 BotBuilder-Samples 15.handling-attachments 的示例,但 Skype 频道出现 401 Unauthorized 错误。

foreach (var file in activity.Attachments)
{
    // Determine where the file is hosted.
    var remoteFileUrl = file.ContentUrl;

    // Save the attachment to the system temp directory.
    var localFileName = Path.Combine(Path.GetTempPath(), file.Name)

    // Download the actual attachment
    using (var webClient = new WebClient())
    {
        webClient.DownloadFile(remoteFileUrl, localFileName); <-- 401 here
    }

【问题讨论】:

  • 你到底想做什么?上传附件还是下载?
  • Bot 用户上传附件(一些文本文件),我需要阅读和处理它
  • 看起来您无权将文件保存到该目录,也许尝试另一个目录或尝试将文件保存到 azure storage 之类的地方。
  • 只有Skype频道有问题,webchat没有错误,所以问题不在本地路径
  • 看看当您使用图像(png、jpeg 等)而不是文本文件时会发生什么。 Skype 对允许您使用的文件类型很挑剔。

标签: c# .net botframework


【解决方案1】:

我在 github.com 讨论 Skype Can not receive attachment? #3623 上发现了解决方案,我也刚刚成功测试过。

我看到对您的代码示例的最小修改如下:

string channelToken = null;

if ((activity.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase)) 
{
    var credentials = new MicrosoftAppCredentials(youBotAppId, yourBotAppPassword);

    channelToken = await credentials.GetTokenAsync();
}

foreach (var file in activity.Attachments)
{
    // Determine where the file is hosted.
    var remoteFileUrl = file.ContentUrl;

    // Save the attachment to the system temp directory.
    var localFileName = Path.Combine(Path.GetTempPath(), file.Name)

    // Download the actual attachment
    using (var webClient = new WebClient())
    {
        if (!string.IsNullOrWhiteSpace(channelToken))
        {
            webClient.DefaultRequestHeaders.Authorization = 
                new AuthenticationHeaderValue("Bearer", channelToken);
        }

        webClient.DownloadFile(remoteFileUrl, localFileName);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多