Skip to main content

The Bolt Framework for Python

Project description

Bolt for Python (still in beta)

Python Version pypi package Build Status Codecov

A Python framework to build Slack apps in a flash with the latest platform features. Check the document and samples to know how to use this framework.

Setup

# Python 3.6+ required
python -m venv .venv
source .venv/bin/activate

pip install -U pip
pip install slack_bolt

First Bolt App (app.py)

Create an app by calling a constructor, which is a top-level export.

import logging
logging.basicConfig(level=logging.DEBUG)

from slack_bolt import App

# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
app = App()

# Events API: https://api.slack.com/events-api
@app.event("app_mention")
def event_test(say):
    say("What's up?")

# Interactivity: https://api.slack.com/interactivity
@app.shortcut("callback-id-here")
# @app.command("/hello-bolt-python")
def open_modal(ack, client, logger, body):
    # acknowledge the incoming request from Slack immediately
    ack()
    # open a modal
    api_response = 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": "b",
                    "element": {
                        "type": "plain_text_input",
                        "action_id": "a"
                    },
                    "label": {
                        "type": "plain_text",
                        "text": "Label",
                    }
                }
            ]
        })
    logger.debug(api_response)

@app.view("view-id")
def view_submission(ack, view, logger):
    ack()
    # Prints {'b': {'a': {'type': 'plain_text_input', 'value': 'Your Input'}}}
    logger.info(view["state"]["values"])

if __name__ == "__main__":
    app.start(3000)  # POST http://localhost:3000/slack/events

Run the Bolt App

export SLACK_SIGNING_SECRET=***
export SLACK_BOT_TOKEN=xoxb-***
python app.py

# in another terminal
ngrok http 3000

AsyncApp Setup

If you prefer building Slack apps using asyncio, you can go with AsyncApp instead. You can use async/await style for everything in the app. To use AsyncApp, AIOHTTP library is required for asynchronous Slack Web API calls and the default web server.

# Python 3.6+ required
python -m venv .venv
source .venv/bin/activate

pip install -U pip
# aiohttp is required
pip install slack_bolt aiohttp

Import slack_bolt.async_app.AsyncApp instead of slack_bolt.App. All middleware/listeners must be async functions. Inside the functions, all utility methods such as ack, say, and respond requires await keyword.

from slack_bolt.async_app import AsyncApp

app = AsyncApp()

@app.event("app_mention")
async def event_test(body, say, logger):
    logger.info(body)
    await say("What's up?")

@app.command("/hello-bolt-python")
async def command(ack, body, respond):
    await ack()
    await respond(f"Hi <@{body['user_id']}>!")

if __name__ == "__main__":
    app.start(3000)

Starting the app is exactly the same with the way using slack_bolt.App.

export SLACK_SIGNING_SECRET=***
export SLACK_BOT_TOKEN=xoxb-***
python app.py

# in another terminal
ngrok http 3000

If you want to use another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at the built-in adapters and their samples.

Feedback

We are keen to hear your feedback. Please feel free to submit an issue!

License

The MIT License

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slack_bolt-0.6.1a0.tar.gz (56.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slack_bolt-0.6.1a0-py2.py3-none-any.whl (106.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file slack_bolt-0.6.1a0.tar.gz.

File metadata

  • Download URL: slack_bolt-0.6.1a0.tar.gz
  • Upload date:
  • Size: 56.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for slack_bolt-0.6.1a0.tar.gz
Algorithm Hash digest
SHA256 c466dd6a24060b4e99f44f527a7134f4ef1be92cec792abdfd599caa706dd9e9
MD5 ce701aa1b26dc7d0a7874e3de2e0fc07
BLAKE2b-256 2cc377a9c3d1757317e45de90c7537ca48e0dac16e2ad52a9dd1ca8a8ee9d0eb

See more details on using hashes here.

File details

Details for the file slack_bolt-0.6.1a0-py2.py3-none-any.whl.

File metadata

  • Download URL: slack_bolt-0.6.1a0-py2.py3-none-any.whl
  • Upload date:
  • Size: 106.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for slack_bolt-0.6.1a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7b2e509aedcda7ad14c3ff5e800aa23d2b667e5ef92ac49eee55260bf5964837
MD5 79dc496a26f894b63f5596ea2af22fe6
BLAKE2b-256 82d9ce3617e07273e345fd54849c56c2befb0a284838b4bbc10935b7361a5a64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page