打印出数组而不将其字符串化为Slack

时间:2018-12-12 15:02:49

标签: node.js slack

我正在尝试返回我正在创建的数组。 问题是我不想使用stringify,因为它返回括号。 一直在尝试不同的方法,但没有真正起作用,而且我不确定是否存在Slacks问题,或者在我的代码中是否= /

这就是我所拥有的。

let elements = [];
          for (let i in response.options) {
            let opt = response.options[i];
            elements.push(
              { 
                "actions"   : [
                  {
                      "name": opt.label,
                      "text": opt.label,
                      "type": "button",
                      "value": opt.value.input.text
                  }]
              }
            );
          }             
          payload = {
            "text": "Would you like to book?",
            attachments: [
              {
                "text": "Choose",
                "fallback": "You are unable to choose a game",
                "callback_id": "wopr_game",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "actions": elements
              }
            ] 
          };
          console.log(payload)

我的控制台返回此值。

{ text: 'Would you like to book?',
  attachments:
   [ { text: 'Choose',
       fallback: 'You are unable to choose a game',
       callback_id: 'wopr_game',
       color: '#3AA3E3',
       attachment_type: 'default',
       actions: [Array] } ] }

然后进行字符串化

{ text: 'Would you like to book?',
  attachments:
   [ { text: 'Choose',
       fallback: 'You are unable to choose a game',
       callback_id: 'wopr_game',
       color: '#3AA3E3',
       attachment_type: 'default',
       actions: '{"actions":{"name":"Book transport","text":"Book transport","type":"button","value":"transport"}},{"actions":{"name":"Book hotel","text":"Book hotel","type":"button","value":"hotel"}},{"actions":{"name":"Travel Policy","text":"Travel Policy","type":"button","value":"Travel Policy"}}' } ] }

关于我如何只返回文本以便松弛显示按钮的任何想法?

谢谢!

更新: 如果我在有效负载中添加了硬编码解决方案,则松弛按钮将返回正确的结果。

  payload = {
            attachments: [
              {
                title: 'Do you want to interact with my buttons?',
                callback_id: '123',
                attachment_type: 'default',
                actions: [
                  {
                    "name": "yes",
                    "text": "Yes",
                    "value": "yes",
                    "type": "button",
                  },
                  {
                    "name": "no",
                    "text": "No",
                    "value": "no",
                    "type": "button",
                  }
                ]
              }
            ]
          };

1 个答案:

答案 0 :(得分:0)

因此,您想将有效负载的“ elements”属性设置为数组内对象的“ actions.text”属性中的值吗?听起来像Array.map的工作:)在有效负载上设置元素时,请尝试以下操作:

"elements": elements.map(el => el.actions.text)