Python-Bolt框架
slack-bolt的Python项目详细描述
Python的Bolt
一个Python框架,在flash中使用最新的平台特性构建Slack应用程序。阅读getting started guide并查看我们的code examples,了解如何使用Bolt构建应用程序。在
设置
# Python 3.6+ required python -m venv .venv source .venv/bin/activate pip install -U pip pip install slack_bolt
创建应用程序
通过调用构造函数创建一个Bolt for Python应用程序,这是一个顶级导出。如果愿意,可以创建一个async app。在
^{pr2}$运行应用程序
exportSLACK_SIGNING_SECRET=*** exportSLACK_BOT_TOKEN=xoxb-*** python app.py # in another terminal ngrok http 3000
倾听事件
应用程序通常对传入事件的集合做出反应,这些事件可以对应于Events API events、actions、shortcuts、slash commands或{a12}。每种类型 请求,有一个方法来构建侦听器函数。在
# 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)
建议使用这些方法的方法是decorators:
@app.event(event_type)defhandle_event(event):pass
让事情发生
应用程序的大部分功能都在监听器函数中(上面的fn
参数)。这些函数用一组参数调用,每个参数可以按任何顺序使用。如果您想访问单个对象的参数,可以使用args
,一个包含该事件所有可用参数的^{
Argument | Description |
---|---|
^{ | Dictionary that contains the entire body of the request (superset of ^{ |
^{ | Contents of the incoming event. The payload structure depends on the listener. For example, for an Events API event, ^{ |
^{ | Event context. This dictionary contains data about the event and app, such as the ^{ |
^{ | Function that must be called to acknowledge that your app received the incoming event. ^{ |
^{ | Utility function that responds to incoming events if it contains a ^{ |
^{ | Utility 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 a ^{ |
^{ | Web 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 using the OAuth library, or manually using the ^{ |
^{ | The built-in ^{ |
创建异步应用程序
如果您希望使用asyncio构建应用程序,可以导入AIOHTTP库并调用AsyncApp
构造函数。在异步应用程序中,可以使用异步/等待模式。在
# Python 3.6+ required python -m venv .venv source .venv/bin/activate pip install -U pip # aiohttp is required pip install slack_bolt aiohttp
在异步应用程序中,所有中间件/侦听器都必须是异步函数。在这些函数中调用实用程序方法(如ack
和say
)时,需要使用await
关键字。在
# 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)
如果您想使用另一个异步Web框架(例如Sanic、FastAPI、Starlette),请看一下内置适配器及其示例。在
- The Bolt app examples
- The built-in adapters 应用程序的运行方式与上面的同步示例相同。如果您更喜欢另一个异步Web框架(例如Sanic、FastAPI、Starlette),请看一下the built-in adapters及其对应的{a19}。在
寻求帮助
The documentation提供了有关Bolt for Python的基本和高级概念的更多信息。在
如果你遇到麻烦,我们会来帮忙的。以下是帮助您解决问题的最佳方法:
- Issue Tracker获取与Bolt for Python相关的问题、bug报告、特性请求和一般性讨论。尝试在创建新问题之前搜索现有问题。在
- Email我们的开发人员支持团队:
support@slack.com
- 项目
标签: