我希望用户能够将图像作为消息发送给Bot。这可能吗?。我在网上搜索了解决方案,感到很累。请有人至少可以与我分享一个链接吗?
答案 0 :(得分:2)
是强>
来自nodejs
文档here
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function (session) {
var msg = session.message;
if (msg.attachments && msg.attachments.length > 0) {
// Echo back attachment
var attachment = msg.attachments[0];
session.send({
text: "You sent:",
attachments: [
{
contentType: attachment.contentType,
contentUrl: attachment.contentUrl,
name: attachment.name
}
]
});
} else {
// Echo back users text
session.send("You said: %s", session.message.text);
}
});
c#
文档为here
答案 1 :(得分:0)
答案是肯定的,您可以提供您正在使用的频道允许附件。频道对大小和文件类型等内容有限制,因此这取决于您使用的频道。因此,如果您无法获得PDF,请尝试使用图像。如果图像不起作用,请尝试使用较小的图像。
在机器人中接收图像不需要特殊代码。 Activity
中的图片将显示为Activity.Attachments
这是附件的List
,或者是您的案例图片。这可以很容易地从Rajesh的答案中推断出来,但为了完整起见,这是一个用收到的文件做一些事情的例子:
在RootDialog.cs
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Net;
using System.Threading.Tasks;
namespace Bot_Application15.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
foreach (var file in activity.Attachments)
{
//where the file is hosted
var remoteFileUrl = file.ContentUrl;
//where we are saving the file
var localFileName = @"C:\Users\{UserName}\pictures\test" + file.Name;
using ( var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
}
await context.PostAsync($"File received");
context.Wait(MessageReceivedAsync);
}
}
}
答案 2 :(得分:0)
是的,可以在Bot对话的MessageRecivedAsync方法中使用。
private async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
if (message.Attachments != null && message.Attachments.Any())
{
// Do something with the attachment
}
else
{
await context.PostAsync("Please upload a picture");
context.Wait(this.MessageReceived);
}
}