-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
441 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,85 @@ | ||
Follow the instructions [here](https://slack.dev/bolt-python/concepts#authenticating-oauth) for configuring OAuth flow supported Slack apps. This example works with the default env variables such as `SLACK_CLIENT_ID`, `SLACK_CLIENT_SECRET`, `SLACK_SCOPES`, `SLACK_SIGNING_SECRET`, and so forth. | ||
## Bolt for Python - Django integration example | ||
|
||
This example demonstrates how you can use Bolt for Python in your Django application. The project consists of two apps. | ||
|
||
### `simple_app` - Single-workspace App Example | ||
|
||
If you want to run a simple app like the one you've tried in the [Getting Started Guide](https://slack.dev/bolt-python/tutorial/getting-started), this is the right one for you. By default, this Django project runs this application. If you want to switch to OAuth flow supported one, modify `myslackapp/urls.py`. | ||
|
||
To run this app, all you need to do are: | ||
|
||
* Create a new Slack app configuration at https://api.slack.com/apps?new_app=1 | ||
* Go to "OAuth & Permissions" | ||
* Add `app_mentions:read`, `chat:write` in Scopes > Bot Token Scopes | ||
* Go to "Install App" | ||
* Click "Install to Workspace" | ||
* Complete the installation flow | ||
* Copy the "Bot User OAuth Token" value, which starts with `xoxb-` | ||
|
||
You can start your Django application this way: | ||
|
||
```bash | ||
python -m venv .venv | ||
source .venv/bin/activate | ||
pip install -U pip | ||
pip install -r requirements.txt | ||
|
||
export SLACK_SIGNING_SECRET=(You can find this value at Settings > Basic Information > App Credentials > Signing Secret) | ||
export SLACK_BOT_TOKEN=(You can find this value at Settings > Install App > Bot User OAuth Token) | ||
|
||
python manage.py migrate | ||
python manage.py runserver 0.0.0.0:3000 | ||
``` | ||
|
||
As you did at [Getting Started Guide](https://slack.dev/bolt-python/tutorial/getting-started), configure ngrok or something similar to serve a public endpoint. Lastly, | ||
|
||
* Go back to the Slack app configuration page | ||
* Go to "Event Subscriptions" | ||
* Turn the feature on | ||
* Set the "Request URL" to `https://{your public domain}/slack/events` | ||
* Go to the Slack workspace you've installed this app | ||
* Invite the app's bot user to a channel | ||
* Mention the bot user in the channel | ||
* You'll see a reply from your app's bot user! | ||
|
||
### `oauth_app` - Multiple-workspace App Example (OAuth flow supported) | ||
|
||
By default, this Django project runs this application. If you want to switch to OAuth flow supported one, modify `myslackapp/urls.py`. | ||
|
||
This example uses SQLite. If you are looking for an example using MySQL, check the `mysql-docker-compose.yml` and the comment in `myslackapp/settings.py`. | ||
|
||
|
||
To run this app, all you need to do are: | ||
|
||
* Create a new Slack app configuration at https://api.slack.com/apps?new_app=1 | ||
* Go to "OAuth & Permissions" | ||
* Add `app_mentions:read`, `chat:write` in Scopes > Bot Token Scopes | ||
* Follow the instructions [here](https://slack.dev/bolt-python/concepts#authenticating-oauth) for configuring OAuth flow supported Slack apps | ||
|
||
You can start your Django application this way: | ||
|
||
```bash | ||
python -m venv .venv | ||
source .venv/bin/activate | ||
pip install -U pip | ||
pip install -r requirements.txt | ||
export SLACK_CLIENT_ID= | ||
export SLACK_CLIENT_SECRET= | ||
export SLACK_SCOPES=commands,chat:write | ||
export SLACK_SIGNING_SECRET= | ||
|
||
export SLACK_SIGNING_SECRET=(You can find this value at Settings > Basic Information > App Credentials > Signing Secret) | ||
export SLACK_CLIENT_ID=(You can find this value at Settings > Basic Information > App Credentials > Client ID) | ||
export SLACK_CLIENT_SECRET=(You can find this value at Settings > Basic Information > App Credentials > Client Secret) | ||
export SLACK_SCOPES=app_mentions:read,chat:write | ||
|
||
python manage.py migrate | ||
python manage.py runserver 0.0.0.0:3000 | ||
``` | ||
|
||
As you did at [Getting Started Guide](https://slack.dev/bolt-python/tutorial/getting-started), configure ngrok or something similar to serve a public endpoint. Lastly, | ||
|
||
* Go back to the Slack app configuration page | ||
* Go to "Event Subscriptions" | ||
* Turn the feature on | ||
* Set the "Request URL" to `https://{your public domain}/slack/events` | ||
* Visit `https://{your public domain}/slack/install` and complete the installation flow | ||
* Invite the app's bot user to a channel | ||
* Mention the bot user in the channel | ||
* You'll see a reply from your app's bot user! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
ASGI config for myslackapp project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myslackapp.settings") | ||
|
||
application = get_asgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""myslackapp URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/3.2/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.contrib import admin | ||
from django.urls import path | ||
|
||
is_simple_app = True | ||
|
||
if is_simple_app: | ||
# A simple app that works only for a single Slack workspace | ||
# (prerequisites) | ||
# export SLACK_BOT_TOKEN= | ||
# export SLACK_SIGNING_SECRET= | ||
from simple_app.urls import slack_events_handler | ||
|
||
urlpatterns = [path("slack/events", slack_events_handler)] | ||
else: | ||
# OAuth flow supported app | ||
# (prerequisites) | ||
# export SLACK_CLIENT_ID= | ||
# export SLACK_CLIENT_SECRET= | ||
# export SLACK_SIGNING_SECRET= | ||
# export SLACK_SCOPES=app_mentions:read | ||
from oauth_app.urls import slack_events_handler, slack_oauth_handler | ||
|
||
urlpatterns = [ | ||
path("slack/events", slack_events_handler, name="handle"), | ||
path("slack/install", slack_oauth_handler, name="install"), | ||
path("slack/oauth_redirect", slack_oauth_handler, name="oauth_redirect"), | ||
] |
6 changes: 3 additions & 3 deletions
6
examples/django/slackapp/wsgi.py → examples/django/myslackapp/wsgi.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
""" | ||
WSGI config for slackapp project. | ||
WSGI config for myslackapp project. | ||
It exposes the WSGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ | ||
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "slackapp.settings") | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myslackapp.settings") | ||
|
||
application = get_wsgi_application() |
File renamed without changes.
Oops, something went wrong.