|
| 1 | +# ------------------------------------------------ |
| 2 | +# instead of slack_bolt in requirements.txt |
| 3 | +import asyncio |
| 4 | +import sys |
| 5 | + |
| 6 | +sys.path.insert(1, "..") |
| 7 | +# ------------------------------------------------ |
| 8 | + |
| 9 | +import logging |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.DEBUG) |
| 12 | + |
| 13 | +from slack_bolt.async_app import AsyncApp |
| 14 | + |
| 15 | +app = AsyncApp() |
| 16 | + |
| 17 | + |
| 18 | +@app.middleware # or app.use(log_request) |
| 19 | +async def log_request(logger, payload, next): |
| 20 | + logger.debug(payload) |
| 21 | + return await next() |
| 22 | + |
| 23 | + |
| 24 | +@app.command("/hello-bolt-python") |
| 25 | +async def handle_command(payload, ack, respond, client, logger): |
| 26 | + logger.info(payload) |
| 27 | + await ack("Accepted!") |
| 28 | + |
| 29 | + await respond( |
| 30 | + blocks=[ |
| 31 | + { |
| 32 | + "type": "section", |
| 33 | + "block_id": "b", |
| 34 | + "text": { |
| 35 | + "type": "mrkdwn", |
| 36 | + "text": "You can add a button alongside text in your message. ", |
| 37 | + }, |
| 38 | + "accessory": { |
| 39 | + "type": "button", |
| 40 | + "action_id": "a", |
| 41 | + "text": {"type": "plain_text", "text": "Button"}, |
| 42 | + "value": "click_me_123", |
| 43 | + }, |
| 44 | + } |
| 45 | + ] |
| 46 | + ) |
| 47 | + |
| 48 | + res = await client.views_open( |
| 49 | + trigger_id=payload["trigger_id"], |
| 50 | + view={ |
| 51 | + "type": "modal", |
| 52 | + "callback_id": "view-id", |
| 53 | + "title": {"type": "plain_text", "text": "My App",}, |
| 54 | + "submit": {"type": "plain_text", "text": "Submit",}, |
| 55 | + "close": {"type": "plain_text", "text": "Cancel",}, |
| 56 | + "blocks": [ |
| 57 | + { |
| 58 | + "type": "input", |
| 59 | + "element": {"type": "plain_text_input"}, |
| 60 | + "label": {"type": "plain_text", "text": "Label",}, |
| 61 | + }, |
| 62 | + { |
| 63 | + "type": "input", |
| 64 | + "block_id": "es_b", |
| 65 | + "element": { |
| 66 | + "type": "external_select", |
| 67 | + "action_id": "es_a", |
| 68 | + "placeholder": {"type": "plain_text", "text": "Select an item"}, |
| 69 | + }, |
| 70 | + "label": {"type": "plain_text", "text": "Search"}, |
| 71 | + }, |
| 72 | + { |
| 73 | + "type": "input", |
| 74 | + "block_id": "mes_b", |
| 75 | + "element": { |
| 76 | + "type": "multi_external_select", |
| 77 | + "action_id": "mes_a", |
| 78 | + "placeholder": {"type": "plain_text", "text": "Select an item"}, |
| 79 | + }, |
| 80 | + "label": {"type": "plain_text", "text": "Search (multi)"}, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + ) |
| 85 | + logger.info(res) |
| 86 | + |
| 87 | + |
| 88 | +@app.options("es_a") |
| 89 | +async def show_options(ack): |
| 90 | + await ack( |
| 91 | + {"options": [{"text": {"type": "plain_text", "text": "Maru"}, "value": "maru"}]} |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +@app.options("mes_a") |
| 96 | +async def show_multi_options(ack): |
| 97 | + await ack( |
| 98 | + { |
| 99 | + "option_groups": [ |
| 100 | + { |
| 101 | + "label": {"type": "plain_text", "text": "Group 1"}, |
| 102 | + "options": [ |
| 103 | + { |
| 104 | + "text": {"type": "plain_text", "text": "Option 1"}, |
| 105 | + "value": "1-1", |
| 106 | + }, |
| 107 | + { |
| 108 | + "text": {"type": "plain_text", "text": "Option 2"}, |
| 109 | + "value": "1-2", |
| 110 | + }, |
| 111 | + ], |
| 112 | + }, |
| 113 | + { |
| 114 | + "label": {"type": "plain_text", "text": "Group 2"}, |
| 115 | + "options": [ |
| 116 | + { |
| 117 | + "text": {"type": "plain_text", "text": "Option 1"}, |
| 118 | + "value": "2-1", |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + ] |
| 123 | + } |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +@app.view("view-id") |
| 128 | +async def view_submission(ack, payload, logger): |
| 129 | + await ack() |
| 130 | + logger.info(payload["view"]["state"]["values"]) |
| 131 | + |
| 132 | + |
| 133 | +@app.action("a") |
| 134 | +async def button_click(ack, respond): |
| 135 | + await ack() |
| 136 | + await asyncio.sleep(5) |
| 137 | + await respond( |
| 138 | + {"response_type": "in_channel", "text": "Clicked!",} |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + app.start(3000) |
| 144 | + |
| 145 | +# pip install slack_bolt |
| 146 | +# export SLACK_SIGNING_SECRET=*** |
| 147 | +# export SLACK_BOT_TOKEN=xoxb-*** |
| 148 | +# python modals_app.py |
0 commit comments