如何在安装 Slack 应用程序后立即向用户发送消息并创建频道

How to send user a message and create a channel right after installation of Slack app

如标题所示,我想知道我们如何:

用户在工作区中首次安装 Slack 应用程序(可分发应用程序)之后?

之前有人问过类似的问题,但是the answer was too concise, and I wonder if someone could be so kind to give a hint using this sample Django Slack app code?干杯。

在您的应用程序的 'OAuth & Permissions' 页面上,有一个部分用于 'Redirect URLs'

它的作用是当用户安装应用程序时,它会重定向到用户实现的端点。

OAuth 流程

  1. 当用户使用 'Add To Slack' 按钮或使用 https://slack.com/oauth/authorize

    安装应用程序时


    详情在这里:https://api.slack.com/legacy/oauth#authenticating-users-with-oauth__the-oauth-flow__step-1---sending-users-to-authorize-andor-install

  2. 生成的代码被重定向到您指定的端点 - “重定向 URL” https://api.slack.com/legacy/oauth#authenticating-users-with-oauth__the-oauth-flow__step-2---users-are-redirected-to-your-server-with-a-verification-code

  3. 解决方案:现在你需要实现这个端点来生成访问令牌 https://api.slack.com/legacy/oauth#authenticating-users-with-oauth__the-oauth-flow__step-3---exchanging-a-verification-code-for-an-access-token

  • 生成令牌后,您可以将代码写入:
    1. 创建频道
    2. 向用户发送初始消息

看完Suyash的讲解,再次仔细查看了bolt-python为Django提供的示例代码,体会到:

  1. bolt-python 负责 OAuth 流程,我们只需要在应用程序面板中指定正确的 URL(用于重定向 URL 以及事件订阅、交互性等)。
  2. 可以做的是在 DjangoInstallationStore.save 方法中执行所有这些所需的安装操作,因为那是安装最终完成的时候。在我的例子中,我创建了一个 SlackManager 和一个特定的方法,我可以使用它(通过 local import)来执行 post 安装操作。