Created
November 14, 2023 07:06
-
-
Save seratch/0311648b675ae1a14fac458d8e045a92 to your computer and use it in GitHub Desktop.
file input block in bolt-python apps
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
display_information: | |
name: file-uploader | |
features: | |
bot_user: | |
display_name: file-uploader | |
slash_commands: | |
- command: /send-files | |
description: Send files on a modal | |
should_escape: false | |
oauth_config: | |
scopes: | |
bot: | |
- commands | |
- files:read | |
settings: | |
interactivity: | |
is_enabled: true | |
socket_mode_enabled: true |
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 | |
logging.basicConfig(level=logging.DEBUG) | |
import os | |
from slack_sdk import WebClient | |
from slack_bolt import App, Ack | |
from slack_bolt.adapter.socket_mode import SocketModeHandler | |
app = App(token=os.environ.get("SLACK_BOT_TOKEN")) | |
@app.command("/send-files") | |
def handle_command(body: dict, ack: Ack, client: WebClient, logger: logging.Logger): | |
ack() | |
res = client.views_open( | |
trigger_id=body["trigger_id"], | |
view={ | |
"type": "modal", | |
"callback_id": "view-id", | |
"title": {"type": "plain_text", "text": "File uploader"}, | |
"submit": {"type": "plain_text", "text": "Submit"}, | |
"close": {"type": "plain_text", "text": "Close"}, | |
"blocks": [ | |
{ | |
"type": "input", | |
"block_id": "text", | |
"element": {"type": "plain_text_input", "action_id": "input"}, | |
"label": { | |
"type": "plain_text", | |
"text": "Text", | |
}, | |
}, | |
{ | |
"type": "input", | |
"block_id": "files", | |
"element": { | |
"type": "file_input", | |
"action_id": "input", | |
"filetypes": ["jpg", "png"], | |
"max_files": 2, | |
}, | |
"label": { | |
"type": "plain_text", | |
"text": "Files", | |
}, | |
}, | |
], | |
}, | |
) | |
logger.info(res) | |
@app.view("view-id") | |
def view_submission(ack, body, logger): | |
ack() | |
logger.info(body["view"]["state"]["values"]) | |
# { | |
# "text": {"input": {"type": "plain_text_input", "value": "test"}}, | |
# "files": { | |
# "input": { | |
# "type": "file_input", | |
# "files": [ | |
# { | |
# "id": "F0669KB228Y", | |
# "created": 1699945350, | |
# "timestamp": 1699945350, | |
# "name": "xxx.png", | |
# "title": "xxx.png", | |
# "mimetype": "image/png", | |
# "filetype": "png", | |
# "pretty_type": "PNG", | |
# "user": "U03E94MK0", | |
# "user_team": "T03E94MJU", | |
# "editable": False, | |
# "size": 195423, | |
# "mode": "hosted", | |
# "is_external": False, | |
# "external_type": "", | |
# "is_public": False, | |
# "public_url_shared": False, | |
# "display_as_bot": False, | |
# "username": "", | |
# "url_private": "https://files.slack.com/xxx.png", | |
# "url_private_download": "https://files.slack.com/xxx.png", | |
# "media_display_type": "unknown", | |
# "thumb_64": "https://files.slack.com/xxx.png", | |
# "thumb_80": "https://files.slack.com/xxx.png", | |
# "thumb_360": "https://files.slack.com/xxx.png", | |
# "thumb_360_w": 278, | |
# "thumb_360_h": 360, | |
# "thumb_480": "https://files.slack.com/xxx.png", | |
# "thumb_480_w": 371, | |
# "thumb_480_h": 480, | |
# "thumb_160": "https://files.slack.com/xxx.png", | |
# "thumb_720": "https://files.slack.com/xxx.png", | |
# "thumb_720_w": 556, | |
# "thumb_720_h": 720, | |
# "thumb_800": "https://files.slack.com/xxx.png", | |
# "thumb_800_w": 800, | |
# "thumb_800_h": 1035, | |
# "thumb_960": "https://files.slack.com/xxx.png", | |
# "thumb_960_w": 742, | |
# "thumb_960_h": 960, | |
# "thumb_1024": "https://files.slack.com/xxx.png", | |
# "thumb_1024_w": 791, | |
# "thumb_1024_h": 1024, | |
# "original_w": 1088, | |
# "original_h": 1408, | |
# "thumb_tiny": "xxx", | |
# "permalink": "https://files.slack.com/xxx.png", | |
# "permalink_public": "https://slack-files.com/xxx", | |
# "comments_count": 0, | |
# "shares": {}, | |
# "channels": [], | |
# "groups": [], | |
# "ims": [], | |
# "has_more_shares": False, | |
# "has_rich_preview": False, | |
# "file_access": "visible", | |
# } | |
# ], | |
# } | |
# }, | |
# } | |
# Start your app | |
if __name__ == "__main__": | |
SocketModeHandler(app, os.environ["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
slack-bolt>=1.18,<2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment