python3 -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install -r requirements.txt
export SLACK_BOT_TOKEN=(your own value)
export SLACK_SIGNING_SECRET=(your own value)
python app.py
Last active
November 16, 2020 07:09
-
-
Save seratch/c5662b659fb1fb748ed29d7d463233d7 to your computer and use it in GitHub Desktop.
Slack usergroup select menu
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
import logging | |
from typing import List | |
from slack_bolt import App, Ack | |
from slack_sdk import WebClient | |
from slack_sdk.errors import SlackApiError | |
logging.basicConfig(level=logging.DEBUG) | |
app = App() | |
@app.command("/hello-bolt-python") | |
def test_command(body: dict, client: WebClient, ack: Ack, logger: logging.Logger): | |
ack() | |
try: | |
# usergroups:read scope is required | |
user_groups: List[dict] = client.usergroups_list()["usergroups"] | |
options = [ | |
{"value": u["id"], "text": {"type": "plain_text", "text": u["name"]}} | |
for u in user_groups | |
] | |
client.views_open( | |
trigger_id=body["trigger_id"], | |
view={ | |
"type": "modal", | |
"callback_id": "view-id", | |
"title": {"type": "plain_text", "text": "My App"}, | |
"submit": {"type": "plain_text", "text": "Submit"}, | |
"blocks": [ | |
{ | |
"type": "input", | |
"block_id": "b1", | |
"element": { | |
"type": "static_select", | |
"placeholder": { | |
"type": "plain_text", | |
"text": "Select an item", | |
}, | |
"options": options, | |
"action_id": "static-select", | |
}, | |
"label": { | |
"type": "plain_text", | |
"text": "static select", | |
}, | |
}, | |
{ | |
"type": "input", | |
"block_id": "b2", | |
"element": { | |
"type": "external_select", | |
"placeholder": { | |
"type": "plain_text", | |
"text": "Select an item", | |
}, | |
"min_query_length": 0, | |
"action_id": "external-select", | |
}, | |
"label": { | |
"type": "plain_text", | |
"text": "external select", | |
}, | |
}, | |
], | |
}, | |
) | |
except SlackApiError as e: | |
logger.error(f"Failed to open a modal (error: {e})") | |
@app.options("external-select") | |
def load_options(logger: logging.Logger, ack: Ack, client: WebClient): | |
try: | |
# usergroups:read scope is required | |
user_groups: List[dict] = client.usergroups_list()["usergroups"] | |
options = [ | |
{"value": u["id"], "text": {"type": "plain_text", "text": u["name"]}} | |
for u in user_groups | |
] | |
ack(options=options) | |
except SlackApiError as e: | |
logger.error(f"Failed to load options (error: {e})") | |
ack(options=[]) | |
@app.view("view-id") | |
def view_submission(ack: Ack, body: dict, logger: logging.Logger): | |
logger.info(body) | |
ack() | |
if __name__ == "__main__": | |
app.start(3000) |
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
slack_bolt>=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment