通过循环Microsoft Bot Framework中的文件动态创建卡

问题描述:

我一直在尝试使用Microsoftbot.dialog('showShirts',

I have been experimenting with Microsoftbot.dialog('showShirts',

function (session) {
    var msg = new builder.Message(session);
    msg.attachmentLayout(builder.AttachmentLayout.carousel)
    msg.attachments([
        new builder.HeroCard(session)
            .title("Classic White T-Shirt")
            .subtitle("100% Soft and Luxurious Cotton")
            .text("Price is $25 and carried in sizes (S, M, L, and XL)")
            .images([builder.CardImage.create(session, 'http://petersapparel.parseapp.com/img/whiteshirt.png')])
            .buttons([
                builder.CardAction.imBack(session, "buy classic white t-shirt", "Buy")
            ]),
        new builder.HeroCard(session)
            .title("Classic Gray T-Shirt")
            .subtitle("100% Soft and Luxurious Cotton")
            .text("Price is $25 and carried in sizes (S, M, L, and XL)")
            .images([builder.CardImage.create(session, 'http://petersapparel.parseapp.com/img/grayshirt.png')])
            .buttons([
                builder.CardAction.imBack(session, "buy classic gray t-shirt", "Buy")
            ])
    ]);
    session.send(msg).endDialog();
}).triggerAction({ matches: /^(show|list)/i }); bot framework in node js, 
i saw this sample code in the documentation

我的问题是代替手动键入 new builder.HeroCard()... ,我该如何创建一个循环以从json数组填充此循环?

My question is instead of manually typing new builder.HeroCard()... how can i create a loop to populate this from a json array?

我已经尝试过

var obj = require("./dummy_json");
msg.attachments([
    obj.shirts.forEach(function(data){
        new builder.HeroCard(session)
            .title(data.title)
            .subtitle(data.subtitle)
            .text(data.text)
            .images([builder.CardImage.create(session, data.image_path)])
            .buttons([
                builder.CardAction.imBack(session, data.title, "Buy")
            ])
    },this)
]);

问题是您正在执行循环,但似乎没有在数组中添加任何内容.

The problem is that you are doing the loop but it seems you are not adding anything to the array.

尝试这样的事情:

var attachments = [];
var obj = require("./dummy_json");

obj.shirts.forEach(function(data) {
    var card = new builder.HeroCard(session)
                    .title(data.title)
                    .subtitle(data.subtitle)
                    .text(data.text)
                    .images([builder.CardImage.create(session, data.image_path)])
                    .buttons([
                        builder.CardAction.imBack(session, data.title, "Buy")
                    ])

     attachments.push(card); 
},this)

msg.attachments(attachments);