【问题标题】:Incorrect file recieved when user replies with attachment用户回复附件时收到的文件不正确
【发布时间】:2017-04-03 08:53:32
【问题描述】:

我的机器人在对话框中提示用户输入附件,并假设从用户接收图像并将该文件的内容名称、url 和类型保存到变量中。但是,我收到的文件不是用户发送的文件。相反,它从此链接检索名为“blob”(无扩展名)的文件:

https://webchat.botframework.com/attachments/GkLhiqJcvH019mP0iGBKvo/0000052/0/blob?t=X5ICiUrhFas.dAA.RwBrAEwAaABpAHEASgBjAHYASAAwADEAOQBtAFAAMABpAEcAQgBLAHYAbwAtADAAMAAwADAAMAA1ADIA.mcEiHxuH0gE.VMcp6Yduqgc.4xT1ZTOvCX-B7A0nLto6eZNFrFi-0xSzGk5AKmA-EPE

该文件包含:

{"type":"message","from":{"id":"9n0I1ZqSrLF","name":"You"},"locale":"en-US","timestamp":"2017-02-14T21:12:32.074Z","channelData":{"clientActivityId":"1487106689866.9060198022610071.8"}}

这是附件提示的代码。此提示位于对话框中:

private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
    {
        IEnumerable<Attachment> list = await result;
        Attachment attachment = list.FirstOrDefault();

        string filename = attachment.Name;
        string url = attachment.ContentUrl;
        string contentType = attachment.ContentType;


        //Set attachmentFileNames
        if (attachmentFileNames == null)
        {
            attachmentFileNames = "";
        }

        attachmentFileNames += contentType;
        attachmentFileNames += ",";
        numberOfFiles++;

        //Set attachmentFileURLs
        if (attachmentFileURLs == null)
        {
            attachmentFileURLs = "";
        }

        attachmentFileURLs += url;
        attachmentFileURLs += ",";

        attachmentHasBeenAdded = true;
        await FirstMessageReceivedAsync(context, result);

    }

这是我在消息控制器中处理附件的方式。这是在发布任务中:

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            if (activity.Attachments != null && activity.Attachments.Any())
            {
                hasaAttachment = true;
                var attachment = 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 ((activity.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || activity.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase))
                        && new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
                    {
                        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;

                    Activity reply = activity.CreateReply($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received. URL: {attachment.ContentUrl}");

                    await connector.Conversations.ReplyToActivityAsync(reply);
                }
            }

我正在关注这个例子: Receive Attachment Bot Sample

在模拟器中它可以正常工作,但在 azure 上发布的版本不能。该代码与示例非常相似,所以我不明白为什么机器人没有检测到用户的文件。 如何接收正确的文件(用户回复的文件)?

【问题讨论】:

    标签: c# botframework


    【解决方案1】:

    附件将从元素 1 开始。例如

    Attachment attachment = list.ElementAt(1);
    

    网络聊天(可能还有其他平台)有一个奇怪的怪癖,它会在提示和获取用户附件时在 IEnumerable 'result' 的开头添加一个 blob 文件。

    private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
    {...}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2015-02-16
      • 2013-01-18
      • 2022-01-13
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多