node.js - 通过在 Microsoft Bot Framework 中循环文件来动态创建卡片

标签 node.js bots botframework

我一直在尝试 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

我的问题是,我如何创建一个循环来从 json 数组填充它,而不是手动输入 new builder.HeroCard()...

我已经尝试过了

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

关于node.js - 通过在 Microsoft Bot Framework 中循环文件来动态创建卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289623/

相关文章:

node.js - 如何跟踪和处理 Node 中的事件

node.js - 等待时运行其他代码

artificial-intelligence - BotBuilder - FormFlow - 如何更改确认选项语言

bots - Slack channel 不适用于 MS Bot Framework

botframework - 从外部服务关闭团队消息传递扩展模式

javascript - 如何: MS Bot directline messaging using a websocket

Javascript promise 好奇心

node.js - _read() 未在可读流上实现

discord - 当我们进行私信时如何在discord js中获取权限

security - 一个只说 "Mozilla/4.0"的用户代理是一个机器人,对吧?