簡體   English   中英

通過循環Microsoft Bot Framework中的文件動態創建卡

[英]Create cards dynamically by looping a file in Microsoft Bot Framework

我一直在嘗試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

我的問題是代替手動鍵入新的builder.HeroCard()...如何創建循環以從json數組填充此循環?

我已經試過了

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)
]);

問題是您正在執行循環,但似乎沒有在數組中添加任何內容。

嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM