且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用c#Bot Framework创建图表?

更新时间:2023-02-08 15:15:04

BotFramework当前不支持交互式图表,您可以使用@Peter Bons建议的一些第三方库或在线服务来生成图表,但是您将需要尝试将其渲染为图像文件,并使用bot中的HeroCard/AdaptiveCard将此文件附加到您的消息中.

Interactive charts are not currently supported in BotFramework, you can use some third-party libs as @Peter Bons suggested or on-line services to generate your chart, but you will need to try to render it as an image file and attach this file to your message using HeroCard/ AdaptiveCard in bot.

由于您的数据在Finance.Yahoo.com中,因此我不确定Yahoo是否支持生成图表图像,否则,您将需要从Yahoo获取数据,然后尝试查找在线服务或第三方-party lib首先绘制图表图像.

Since your data is in finance.Yahoo.com, I'm not sure if Yahoo supports to generate chart image, if not, you will need to get the data from Yahoo and then try to find an online-service or third-party lib to draw the chart image first.

渲染图像后,可以将其作为图像附件发送 例如这样的

After the image is rendered, you can send it as image attachment for example like this:

[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;

        var card = CreateHeroCard();
        Attachment attachment = card.ToAttachment();
        var message = context.MakeMessage();
        message.Attachments.Add(attachment);

        await context.PostAsync(message);

        context.Wait(MessageReceivedAsync);
    }

    private HeroCard CreateHeroCard()
    {
        List<CardImage> cardImages = new List<CardImage>();
        cardImages.Add(new CardImage("your chart image url goes here"));
        var card = new HeroCard()
        {
            Title = "Months with Numbers Bar Chart",
            Subtitle = "Using a Chart as Image service...",
            Text = "Build and connect intelligent bots that have charts rendered as images.",
            Images = cardImages
        };

        return card;
    }
}