slackapi / bolt-python

A framework to build Slack apps using Python
https://slack.dev/bolt-python/
MIT License
1.04k stars 238 forks source link

How do I map a user from my service to a team ID? #1111

Closed aryabhatta-dey closed 1 month ago

aryabhatta-dey commented 1 month ago

Hey, I am using the Django example from here. I want to be able to map the user who is installing the Slack bot from my site to the team ID they are installing it to. How can I achieve this?

I have found these related threads but am unsure how to implement the same functionality in the Django sample app:

  1. https://github.com/slackapi/python-slack-sdk/issues/1233
  2. https://github.com/slackapi/bolt-js/issues/1541
WilliamBergamin commented 1 month ago

Hi @aryabhatta-dey thanks for writing in!

Have you looked at the django/oauth_app exemple it implements OAuth and stores the user installation information in djando.db models this should allow you to figure out where and by who your application is being installed.

aryabhatta-dey commented 1 month ago

Hey thank you for the prompt reply. I have taken a look at it. There is possibly some miscommunication. Please let me clarify - what I want to do is map the slack team id to an external user_uuid (generated from my service which is external to slack and hence the user_uuid here has no relation to slack's user_id).

WilliamBergamin commented 1 month ago

Would you be able to share a bit more about how your app plans to create this relationship?

Looking closer at the InstallationStore Django model each slack user entry contains the information regarding the specific team and enterprise the OAuth handshake was done with. You should be able map this information to your external user_uuid.

aryabhatta-dey commented 1 month ago

I was hoping there would be a way to achieve what's mentioned here - https://github.com/slackapi/bolt-js/issues/1541

WilliamBergamin commented 1 month ago

I see, there is no documentation explaining how to persist data in the OAuth flow. Similarly to bolt-js you can use cookies to persist data in the OAuth flow

The following is a modified version of the django oauth app that uses cookies to persist data in the Oauth flow

from django.urls import path

from django.http import HttpRequest
from django.views.decorators.csrf import csrf_exempt

from slack_bolt.adapter.django import SlackRequestHandler
from .slack_listeners import app

handler = SlackRequestHandler(app=app)

@csrf_exempt
def slack_events_handler(request: HttpRequest):
    return handler.handle(request)

def slack_oauth_install_handler(request: HttpRequest):
    # Get the user_uuid with the request and your specific logic
    user_uuid = db.lookup(request)
    django_response = handler.handle(request)
    django_response.set_cookie('user_uuid', user_uuid)
    return django_response

def slack_oauth_redirect_handler(request: HttpRequest):
    # this request should contain the slack user information including the team_id
    user_uuid = request.COOKIES['user_uuid']
    return handler.handle(request)

urlpatterns = [
    path("slack/events", slack_events_handler, name="handle"),
    path("slack/install",slack_oauth_install_handler, name="install"),
    path("slack/oauth_redirect",slack_oauth_redirect_handler, name="oauth_redirect"),
]

You should be able to use the state and StateStore to accomplish a similar behavior without using cookies

Let me know if this resolves your issue

aryabhatta-dey commented 1 month ago

Hey this resolved it. Thank you for the help! Appreciate it!