【问题标题】:Create cards dynamically by looping a file in Microsoft Bot Framework通过在 Microsoft Bot Framework 中循环文件来动态创建卡片
【发布时间】:2017-12-30 13:44:45
【问题描述】:

我一直在试验 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 数组填充它?

我试过了

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

【问题讨论】:

  • 将这个包裹在 for/forEach 调用中?
  • 嘿@EzequielJadib 我尝试将“new builder.HeroCard()...”包装在 forEach 中,并返回一个空的附件数组。 { "type": "message", "attachmentLayout": "carousel", "attachments": [], "locale": "en-US", "localTimestamp": "2017-07-24T20:26:31.414Z", "from": { "id": "0db08g0j3blgl1jfmc", "name": "Bot" }, "recipient": { "id": "default-user", "name": "User" }, "inputHint": "acceptingInput", "id": null, "replyToId": "1kf4ad6jjihlk7k73" }
  • 请添加您的新代码
  • 是的,您的代码是错误的,您没有向数组添加任何内容。我会发布答案。

标签: node.js bots botframework


【解决方案1】:

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

试试这样的:

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

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 2019-07-26
    • 2017-03-03
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多