2017-07-24 109 views
2

循环文件我一直在尝试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) 
]); 
+0

总结呢? –

+0

嘿@EzequielJadib我尝试在forEach中包装“new builder.HeroCard()...”,并返回一个空的附件数组。 '{ “类型”: “消息”, “attachmentLayout”: “转盘”, “附件”:[], “区域设置”: “的en-US”, “LOCALTIMESTAMP”:“2017-07 -24T20:26:31.414Z “ ”从“:{ ”ID“: ”0db08g0j3blgl1jfmc“, ”名“: ”博特“ }, ”收件人“:{ ”ID“:” 默认用户” “名”: “用户” }, “inputHint”: “acceptingInput”, “ID”:无效, “replyToId”: “1kf4ad6jjihlk7k73” }' –

+0

添加你的新代码,请 –

回答

2

的问题是使y你正在做循环,但似乎你没有添加任何东西到数组中。

尝试是这样的:一个用于/的forEach调用内部

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

非常感谢你。 –