Package: slack-bolt
Boltfor PythonA Python framework to build Slack apps in a flash with the latest platform features. Read thegetting started guideand look at ourcode examplesto learn how to build apps using Bolt. The Python module documents are availablehere.Setup# Python 3.6+ requiredpython-mvenv.venvsource.venv/bin/activate

pipinstall-Upip
pipinstallslack_boltCreating an appCreate a Bolt for Python app by calling a constructor, which is a top-level export. If you'd prefer, you can create anasync app.importlogginglogging.basicConfig(level=logging.DEBUG)fromslack_boltimportApp# export SLACK_SIGNING_SECRET=***# export SLACK_BOT_TOKEN=xoxb-***app=App()# Add functionality hereif__name__=="__main__":app.start(3000)# POST http://localhost:3000/slack/eventsRunning an appexportSLACK_SIGNING_SECRET=***exportSLACK_BOT_TOKEN=xoxb-***
pythonapp.py# in another terminalngrokhttp3000Running a Socket Mode appIf you useSocket Modefor running your app,SocketModeHandleris available for it.importosfromslack_boltimportAppfromslack_bolt.adapter.socket_modeimportSocketModeHandler# Install the Slack app and get xoxb- token in advanceapp=App(token=os.environ["SLACK_BOT_TOKEN"])# Add functionality hereif__name__=="__main__":# Create an app-level token with connections:write scopehandler=SocketModeHandler(app,os.environ["SLACK_APP_TOKEN"])handler.start()Run the app this way:exportSLACK_APP_TOKEN=xapp-***exportSLACK_BOT_TOKEN=xoxb-***
pythonapp.py# SLACK_SIGNING_SECRET is not required# Running ngrok is not requiredListening for eventsApps typically react to a collection of incoming events, which can correspond toEvents API events,actions,shortcuts,slash commandsoroptions requests. For each type of
request, there's a method to build a listener function.# Listen for an event from the Events APIapp.event(event_type)(fn)# Convenience method to listen to only `message` events using a string or re.Patternapp.message([pattern,])(fn)# Listen for an action from a Block Kit element (buttons, select menus, date pickers, etc)app.action(action_id)(fn)# Listen for dialog submissionsapp.action({"callback_id":callbackId})(fn)# Listen for a global or message shortcutsapp.shortcut(callback_id)(fn)# Listen for slash commandsapp.command(command_name)(fn)# Listen for view_submission modal eventsapp.view(callback_id)(fn)# Listen for options requests (from select menus with an external data source)app.options(action_id)(fn)The recommended way to use these methods are decorators:@app.event(event_type)defhandle_event(event):passMaking things happenMost of the app's functionality will be inside listener functions (thefnparameters above). These functions are called with a set of arguments, each of which can be used in any order. If you'd like to access arguments off of a single object, you can useargs, anslack_bolt.kwargs_injection.Argsinstance that contains all available arguments for that event.ArgumentDescriptionbodyDictionary that contains the entire body of the request (superset ofpayload). Some accessory data is only available outside of the payload (such astrigger_idandauthorizations).payloadContents of the incoming event. The payload structure depends on the listener. For example, for an Events API event,payloadwill be theevent type structure. For a block action, it will be the action from within theactionslist. Thepayloaddictionary is also accessible via the alias corresponding to the listener (message,event,action,shortcut,view,command, oroptions). For example, if you were building amessage()listener, you could use thepayloadandmessagearguments interchangably.An easy way to understand what's in a payload is to log it.contextEvent context. This dictionary contains data about the event and app, such as thebotId. Middleware can add additional context before the event is passed to listeners.ackFunction thatmustbe called to acknowledge that your app received the incoming event.ackexists for all actions, shortcuts, view submissions, slash command and options requests.ackreturns a promise that resolves when complete. Read more inAcknowledging events.respondUtility function that responds to incoming eventsifit contains aresponse_url(shortcuts, actions, and slash commands).sayUtility function to send a message to the channel associated with the incoming event. This argument is only available when the listener is triggered for events that contain achannel_id(the most common beingmessageevents).sayaccepts simple strings (for plain-text messages) and dictionaries (for messages containing blocks).clientWeb API client that uses the token associated with the event. For single-workspace installations, the token is provided to the constructor. For multi-workspace installations, the token is returned by usingthe OAuth library, or manually using theauthorizefunction.loggerThe built-inlogging.Loggerinstance you can use in middleware/listeners.Creating an async appIf you'd prefer to build your app withasyncio, you can import theAIOHTTPlibrary and call theAsyncAppconstructor. Within async apps, you can use the async/await pattern.# Python 3.6+ requiredpython-mvenv.venvsource.venv/bin/activate

pipinstall-Upip# aiohttp is requiredpipinstallslack_boltaiohttpIn async apps, all middleware/listeners must be async functions. When calling utility methods (likeackandsay) within these functions, it's required to use theawaitkeyword.# Import the async app instead of the regular onefromslack_bolt.async_appimportAsyncAppapp=AsyncApp()@app.event("app_mention")asyncdefevent_test(body,say,logger):logger.info(body)awaitsay("What's up?")@app.command("/hello-bolt-python")asyncdefcommand(ack,body,respond):awaitack()awaitrespond(f"Hi <@{body['user_id']}>!")if__name__=="__main__":app.start(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 examples.The Bolt app examplesThe built-in adaptersApps can be run the same way as the syncronous example above. If you'd prefer another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look atthe built-in adaptersand their correspondingexamples.Getting HelpThe documentationhas more information on basic and advanced concepts for Bolt for Python. Also, all the Python module documents of this library are availablehere.If you otherwise get stuck, we're here to help. The following are the best ways to get assistance working through your issue:Issue Trackerfor questions, bug reports, feature requests, and general discussion related to Bolt for Python. Try searching for an existing issue before creating a new one.Emailour developer support team:support@slack.com