繁体   English   中英

Slack Bot with Bolt for JavaScript 和 OAuth 2.0 与其他工作区共享应用程序

[英]Slack Bot with Bolt for JavaScript and OAuth 2.0 to share the app with other workspaces

我使用 Bolt for Javascript 添加了一个带有 Add to Slack 按钮的应用程序。 Slack 命令还不起作用,因为我还没有带有身份验证令牌的数据库。 我计划实现它,但意识到我从来没有看到控制台日志。

所以据我了解,console.log("authorizeFn") 应该可以工作

const { App, ExpressReceiver } = require("@slack/bolt");;

const expressReceiver = new ExpressReceiver({
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  endpoints: "/events"
});

const authorizeFn = async ({ teamId }) => {
  //Implement query of database looking for teamId, getting auth token... 

  console.log("authorizeFn") //This one never logs???
};

const app = new App({
  authorize: authorizeFn,
  receiver: expressReceiver
});

const app_express = expressReceiver.app;

如果用户被授权,它应该检查每个事件,对吗?

代码是这样继续的

/* Page with add button, can be implemented in website instead */
app_express.get("/auth/add", (req, res, next) => {
  res.write(
    '<a href="https:/...'
  );
  res.end();
});
app_express.get("/auth/direct", (req, res, next) => {
  res.redirect(
    "https://slack...."
  );
  res.end();
});
/* Thanks for installing! */
app_express.get("/auth/thanks", (req, res, next) => {
  res.write("Thanks for installing!");
  res.end();
});

/* oauth callback function */
app_express.get("/auth/callback", (req, res, next) => {
  let code = req.query.code;

  let state = req.query.state;

  return app.client.oauth.v2
    .access({
      client_id: process.env.SLACK_CLIENT_ID,
      client_secret: process.env.SLACK_CLIENT_SECRET,
      code: code
    })
    .then(async result => {
      console.log(result);
      // save result of oauth.access call somewhere, like in a database.

      res.redirect(process.env.BASE_DOMAIN + "/auth/thanks");
      res.end();
    })
    .catch(error => {
      throw error;
    });
});

控制台.log(结果); 记录一些有用的东西,看起来像 teamId、用户和令牌

它必须是

const expressReceiver = new ExpressReceiver({
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  endpoints: "/slack/events"
});

就我而言。 不是:

  endpoints: "/events"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM