Slack SocketModeClient - 仅响应频道中提及应用程序的消息

问题描述 投票:0回答:1

我正在尝试使用

SocketModeClient
创建 Slack 机器人,该机器人仅响应已明确提及的频道中的消息。我最初的想法是做如下事情:

    socket_mode_client = SocketModeClient(
        # This app-level token will be used only for establishing a connection
        app_token=os.environ['APP_TOKEN'],  # xapp-A111-222-xyz
        # You will be using this WebClient for performing Web API calls in listeners
        web_client=WebClient(token=os.environ['TOKEN']),  # xoxb-111-222-xyz
    )

    def _process_websocket_message(client: SocketModeClient, request: SocketModeRequest):
        # Acknowledge the request
        response = SocketModeResponse(envelope_id=request.envelope_id)
        client.send_socket_mode_response(response)

        if request.type != 'events_api':
            return

        # ignore duplicated requests
        if request.retry_attempt is not None and request.retry_attempt > 0:
            return

        payload_event = request.payload['event']

        if payload_event['type'] == 'app_mention' and '<app_user_id>' in payload_event['<some-attr>']:
            # respond

这里的想法是检查消息文本中应用程序的用户 ID,并且仅在其中存在时才进行响应。但是,我不完全确定如何获取应用程序的用户 ID。我知道使用

users_profile_get()
WebClient
方法,我可以获取机器人 ID 和应用程序 ID,但当提到应用程序时,这些不是文本中出现的 ID。

我也愿意尝试不同的方法。

python slack slack-api
1个回答
0
投票

我认为正确的方法是仅订阅

app_mention
事件,而不是
message.channels
(通过应用程序)。我注意到我已经订阅了两者并取消了后者的订阅。那行得通。

但是,如果出于某种原因需要订阅这两个事件,并且您只想在提及应用程序时进行响应,则可以使用

auth_test()
提供的
WebClient
方法来提取用户 ID机器人的,然后按照上面问题中的解释在文本中检查它。

© www.soinside.com 2019 - 2024. All rights reserved.