当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

bolt-python

A framework to build Slack apps using Python
授权协议 MIT License
开发语言 Python
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 赵智
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Bolt for Python

Build Status

A Python framework to build Slack apps in a flash with the latest platform features. Read the getting started guide and look at our code examples to learn how to build apps using Bolt. The Python module documents are available here.

Setup

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

pip install -U pip
pip install slack_bolt

Creating an app

Create a Bolt for Python app by calling a constructor, which is a top-level export. If you'd prefer, you can create an async app.

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

from slack_bolt import App

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

# Add functionality here

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

Running an app

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

# in another terminal
ngrok http 3000

Running a Socket Mode app

If you use Socket Mode for running your app, SocketModeHandler is available for it.

import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])

# Add functionality here

if __name__ == "__main__":
    # Create an app-level token with connections:write scope
    handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
    handler.start()

Run the app this way:

export SLACK_APP_TOKEN=xapp-***
export SLACK_BOT_TOKEN=xoxb-***
python app.py

# SLACK_SIGNING_SECRET is not required
# Running ngrok is not required

Listening for events

Apps typically react to a collection of incoming events, which can correspond to Events API events, actions, shortcuts, slash commands or options requests. For each type ofrequest, there's a method to build a listener function.

# Listen for an event from the Events API
app.event(event_type)(fn)

# Convenience method to listen to only `message` events using a string or re.Pattern
app.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 submissions
app.action({"callback_id": callbackId})(fn)

# Listen for a global or message shortcuts
app.shortcut(callback_id)(fn)

# Listen for slash commands
app.command(command_name)(fn)

# Listen for view_submission modal events
app.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)
def handle_event(event):
    pass

Making things happen

Most of the app's functionality will be inside listener functions (the fn parameters 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 use args, an slack_bolt.kwargs_injection.Args instance that contains all available arguments for that event.

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

Creating an async app

If you'd prefer to build your app with asyncio, you can import the AIOHTTP library and call the AsyncApp constructor. Within async apps, you can use the async/await pattern.

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

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

In async apps, all middleware/listeners must be async functions. When calling utility methods (like ack and say) within these functions, it's required to use the await keyword.

# Import the async app instead of the regular one
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)

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.

Getting Help

The documentation has more information on basic and advanced concepts for Bolt for Python. Also, all the Python module documents of this library are available here.

If you otherwise get stuck, we're here to help. The following are the best ways to get assistance working through your issue:

  • Issue Tracker for questions, bug reports, feature requests, and general discussion related to Bolt for Python. Try searching for an existing issue before creating a new one.
  • Email our developer support team: support@slack.com
  • 内容摘自:https://github.com/jobbole/awesome-machine-learning-cn 计算机视觉 SimpleCV—开源的计算机视觉框架,可以访问如OpenCV等高性能计算机视觉库。使用Python编写,可以在Mac、Windows以及Ubuntu上运行。 自然语言处理 NLTK —一个领先的平台,用来编写处理人类语言数据的Python程序 Pattern—Pyt

  • 要连接到 Neo4j 图形数据库,你可以使用 Py2neo 库。它是一个 Python 库,用于连接和操作 Neo4j 图形数据库。 首先,你需要安装 Py2neo 库。你可以使用 pip 命令来安装它: pipinstall py2neo 然后,你就可以使用 Py2neo 库来连接到 Neo4j 图形数据库了。 例如,你可以使用下面的代码来连接到 Neo4j 图形数据库: from py2ne

  • 每次执行Cypher时,BoltStatementResult都会返回a。这提供了查询结果的句柄,从而可以访问其中的记录以及结果元数据。 每个结果均包含页眉元数据,零个或多个Record对象以及页脚元数据(摘要)。结果还包含一个缓冲区,当结果被无序使用时,该缓冲区会自动存储未使用的记录。通过将A BoltStatementResult附加到活动连接,Session直到其所有内容都已缓冲或消耗完毕。

  • 采用python-pptx创建图形时,不知道MSO_SHAPE有多少种图形类,都在这里了,总共182种,总有一款适合你: ACTION_BUTTON_BACK_OR_PREVIOUS Back or Previous button. Supports mouse-click and mouse-over actions ACTION_BUTTON_BEGINNING Beginning butto

  • Storm可支持多种语言,其中就有python . 首先需要创建一个类, public static class BasieCalculateBolt extends ShellBolt implementsIRichBolt {publicBasieCalculateBolt() {super("python", "bolt_base_calculate.py"); } @Overridepub

  • from :www.neo4j.com from neo4j import GraphDatabase import logging from neo4j.exceptions import ServiceUnavailable class App: def __init__(self, uri, user, password): self.driver = Graph

  • python连接neo4j数据库:   https://neo4j.com/docs/api/python-driver/current/#example-application   系统版本:window10(推荐linux系统来安装来减少一些错误的概率) neo4j数据库版本:neo4j  version-4.0.1             python版本:3.7.6     其他版本匹配:

 相关资料
  • Bolt是一个实时裁剪压缩图片服务器,其比nginx的image_filter快2倍以上,主要是因为Bolt对一张图片只做一次处理,就算在处理图片的过程中,其他的客户端也在请求此图片,Bolt也能保证只有一个线程在处理此图片。 另外Bolt替换缓存机制,处理过的图片不再进行第二次处理,除非内存不足的时候,Bolt才会处理LRU算法来删除缓存中的图片,在启动Bolt的时候可以使用“--max-cac

  • Bolt 是一个内容管理工具,特点是简单而且尽可能直接。安装配置快速,使用优雅的模板和 HTML5 技术。 License: Open source Server Language: PHP 5.3.2+ Database: Yes. SQLite, MySQL or PostgreSQL. Self-Hosted: Yes Support Plugins/Extensions: Yes

  • Facebook 宣布开源其二进制优化和布局工具 BOLT,以帮助工程师来加速他们的大规模应用。 据 Facebook 介绍,BOLT 可以用来优化内存中的指令配置,并且旨在将 CPU 执行时间减少 2% 到 15%,尽管在大多数情况下它将其减少了 8%。 Facebook 表示,“高度复杂的服务,例如 Facebook 上的服务,拥有大量的源代码库以提供广泛的特性和功能。即使编译了其中一种服务的

  • React Bolt The best boilerplate for your React projects. Introduction This boilerplate contains all you need to start your next React.js project. Simple, robust, well-organized, all you need to do is

  • Bolt 是 Objective Caml 编程语言的日志记录框架。

  • Bolt 是一个给本地或分布式多维数组提供 Python 界面的开源库,它旨在优化数据处理性能,不管数据是小型、中型还是非常非常大型的数据,它通过一个普通且熟悉的 ndarray 界面来完成这项任务。 Bolt 核心 100% 是 Python。目前由 NumPy (本地)或火花(分布式)支持,未来会扩大到其它的。 安装: $ pip install bolt-python