Python-Bolt框架

slack-bolt的Python项目详细描述


Python的BoltBolt logo

Python Versionpypi packageBuild StatusCodecov

一个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 eventsactionsshortcutsslash 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,一个包含该事件所有可用参数的^{}实例。在

ArgumentDescription
^{}Dictionary that contains the entire body of the request (superset of ^{}). Some accessory data is only available outside of the payload (such as ^{} and ^{}).
^{}Contents of the incoming event. The payload structure depends on the listener. For example, for an Events API event, ^{} will be the event type structure. For a block action, it will be the action from within the ^{} list. The ^{} dictionary is also accessible via the alias corresponding to the listener (^{}, ^{}, ^{}, ^{}, ^{}, ^{}, or ^{}). For example, if you were building a ^{} listener, you could use the ^{} and ^{} arguments interchangably. An easy way to understand what's in a payload is to log it.
^{}Event context. This dictionary contains data about the event and app, such as the ^{}. Middleware can add additional context before the event is passed to listeners.
^{}Function that must be called to acknowledge that your app received the incoming event. ^{} exists for all actions, shortcuts, view submissions, slash command and options requests. ^{} returns a promise that resolves when complete. Read more in Acknowledging events.
^{}Utility function that responds to incoming events if it contains a ^{} (shortcuts, actions, and slash commands).
^{}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 ^{} (the most common being ^{} events). ^{} accepts simple strings (for plain-text messages) and dictionaries (for messages containing blocks).
^{}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 ^{} function.
^{}The built-in ^{} instance you can use in middleware/listeners.

创建异步应用程序

如果您希望使用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

在异步应用程序中,所有中间件/侦听器都必须是异步函数。在这些函数中调用实用程序方法(如acksay)时,需要使用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 documentation提供了有关Bolt for Python的基本和高级概念的更多信息。在

如果你遇到麻烦,我们会来帮忙的。以下是帮助您解决问题的最佳方法:

  • Issue Tracker获取与Bolt for Python相关的问题、bug报告、特性请求和一般性讨论。尝试在创建新问题之前搜索现有问题。在
  • Email我们的开发人员支持团队:support@slack.com

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java HttpSession API   如果模式可用,xml java将忽略DTD进行验证   java从远程位置调用Jersey API   在Java中直接从URL读取html   如何使用#Hibernate#Spring#Java在MySql中存储“日期”?   java如何在JSP中迭代列表对象?   java如何使用PDFBox确定实际PDF内容的位置?   utf 8计算Java字符串的UTF8长度,而不实际对其进行编码   java我正在尝试通过whatsapp或任何其他messenger应用程序发送/共享mp3原始文件。   使用Java定期将记录写入CSV文件   用于队列和工作程序设计的java实时执行器池   java与t1的区别是什么。当前线程和线程。当前线程?   java将大字符串写入文本文件?   java传递给侦听器   java如何从对象列表向JComboBox添加项