如何在套接字模式下从 slack 应用程序发送 HTTP POST 请求?

How do you send HTTP POST Requests from a slack app in socket mode?

我目前正在使用 JavaScript 创建一个 bolt slack 应用程序,它需要在单击按钮时向外部 webhook 发送 HTTP POST 请求。使用应用程序设置中提供的 slack webhook 从外部源向我的 slack 应用程序发送数据非常简单,但我似乎无法弄清楚如何从我的 app.js 文件发送消息。我的应用程序目前处于套接字模式,因为我不想在我的 Ngrok url 似乎无法在 slack 中使用验证后设置请求 URL,而且似乎没有关于这个问题的文档很多。在我找到的文档 https://api.slack.com/apis/connections/socket-implement#connect 中,我不确定如何将 apps.connections.open 实现到我的 JavaScript 文件中。我对 JavaScript 和 HTTP 请求还很陌生,所以非常感谢任何帮助。我也在使用 slack 的 Bolt 应用程序包,下面是我的代码。

const { App } = require('@slack/bolt');
// Initializes your app with your bot token and signing secret

const app = new App({
  token: "xoxb-",
  signingSecret: "",
  appToken: "xapp-",
  socketMode: true,
  port: process.env.PORT || 3000
});

app.message('Hey bot', async ({ message, say }) => {
  // say() sends a message to the channel where the event was triggered

  await say({
    "blocks": [
      {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": `You made a request <@${message.user}>?`
          }},
        {
            "type": "divider"
        },
        {
            "type": "input",
            "element": {
                "type": "plain_text_input",
                "action_id": "plain_text_input-action"
            },
            "label": {
                "type": "plain_text",
                "text": "Error Message",
                "emoji": true
            }
        },
        {
            "type": "input",
            "element": {
                "type": "plain_text_input",
                "multiline": true,
                "action_id": "plain_text_input-action"
            },
            "label": {
                "type": "plain_text",
                "text": "Description of Error",
                "emoji": true
            }
        },
        {
            "type": "input",
            "element": {
                "type": "plain_text_input",
                "action_id": "plain_text_input-action"
            },
            "label": {
                "type": "plain_text",
                "text": "Aditional notes?",
                "emoji": true
            }
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Submit",
                        "emoji": true
                    },
                    "value": "click_me_123",
                    "action_id": "button_click"
                }
            ]
        }
    ]
  });
});


app.action('button_click', async ({ body, ack, say }) => {
  // Acknowledge the action
  await ack();
  say("No Flagged Issues");

  // My HTTP POST REQUEST would go here, IF I HAD ANY!
}

});

(async () => {
  // Start your app
  await app.start(process.env.PORT || 3000);
  console.log('⚡️ Bolt app is running!');
})();

归根结底,Bolt 应用程序只是普通的旧 JS 应用程序,因此您不需要做任何特别的事情就可以从 app.action() 中创建 HTTP POST

有很多库,但其中一个比较流行的是 Axios。导入后,您的 app.action 方法中就会有这样的代码

axios.get('https://www.nodesource.com/')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

// Using async/await
async function getUser() {
  try {
    const response = await axios.get('https://www.nodesource.com/');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}

这个片段绝不是惯用的 JS,但它足以让你继续。