Skip to content

Commit 95c64f2

Browse files
authored
Initial commit
0 parents  commit 95c64f2

27 files changed

+624
-0
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 125
3+
exclude = .gitignore,venv

.github/CODEOWNERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code owners are the default owners for everything in
2+
# this repository. The owners listed below will be requested for
3+
# review when a pull request is opened.
4+
# To add code owner(s), uncomment the line below and
5+
# replace the @global-owner users with their GitHub username(s).
6+
# * @global-owner1 @global-owner2

.github/dependabot.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "pip"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"
7+
labels:
8+
- "pip"
9+
- "dependencies"
10+
- package-ecosystem: "github-actions"
11+
directory: "/"
12+
schedule:
13+
interval: "monthly"

.github/workflows/black.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Formatting validation using black
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 5
12+
strategy:
13+
matrix:
14+
python-version: ['3.9']
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: |
24+
pip install -U pip
25+
pip install -r requirements.txt
26+
- name: Format with black
27+
run: |
28+
black .
29+
if git status --porcelain | grep .; then git --no-pager diff; exit 1; fi

.github/workflows/flake8.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Linting validation using flake8
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 5
12+
strategy:
13+
matrix:
14+
python-version: ['3.9']
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install dependencies
23+
run: |
24+
pip install -U pip
25+
pip install -r requirements.txt
26+
- name: Lint with flake8
27+
run: |
28+
flake8 *.py && flake8 listeners/

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# general things to ignore
2+
build/
3+
dist/
4+
docs/_sources/
5+
docs/.doctrees
6+
.eggs/
7+
*.egg-info/
8+
*.egg
9+
*.py[cod]
10+
__pycache__/
11+
*.so
12+
*~
13+
14+
# virtualenv
15+
env*/
16+
venv/
17+
.venv*
18+
.env*
19+
20+
# codecov / coverage
21+
.coverage
22+
cov_*
23+
coverage.xml
24+
25+
# due to using tox and pytest
26+
.tox
27+
.cache
28+
.pytest_cache/
29+
.python-version
30+
pip
31+
.mypy_cache/
32+
33+
# misc
34+
tmp.txt
35+
.DS_Store
36+
logs/
37+
*.db
38+
.pytype/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Slack Technologies, LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Bolt for Python Template App
2+
3+
This is a generic Bolt for Python template app used to build out Slack apps.
4+
5+
Before getting started, make sure you have a development workspace where you have permissions to install apps. If you don’t have one setup, go ahead and [create one](https://slack.com/create).
6+
## Installation
7+
8+
#### Create a Slack App
9+
1. Open [https://api.slack.com/apps/new](https://api.slack.com/apps/new) and choose "From an app manifest"
10+
2. Choose the workspace you want to install the application to
11+
3. Copy the contents of [manifest.json](./manifest.json) into the text box that says `*Paste your manifest code here*` (within the JSON tab) and click *Next*
12+
4. Review the configuration and click *Create*
13+
5. Click *Install to Workspace* and *Allow* on the screen that follows. You'll then be redirected to the App Configuration dashboard.
14+
15+
#### Environment Variables
16+
Before you can run the app, you'll need to store some environment variables.
17+
18+
1. Open your apps configuration page from this list, click **OAuth & Permissions** in the left hand menu, then copy the Bot User OAuth Token. You will store this in your environment as `SLACK_BOT_TOKEN`.
19+
2. Click ***Basic Information** from the left hand menu and follow the steps in the App-Level Tokens section to create an app-level token with the `connections:write` scope. Copy this token. You will store this in your environment as `SLACK_APP_TOKEN`.
20+
21+
```zsh
22+
# Replace with your app token and bot token
23+
export SLACK_BOT_TOKEN=<your-bot-token>
24+
export SLACK_APP_TOKEN=<your-app-token>
25+
```
26+
27+
### Setup Your Local Project
28+
```zsh
29+
# Clone this project onto your machine
30+
git clone https://github.com/slack-samples/bolt-python-starter-template.git
31+
32+
# Change into this project directory
33+
cd bolt-python-starter-template
34+
35+
# Setup your python virtual environment
36+
python3 -m venv .venv
37+
source .venv/bin/activate
38+
39+
# Install the dependencies
40+
pip install -r requirements.txt
41+
42+
# Start your local server
43+
python3 app.py
44+
```
45+
46+
#### Linting
47+
```zsh
48+
# Run flake8 from root directory for linting
49+
flake8 *.py && flake8 listeners/
50+
51+
# Run black from root directory for code formatting
52+
black .
53+
```
54+
55+
## Project Structure
56+
57+
### `manifest.json`
58+
59+
`manifest.json` is a configuration for Slack apps. With a manifest, you can create an app with a pre-defined configuration, or adjust the configuration of an existing app.
60+
61+
### `app.py`
62+
63+
`app.py` is the entry point for the application and is the file you'll run to start the server. This project aims to keep this file as thin as possible, primarily using it as a way to route inbound requests.
64+
65+
### `/listeners`
66+
67+
Every incoming request is routed to a "listener". Inside this directory, we group each listener based on the Slack Platform feature used, so `/listeners/shortcuts` handles incoming [Shortcuts](https://api.slack.com/interactivity/shortcuts) requests, `/listeners/views` handles [View submissions](https://api.slack.com/reference/interaction-payloads/views#view_submission) and so on.
68+
69+
## App Distribution / OAuth
70+
71+
Only implement OAuth if you plan to distribute your application across multiple workspaces. A separate `app_oauth.py` file can be found with relevant OAuth settings.
72+
73+
When using OAuth, Slack requires a public URL where it can send requests. In this template app, we've used [`ngrok`](https://ngrok.com/download). Checkout [this guide](https://ngrok.com/docs#getting-started-expose) for setting it up.
74+
75+
Start `ngrok` to access the app on an external network and create a redirect URL for OAuth.
76+
77+
```
78+
ngrok http 3000
79+
```
80+
81+
This output should include a forwarding address for `http` and `https` (we'll use `https`). It should look something like the following:
82+
83+
```
84+
Forwarding https://3cb89939.ngrok.io -> http://localhost:3000
85+
```
86+
87+
Navigate to **OAuth & Permissions** in your app configuration and click **Add a Redirect URL**. The redirect URL should be set to your `ngrok` forwarding address with the `slack/oauth_redirect` path appended. For example:
88+
89+
```
90+
https://3cb89939.ngrok.io/slack/oauth_redirect
91+
```

app.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
import logging
3+
4+
from slack_bolt import App
5+
from slack_bolt.adapter.socket_mode import SocketModeHandler
6+
7+
from listeners import register_listeners
8+
9+
# Initialization
10+
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
11+
logging.basicConfig(level=logging.DEBUG)
12+
13+
# Register Listeners
14+
register_listeners(app)
15+
16+
# Start Bolt app
17+
if __name__ == "__main__":
18+
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN")).start()

app_oauth.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
import os
3+
from slack_bolt import App, BoltResponse
4+
from slack_bolt.oauth.callback_options import CallbackOptions, SuccessArgs, FailureArgs
5+
from slack_bolt.oauth.oauth_settings import OAuthSettings
6+
7+
from slack_sdk.oauth.installation_store import FileInstallationStore
8+
from slack_sdk.oauth.state_store import FileOAuthStateStore
9+
10+
from listeners import register_listeners
11+
12+
logging.basicConfig(level=logging.DEBUG)
13+
14+
15+
# Callback to run on successful installation
16+
def success(args: SuccessArgs) -> BoltResponse:
17+
# Call default handler to return an HTTP response
18+
return args.default.success(args)
19+
# return BoltResponse(status=200, body="Installation successful!")
20+
21+
22+
# Callback to run on failed installation
23+
def failure(args: FailureArgs) -> BoltResponse:
24+
return args.default.failure(args)
25+
# return BoltResponse(status=args.suggested_status_code, body=args.reason)
26+
27+
28+
# Initialization
29+
app = App(
30+
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
31+
installation_store=FileInstallationStore(),
32+
oauth_settings=OAuthSettings(
33+
client_id=os.environ.get("SLACK_CLIENT_ID"),
34+
client_secret=os.environ.get("SLACK_CLIENT_SECRET"),
35+
scopes=["channels:history", "chat:write", "commands"],
36+
user_scopes=[],
37+
redirect_uri=None,
38+
install_path="/slack/install",
39+
redirect_uri_path="/slack/oauth_redirect",
40+
state_store=FileOAuthStateStore(expiration_seconds=600),
41+
callback_options=CallbackOptions(success=success, failure=failure),
42+
),
43+
)
44+
45+
# Register Listeners
46+
register_listeners(app)
47+
48+
# Start Bolt app
49+
if __name__ == "__main__":
50+
app.start(3000)

0 commit comments

Comments
 (0)