我用的是蟒蛇松螺栓。https://api.slack.com/start/building/bolt-python#create
我在链接中创建了这个示例,并且能够创建一个主页选项卡页面,但是我想从通道中的消息中创建一个模式,而不是主页视图。我到处寻找一个基本的例子,但是我无法让任何模式使用我从slack自己的文档中学到的东西。这是我唯一能找到的例子(在您阅读起始页面之后,这些例子来自slack自己的文档)。
下面是一个测试示例,它可以使用主页而不是模式显示:
SLACK_BOT_TOKEN="slackbottokenstring"
SLACK_SIGNING_SECRET="slacksigningsecretstring"
import os
# Use the package we installed
from slack_bolt import App
# Initializes your app with your bot token and signing secret
app = App(
token = SLACK_BOT_TOKEN,
signing_secret = SLACK_SIGNING_SECRET
# token=os.environ.get("SLACK_BOT_TOKEN"),
# signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
# Add functionality here
@app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
# views.publish is the method that your app uses to push a view to the Home tab
client.views_publish(
# the user that opened your app's app home
user_id=event["user"],
# the view object that appears in the app home
view={
"type": "home",
"callback_id": "home_view",
# body of the view
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Welcome to your _App's Home_* :tada:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This button won't do much for now but you can set up a listener for it using the `actions()` method and passing its unique `action_id`. See an example in the `examples` folder within your Bolt app."
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Click me!"
}
}
]
}
]
}
)
except Exception as e:
logger.error(f"Error publishing home tab: {e}")
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))
这是可行的:
但是,如果我试图将其更改为一个模式,它就会失败:代码:
@app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
# views.publish is the method that your app uses to push a view to the Home tab
client.views_publish(
# the user that opened your app's app home
user_id=event["user"],
# the view object that appears in the app home
view={
"type": "modal",
"callback_id": "modal-identifier",
"title": {
"type": "plain_text",
"text": "Just a modal"
},
"blocks": [
{
"type": "section",
"block_id": "section-identifier",
"text": {
"type": "mrkdwn",
"text": "*Welcome* to ~my~ Block Kit _modal_!"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Just a button",
},
"action_id": "button-identifier",
}
}
],
}
)
错误:
127.0.0.1 - - [14/Aug/2022 15:21:15] "POST /slack/events HTTP/1.1" 200 -
Error publishing home tab: The request to the Slack API failed. (url: https://www.slack.com/api/views.publish)
The server responded with: {'ok': False, 'error': 'invalid_arguments', 'response_metadata': {'messages': ['[ERROR] failed to match all allowed schemas [json-pointer:/view]', '[ERROR] unsupported type: modal [json-pointer:/view/type]']}}
我遵循json对象中的视图结构设计,如下所示:https://api.slack.com/surfaces/modals/using#composing_views
发布于 2022-08-15 05:35:24
我想出了我该做什么,但这有点烦人,我想先说一句。有几个原因,为什么它是恼人的弄明白。
trigger_id
的奇怪的东西,您可以从启动特定的响应中获得这些响应,这将导致向端点发送对象的松弛。问题是没有这方面的例子。如果您正在阅读slack的教程,它们只会向您展示如何创建一个url,以便生成一个事件侦听和响应消息的bot。你可以使用烧瓶制作你自己的url,但是由于我使用的是螺栓松弛模块,他们把自己的东西包在烧瓶周围,这使得很难为烧瓶制作基本的装饰器。-换句话说,松弛的文档对于modals来说是非常混乱的,而且它们没有任何实际的例子来描述使用bolt ..
在业余时间做了几天的研究之后,我终于得到了一个示例,您可以在其中复制和粘贴这段代码。您只需要您的bot令牌秘密和签名密钥,您可以设置在您的环境或字符串,如果您只是在您的本地计算机测试。
SLACK_BOT_TOKEN="slackbottokenstring"
SLACK_SIGNING_SECRET="slacksigningsecretstring"
import os
# Use the package we installed
from slack_bolt import App
from slack_bolt.adapter.flask import SlackRequestHandler
from flask import Flask, request
flask_app = Flask(__name__)
# Initializes your app with your bot token and signing secret
app = App(
token = SLACK_BOT_TOKEN,
signing_secret = SLACK_SIGNING_SECRET
# token=os.environ.get("SLACK_BOT_TOKEN"),
# signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
handler = SlackRequestHandler(app)
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
return handler.handle(request)
@app.shortcut("SearchMessagesID")
def handle_shortcuts(ack, body, logger,client):
ack()
logger.info(body)
print(body)
res = client.views_open(
trigger_id=body["trigger_id"],
view={
"type": "modal",
"callback_id": "modal-identifier",
"title": {
"type": "plain_text",
"text": "Just a modal"
},
"blocks": [
{
"type": "section",
"block_id": "section-identifier",
"text": {
"type": "mrkdwn",
"text": "*Welcome* to ~my~ Block Kit _modal_!"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Just a button",
},
"action_id": "button-identifier",
}
}
],
}
)
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))
https://stackoverflow.com/questions/73355473
复制相似问题