如何使用C#发送Microsoft botframework sdk v4本地文件夹中的图像 [英] how to send images which are in local folder in microsoft botframework sdk v4 using c#
问题描述
我使用Microsoft Botframework C#SDK V4创建了一个机器人,并且运行良好.现在,我想向该机器人添加一些图像.我正在使用卡片,但是卡片仅使用图像的网址.我要发送本地文件夹中的图像.我如何使它工作?
I created a bot with Microsoft Botframework C# SDK V4 and it is working well. Now I want to add some images to that bot. I am using cards but cards are taking only url of an image. I want to send the images which are in my local folder. How can I make it work??
我已经尝试过使用15.handling附件github repo(
I already tried this with 15.handling attachments github repo(https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/15.handling-attachments). And while I am using the exact code in handling attachments github repo, I am getting the below exception stacktrace for inline attachments.
Sorry, it looks like something went wrong.’ at Microsoft.Bot.Connector.Conversations.d__10.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Connector.ConversationsExtensions.d__17.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Builder.BotFrameworkAdapter.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.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Builder.TurnContext.<>c__DisplayClass22_0.
<g__SendActivitiesThroughAdapter|1>d.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Builder.TurnContext.d__21.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.BotBuilderSamples.AttachmentsBot.d__0.MoveNext() in
C:\botbuilder-samples\samples\csharp_dotnetcore\15.handling-attachments\AttachmentsBot.cs:line 65
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Builder.MiddlewareSet.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.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Bot.Builder.BotAdapter.d__13.MoveNext()’
如何解决此问题?
推荐答案
The 15.handling-attachments sample demonstrates how to read a local file, and attach it as a base64 encoded image:
reply.Attachments = new List<Attachment>() { GetInlineAttachment() };
private static Attachment GetInlineAttachment()
{
var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\architecture-resize.png");
var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));
return new Attachment
{
Name = @"Resources\architecture-resize.png",
ContentType = "image/png",
ContentUrl = $"data:image/png;base64,{imageData}",
};
}
另一种选择是使用ConnectorClient的 UploadAttachmentAsync API:
Another option is to use the ConnectorClient's UploadAttachmentAsync api:
private async Task SendFile(ITurnContext turnContext)
{
var webRoot = _env.ContentRootPath;
var imagePath = System.IO.Path.Combine(webRoot, "Resources", "BotFrameworkDiagram.png");
var connector = turnContext.TurnState.GetValueOrDefault("Microsoft.Bot.Connector.IConnectorClient") as ConnectorClient;
var attachments = new Attachments(connector);
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
turnContext.Activity.Conversation.Id,
new AttachmentData
{
Name = "BotFrameworkDiagram.png",
OriginalBase64 = File.ReadAllBytes(imagePath),
Type = "image/png"
});
var attachmentUri = attachments.GetAttachmentUri(response.Id);
var attachment = new Attachment
{
Name = "BotFrameworkDiagram.png",
ContentType = "image/png",
ContentUrl = attachmentUri
};
var reply = turnContext.Activity.CreateReply();
reply.Attachments.Add(attachment);
await turnContext.SendActivityAsync(reply);
}
这篇关于如何使用C#发送Microsoft botframework sdk v4本地文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!