0

我正在研究 Slack Bolt 框架,并创建了一个非常简单的应用程序,可以使用斜杠命令。当我输入“/cep”时,会出现以下屏幕:

打印屏幕

单击按钮时如何获取输入值字段?

我正在使用带有 Javascript 的 Bolt 框架。

这里是屏幕代码:

/ Listen for a slash command invocation 'Busca de CEP'
app.command('/cep', async ({ command, ack, say }) => {
  
  // Acknowledge the command request
  await ack();
  
  await say(
    {
        "blocks": [
            {
                "type": "header",
                "text": {
                    "type": "plain_text",
                    "text": " Busca de Endereço",
                    "emoji": true
                }
            },
            {
                "type": "divider"
            },
            {
                "type": "section",
                "text": {
                    "type": "plain_text",
                    "text": "Digite o CEP que deseja pesquisar:",
                    "emoji": true
                }
            },
            {
                "type": "input",
                "element": {
                    "type": "plain_text_input",
                    "action_id": "plain_text_input-action"
                },
                "label": {
                    "type": "plain_text",
                    "text": " ",
                    "emoji": true
                }
            },
            {
                "type": "actions",
                "elements": [
                    {
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "text": "Buscar",
                            "emoji": true
                        },
                        "value": "submitCEPButton",
                        "action_id": "submitCEPButton"
                    }
                ]
            }
        ]
    }
  )
  
});

这里是斜杠命令代码:

/ Action listener function called when an interactive component with action_id of “click_me_button” is triggered
app.action('submitCEPButton', async ({ ack, body, client, say }) => {
  // Acknowledge action request before anything else
  await ack();
  
  let channelID = body.channel.id
  let userID = body.user.id
  
  // Respond to action with an ephemeral message
  await client.chat.postEphemeral({
    channel: channelID,
    user: userID,
    text: `<@${userID}> clicked the button! `
  });
});

更新

输入斜杠命令“/cep”时的屏幕代码

app.command('/cep', async ({ command, ack, say }) => {
  
  // Acknowledge the command request
  await ack();
  
  await say(
    {
        "blocks": [
            {
                "type": "header",
                "block_id": "headerBlock",
                "text": {
                    "type": "plain_text",
                    "text": " Busca de Endereço",
                    "emoji": true
                }
            },
            {
                "type": "divider",
                "block_id": "dividerBlock",
            },
            {
                "type": "section",
                "block_id": "sectionBlock",
                "text": {
                    "type": "plain_text",
                    "text": "Digite o CEP que deseja pesquisar:",
                    "emoji": true
                }
            },
            {
                "type": "input",
                "block_id": "inputBlock",
                "element": {
                    "type": "plain_text_input",
                    "action_id": "inputCEP"
                },
                "label": {
                    "type": "plain_text",
                    "text": " ",
                    "emoji": false
                }
            },
            {
                "type": "actions",
                "block_id": "submitBlock",
                "elements": [
                    {
                        "type": "button",
                        "text": {
                            "type": "plain_text",
                            "text": "Buscar",
                            "emoji": true
                        },
                        "value": "submitCEPButton",
                        "action_id": "submitCEPButton"
                    }
                ]
            }
        ]
    }
  )
  
});

单击按钮时的命令

// Action listener function called when an interactive component with action_id of “click_me_button” is triggered
app.action('submitCEPButton', async ({ ack, body, client, say }) => {
  // Acknowledge action request before anything else
  await ack();
  
  let channelID = body.channel.id
  let userID = body.user.id
  console.log(body.state.values)
});

控制台打印的结果:

{
  njQfY: {
    'plain_text_input-action': { type: 'plain_text_input', value: 'abc123' }
  }
}
4

1 回答 1

0


您需要view.state.values从 view_submission 有效负载中获取。


参考:https ://api.slack.com/reference/interaction-payloads/views

您需要关注block_id 和 action_id因为这可能很棘手。

于 2022-01-08T18:03:48.040 回答