对于使用c#构建的聊天机器人,我需要显示带有图像的个人资料卡片(ThumbnailCard
)。
图像以base64
存储在数据库中。
CardBuilder:
internal static ThumbnailCard CreateProfileCard(Belegplan profile)
{
ThumbnailCard card = new ThumbnailCard()
{
Title = $"{profile.Nachname}, {profile.Vorname}",
Text = $"({profile.Bereich}-{profile.Jobtitel}). {profile.SitznummerGesamt}:: Stockwerk: {profile.Stockwerk}, Tischgruppe: {profile.Tischgruppe}, Sitznummer:{profile.Sitzplatz?.ToString()}.",
};
String imgUrl = DataService.GetPhotoDataByName(testchatbotservice.Properties.Settings.Default.DB_Azure, profile);
if (!String.IsNullOrEmpty(imgUrl))
{
card.Images = new List<CardImage>()
{
new CardImage()
{
Url = "data:image/png;base64,"+ imgUrl
}
};
}
return card;
}
它在botframework模拟器中工作,但在我使用Web作为通道时却不行。发回卡时我得到BadRequest。
Attachment attachment = new Attachment()
{
ContentType = HeroCard.ContentType,
Content = CardsBuilder.CreateProfileCard(person)
};
var reply = context.MakeMessage();
reply.Attachments.Add(attachment);
await context.PostAsync(reply, CancellationToken.None);
我应该如何创建卡片来显示图像?如果无法使用base64,将图像存储在Azure中的博客存储表中是一种选择,但它需要在我们的工作流程中进行一些调整,因此不需要。
更新
存储为varbinary(max)
的图像。我获取图像,然后将它们转换为base64 string
使用静态文件夹中的图像可以正常工作,从数据库中获取数据仍然无法正常工作。
例外:
POST到test_chatbot_service失败:对机器人端点的POST失败,HTTP状态500到机器人端点的POST失败,HTTP状态为500
方法失败Microsoft.Bot.ChannelConnector.BotAPI + d__29.MoveNext
{ &#34; message&#34;:&#34;发生了错误。&#34;, &#34; exceptionMessage&#34;:&#34;对象引用未设置为对象的实例。&#34;, &#34; exceptionType&#34;:&#34; System.NullReferenceException&#34;, &#34; stackTrace&#34;:&#34; at testchatbotservice.BasicLuisDialog.d__19.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.LuisDialog
1.<DispatchToIntentHandler>d__9.MoveNext() in D:\\a\\1\\s\\CSharp\\Library\\Microsoft.Bot.Builder\\Dialogs\\LuisDialog.cs:line 270\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.LuisDialog
1.d__8.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Dialogs \ LuisDialog.cs:第241行\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime .CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume {{1} } 2.-PollAsync&gt; d__19.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Fibers \ Wait.cs:line 0 \ r \ n ---堆栈跟踪结束从先前的位置抛出异常--- \ r \ n在Syst em.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Internals.Fibers.Frame {{1 1.-PollAsync&gt; d__16.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Fibers \ Fiber.cs:line 0 \ r \ n ---堆栈结束从抛出异常的先前位置追踪--- \ r \ n在Microsoft.Bot.Builder.Internals.Fibers.Wait1.<Rest>d__5.MoveNext() in D:\\a\\1\\s\\CSharp\\Library\\Microsoft.Bot.Builder\\Dialogs\\DialogTask.cs:line 164\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Wait
1的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n中。 d _3.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Dialogs \ Chain.cs:第752行\ r \ n ---从前一个位置的堆栈跟踪结束,其中异常是抛出--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n,在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder .Dialogs.Internals.DialogTask.ThunkResume1.<Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop<C>-PollAsync>d__9.MoveNext() in D:\\a\\1\\s\\CSharp\\Library\\Microsoft.Bot.Builder\\Fibers\\Fiber.cs:line 0\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Internals.Fibers.Fiber
2.-PollAsync&GT; d__19.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Fibers \ Wait.cs:第0行\ r \ n ---从先前位置抛出异常的堆栈跟踪结束--- r \ n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Internals.Fibers。框架2.Microsoft.Bot.Builder.Internals.Fibers.IAwaiter<T>.GetResult() in D:\\a\\1\\s\\CSharp\\Library\\Microsoft.Bot.Builder\\Fibers\\Wait.cs:line 378\r\n at Microsoft.Bot.Builder.Dialogs.Chain.LoopDialog
1.-PollAsync&gt; d__16.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Fibers \ Fiber.cs:第240行\ r \ n-- - 从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n处于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的\ r \ n )\ r \ n在Microsoft的.Bot.Builder.Dialogs.Internals.DialogTask.d__23.MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Dialogs \ DialogTask.cs:line 321 \ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder.Dialogs.Internals.ReactiveDialogTask.d__3 .MoveNext()在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Dialogs \ DialogTask.cs:第376行\ r \ n ---从前一个位置抛出异常的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n,在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.Bot.Builder。 D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Dialogs \ ScoringEventLoop.cs中的Dialogs.Internals.ScoringEventLoop`1.d__5.MoveNext():第87行\ r \ n ---结束从抛出异常的先前位置堆栈跟踪--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessA处ndDebuggerNotification(任务任务)\ r \ n在D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft.Bot.Builder \ Dialogs \ DialogTask中的Microsoft.Bot.Builder.Dialogs.Internals.EventLoopDialogTask.d__3.MoveNext() .cs:第435行\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在系统中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n处于\ r \ n .Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n,位于D:\ a \ 1 \ s \ CSharp \ Library \ Microsoft中的Microsoft.Bot.Builder.Dialogs.Internals.SetAmbientThreadCulture.d__3.MoveNext()。 Bot.Builder \ ConnectorEx \ PostToBot.cs:第103行\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) )\ r \ n at
答案 0 :(得分:1)
在botframework模拟器中工作,但在我使用Web作为通道时却不行。发回卡时我得到BadRequest。
我使用以下示例代码进行测试,以使用图像的base64二进制文件发送缩略图卡,这适用于网络聊天频道。如果可能,您可以将imgUrl
替换为存储在数据库中的图像的base64string,并测试代码是否适合您。
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
int length = (activity.Text ?? string.Empty).Length;
if (activity.Text=="card")
{
var replymess = context.MakeMessage();
String imgUrl = ImageToBase64();
List <CardImage> cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url: imgUrl));
ThumbnailCard plCard = new ThumbnailCard()
{
Title = "I'm a thumbnail card",
Text = "test pic",
Images = cardImages
};
Attachment plAttachment = plCard.ToAttachment();
replymess.Attachments.Add(plAttachment);
await context.PostAsync(replymess);
}
else
{
await context.PostAsync($"You sent {activity.Text} which was {length} characters");
}
context.Wait(MessageReceivedAsync);
}
图片到Base64:
public static string ImageToBase64()
{
var path = System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\testpic.PNG");
Byte[] bytes = File.ReadAllBytes(path);
string base64String = Convert.ToBase64String(bytes);
return "data:image/png;base64," + base64String;
}
测试结果:
此外,请在不发送缩略图卡时检查您的机器人是否可以按预期工作。要解决问题,您可以尝试使用fiddler捕获并检查请求,这将有助于您获得有关“BadRequest”的更多有用和详细的错误消息。