generated from slack-samples/bolt-python-custom-step-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 8eddff9
Showing
14 changed files
with
323 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length = 125 |
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,6 @@ | ||
# Code owners are the default owners for everything in | ||
# this repository. The owners listed below will be requested for | ||
# review when a pull request is opened. | ||
# To add code owner(s), uncomment the line below and | ||
# replace the @global-owner users with their GitHub username(s). | ||
# * @global-owner1 @global-owner2 |
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,9 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: "pip" | ||
directory: "/" | ||
schedule: | ||
interval: "monthly" | ||
labels: | ||
- "pip" | ||
- "dependencies" |
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,29 @@ | ||
name: Formatting validation using black | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
strategy: | ||
matrix: | ||
python-version: ['3.9'] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
pip install -U pip | ||
pip install -r requirements.txt | ||
- name: Format with black | ||
run: | | ||
black . | ||
if git status --porcelain | grep .; then git --no-pager diff; exit 1; fi |
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,28 @@ | ||
name: Linting validation using flake8 | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
strategy: | ||
matrix: | ||
python-version: ['3.9'] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
pip install -U pip | ||
pip install -r requirements.txt | ||
- name: Lint with flake8 | ||
run: | | ||
flake8 *.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# general things to ignore | ||
build/ | ||
dist/ | ||
docs/_sources/ | ||
docs/.doctrees | ||
.eggs/ | ||
*.egg-info/ | ||
*.egg | ||
*.py[cod] | ||
__pycache__/ | ||
*.so | ||
*~ | ||
|
||
# virtualenv | ||
env*/ | ||
venv/ | ||
.venv* | ||
.env* | ||
|
||
# codecov / coverage | ||
.coverage | ||
cov_* | ||
coverage.xml | ||
|
||
# due to using tox and pytest | ||
.tox | ||
.cache | ||
.pytest_cache/ | ||
.python-version | ||
pip | ||
.mypy_cache/ | ||
|
||
# misc | ||
tmp.txt | ||
.DS_Store | ||
logs/ | ||
*.db | ||
.pytype/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Slack Technologies, LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,63 @@ | ||
# Bolt for Python Automation Template App | ||
|
||
This is a generic Bolt for Python template app used to build out Slack automations. | ||
|
||
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). | ||
|
||
## Installation | ||
|
||
#### Create a Slack App | ||
|
||
1. Open [https://api.slack.com/apps/new](https://api.slack.com/apps/new) and choose "From an app manifest" | ||
2. Choose the workspace you want to install the application to | ||
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* | ||
4. Review the configuration and click *Create* | ||
5. Click *Install to Workspace* and *Allow* on the screen that follows. You'll then be redirected to the App Configuration dashboard. | ||
|
||
#### Environment Variables | ||
|
||
Before you can run the app, you'll need to store some environment variables. | ||
|
||
1. Rename `.env.sample` to `.env` | ||
2. Open your apps configuration page from [this list](https://api.slack.com/apps), click *OAuth & Permissions* in the left hand menu, then copy the *Bot User OAuth Token* into your `.env` file under `SLACK_BOT_TOKEN` | ||
3. 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 that token into your `.env` as `SLACK_APP_TOKEN`. | ||
|
||
### Setup Your Local Project | ||
|
||
```zsh | ||
# Clone this project onto your machine | ||
git clone https://github.com/slack-samples/bolt-python-automation-template.git | ||
|
||
# Change into this project directory | ||
cd bolt-python-automation-template | ||
|
||
# Set up virtual environment | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
|
||
# Install dependencies | ||
pip install -r requirements.txt | ||
|
||
# Run Bolt server | ||
slack run | ||
``` | ||
|
||
#### Linting | ||
|
||
```zsh | ||
# Run flake8 from root directory for linting | ||
flake8 *.py | ||
|
||
# Run black from root directory for code formatting | ||
black . | ||
``` | ||
|
||
## Project Structure | ||
|
||
### `manifest.json` | ||
|
||
`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. | ||
|
||
### `app.py` | ||
|
||
`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. |
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,58 @@ | ||
import os | ||
import logging | ||
from slack_bolt import Ack, App, BoltContext, Say, Complete, Fail | ||
from slack_bolt.adapter.socket_mode import SocketModeHandler | ||
from slack_sdk import WebClient | ||
|
||
app = App(token=os.environ.get("SLACK_BOT_TOKEN")) | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
|
||
@app.function("sample_function") | ||
def handle_sample_function_event(inputs: dict, say: Say, fail: Fail, logger: logging.Logger): | ||
user_id = inputs["user_id"] | ||
|
||
try: | ||
say( | ||
channel=user_id, # sending a DM to this user | ||
text="Click button to complete function!", | ||
blocks=[ | ||
{ | ||
"type": "section", | ||
"text": {"type": "mrkdwn", "text": "Click button to complete function!"}, | ||
"accessory": { | ||
"type": "button", | ||
"text": {"type": "plain_text", "text": "Click me!"}, | ||
"action_id": "sample_click", | ||
}, | ||
} | ||
], | ||
) | ||
except Exception as e: | ||
logger.exception(e) | ||
fail(f"Failed to handle a function request (error: {e})") | ||
|
||
|
||
@app.action("sample_click") | ||
def handle_sample_click( | ||
ack: Ack, body: dict, context: BoltContext, client: WebClient, complete: Complete, fail: Fail, logger: logging.Logger | ||
): | ||
ack() | ||
|
||
try: | ||
# Since the button no longer works, we should remove it | ||
client.chat_update( | ||
channel=context.channel_id, | ||
ts=body["message"]["ts"], | ||
text="Congrats! You clicked the button", | ||
) | ||
|
||
# Signal that the function completed successfully | ||
complete({"user_id": context.actor_user_id}) | ||
except Exception as e: | ||
logger.exception(e) | ||
fail(f"Failed to handle a function request (error: {e})") | ||
|
||
|
||
if __name__ == "__main__": | ||
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN")).start() |
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,48 @@ | ||
{ | ||
"display_information": { | ||
"name": "BoltPy Automation Template" | ||
}, | ||
"outgoing_domains": [], | ||
"settings": { | ||
"org_deploy_enabled": true, | ||
"socket_mode_enabled": true | ||
}, | ||
"features": { | ||
"bot_user": { | ||
"display_name": "BoltPy Automation Template" | ||
}, | ||
"app_home": { | ||
"messages_tab_enabled": true | ||
} | ||
}, | ||
"oauth_config": { | ||
"scopes": { | ||
"bot": ["chat:write"] | ||
} | ||
}, | ||
"functions": { | ||
"sample_function": { | ||
"title": "Sample function", | ||
"description": "Runs sample function", | ||
"input_parameters": { | ||
"user_id": { | ||
"type": "slack#/types/user_id", | ||
"title": "User", | ||
"description": "Send this to who?", | ||
"is_required": true, | ||
"hint": "Select a user in the workspace", | ||
"name": "user_id" | ||
} | ||
}, | ||
"output_parameters": { | ||
"user_id": { | ||
"type": "slack#/types/user_id", | ||
"title": "User", | ||
"description": "User to used the function", | ||
"is_required": true, | ||
"name": "user_id" | ||
} | ||
} | ||
} | ||
} | ||
} |
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,11 @@ | ||
# black project prefers pyproject.toml | ||
# that's why we have this file in addition to other setting files | ||
[tool.black] | ||
line-length = 125 | ||
|
||
[tool.pytest.ini_options] | ||
testpaths = ["tests"] | ||
log_file = "logs/pytest.log" | ||
log_file_level = "DEBUG" | ||
log_format = "%(asctime)s %(levelname)s %(message)s" | ||
log_date_format = "%Y-%m-%d %H:%M:%S" |
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,4 @@ | ||
slack-cli-hooks | ||
pytest | ||
flake8==7.0.0 | ||
black==23.12.1 |
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,5 @@ | ||
{ | ||
"hooks": { | ||
"get-hooks": "python3 -m slack_cli_hooks.hooks.get_hooks" | ||
} | ||
} |
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 @@ | ||
# Copyright 2022, Slack Technologies, LLC. All rights reserved. |