无法使用 slack SocketModeHandler 从 Python 中的 slack 读取频道消息

Cannot read channel message from slack in Python using slack SocketModeHandler

下面是 python 代码,用于读取和响应从松弛通道到 python 的消息。我使用他们的教程编写了这个脚本,但最终遇到了这个问题。此外,我无法使用 client.chat_postMessage(channel="XXXXXXXXXXX", text=msg, user="XXXXXXXXXXX")

向 slack 发送消息

我不知道为什么,但是当我在通道中写入命令“/hi”时,python 读取事件并打印数据,但是如果我尝试任何关键字,如 check 和 knock knock,python 没有回应,

import os
# Use the package we installed
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from os.path import join, dirname

import time
import re
from datetime import datetime


dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)

# Initializes your app with your bot token and signing secret
app = App(
    token=os.environ['SLACK_BOT_TOKEN'],
    signing_secret=os.environ['SIGNING_SECRET']
)


# Add functionality here
@app.message("check")
def say_hello(message, client, body, logger):
    print(message)
    print(client)
    print(body)
    msg = "Hi there from Python"
    try:
        client.chat_postMessage(channel="XXXXXXXXXXX", text=msg, user="XXXXXXXXXXX")
    except Exception as e:
        logger.exception(f"Failed to post a message {e}")
        print(e)


@app.message("knock knock")
def ask_who(message, say):
    say("_Who's there?_")

@app.event("message")
def handle_message_events(body, logger):
    logger.info(body)
    print("messaging", body)


@app.command("/hi")
def handle_some_command(ack, body, logger):
    ack()
    logger.info(body)
    print(body)


# Start your app
if __name__ == "__main__":
    SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()

这是我的应用程序来自 slackbolt 的清单

_metadata:
  major_version: 1
  minor_version: 1
display_information:
  name: Hotline App
features:
  app_home:
    home_tab_enabled: true
    messages_tab_enabled: true
    messages_tab_read_only_enabled: false
  bot_user:
    display_name: Hotline Bot
    always_online: false
  slash_commands:
    - command: /hi
      description: greets user
      should_escape: false
oauth_config:
  scopes:
    user:
      - chat:write
      - channels:read
      - im:history
      - channels:history
      - groups:history
    bot:
      - incoming-webhook
      - calls:read
      - calls:write
      - app_mentions:read
      - channels:history
      - channels:join
      - channels:manage
      - channels:read
      - chat:write
      - chat:write.customize
      - chat:write.public
      - commands
      - dnd:read
      - emoji:read
      - files:read
      - files:write
      - groups:history
      - groups:read
      - groups:write
      - im:history
      - im:read
      - im:write
      - links:read
      - links:write
      - mpim:history
      - mpim:read
      - mpim:write
      - pins:read
      - pins:write
      - reactions:read
      - reactions:write
      - reminders:read
      - reminders:write
      - remote_files:read
      - remote_files:share
      - remote_files:write
      - team:read
      - usergroups:write
      - usergroups:read
      - users.profile:read
      - users:read
      - users:read.email
      - users:write
      - workflow.steps:execute
settings:
  event_subscriptions:
    user_events:
      - channel_archive
      - channel_created
      - channel_deleted
      - channel_rename
      - message.channels
      - message.groups
      - message.im
    bot_events:
      - app_mention
      - channel_archive
      - channel_created
      - channel_deleted
      - channel_history_changed
      - channel_id_changed
      - channel_left
      - channel_rename
      - channel_shared
      - channel_unarchive
      - channel_unshared
      - dnd_updated_user
      - email_domain_changed
      - emoji_changed
      - file_change
      - file_created
      - file_deleted
      - file_public
      - file_shared
      - file_unshared
      - group_archive
      - group_deleted
      - group_history_changed
      - group_left
      - group_rename
      - group_unarchive
      - im_history_changed
      - link_shared
      - member_joined_channel
      - member_left_channel
      - message.channels
      - message.groups
      - message.im
      - message.mpim
      - pin_added
      - pin_removed
      - reaction_added
      - reaction_removed
      - subteam_created
      - subteam_members_changed
      - subteam_updated
      - team_domain_change
      - team_join
      - team_rename
      - user_change
  interactivity:
    is_enabled: true
  org_deploy_enabled: false
  socket_mode_enabled: true

专家对这个问题的任何帮助都可以减轻我的头痛和工作量,在此先感谢!

亲切的问候, 戈哈尔

机器人必须是发送消息的频道的成员——请确保邀请机器人加入该频道,它应该开始接收这些消息事件。

此外,这对您的问题来说有点偶然,但作为安全预防措施,请只请求您的机器人运行所需的范围。您冒着创建范围过于宽松的令牌的风险。您可能不需要此应用的用户范围。这同样适用于事件——只考虑订阅您的应用实际需要的事件。