我的机器人在对话框中提示用户输入附件,并假设从用户接收图像并将该文件的内容名称,URL和类型保存到变量中。但是,我收到的文件不是用户发送的文件。而是从此链接检索名为“blob”(无扩展名)的文件:
该文件包含:
{"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);
}
以下是我在消息控制器中处理附件的方法。这是在Post Task中:
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上发布的版本没有。代码与示例非常相似,所以我不明白为什么机器人没有检测到用户的文件。 如何收到正确的文件(用户回复的文件)?
答案 0 :(得分:0)
附件将在元素1之后。 E.g。
Attachment attachment = list.ElementAt(1);
网络聊天(可能还有其他平台)有一个奇怪的怪癖,在提示并获取用户的附件时,它会在IEnumerable'结果'的开头添加一个blob文件。
private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{...}