首页 > 解决方案 > 如何在 slack api 的事件中触发 Modal?

问题描述

我想创建一个模式,当新用户加入频道时会立即弹出。我的想法是在新成员加入时使用@app.event 触发,然后以某种方式激活我的斜杠命令。但不幸的是 app.event 没有 trigger_id 所以我不能只在事件方法中创建模式。我也不喜欢使用斜杠命令,但我发现这一切都可以让用户使用复选框并提交已检查的模式响应。将两者联系起来的任何帮助都会很棒,或者其他建议将不胜感激。

@app.event("member_joined_channel")
def modal_event(event, say, body):
    usr_id = event["user"],
    user_id = usr_id[0]
    channel_id = event["channel"]
    pprint.pprint(body)
    say(text=f"Welcome to the channel, <@{user_id}>!  You can introduce yourself in this channel.")
    app.client.chat_postMessage(
        channel=channel_id,
    )


@app.command("/cmd")
def modal(body):
    pprint.pprint(body)
    result = app.client.views_open(
        trigger_id=body['trigger_id'],
        view={
            "title": {
                "type": "plain_text",
                "text": "My App",
                "emoji": True
            },
            "submit": {
                "type": "plain_text",
                "text": "Submit",
                "emoji": True
            },
            "type": "modal",
            "close": {
                "type": "plain_text",
                "text": "Cancel",
                "emoji": True
            },
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n"
                    }
                },
                {
                    "type": "input",
                    "element": {
                        "type": "checkboxes",
                        "options": [
                            {
                                "text": {
                                    "type": "plain_text",
                                    "text": "Gary Danko",
                                    "emoji": True
                                },
                                "value": "value-0"
                            },
                            {
                                "text": {
                                    "type": "plain_text",
                                    "text": "Chipotle",
                                    "emoji": True
                                },
                                "value": "value-1"
                            },
                            {
                                "text": {
                                    "type": "plain_text",
                                    "text": "Slack Cafe",
                                    "emoji": True
                                },
                                "value": "value-2"
                            }
                        ]
                    },
                    "label": {
                        "type": "plain_text",
                        "text": "Please select all restaurants you'd be willing to eat at:",
                        "emoji": True
                    }
                }
            ]
        }
    )

标签: pythonslack-api

解决方案


不幸的是,您需要trigger_id打开一个模态。我不知道没有它可以打开一个。您可以做的是监听[member_joined_channel][1]事件,然后让您的应用程序在频道中向用户发送一条临时消息,提示他们单击一个按钮,然后该按钮将打开一个模式。


推荐阅读