Search code examples
pythonpython-3.xslackslack-apislack-commands

Cannot read channel message from slack in Python using slack SocketModeHandler


Below is the python code for reading and responding to message from slack channel to python. I wrote this script by using their tutorials and ended up here with the problem. Also I am unable to send message to slack using client.chat_postMessage(channel="XXXXXXXXXXX", text=msg, user="XXXXXXXXXXX")

I don't know why but when I write command "/hi" in channel, the python reads the event and prints data but if I try any keyword like check and knock knock, the python doesn't responds to this,

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()

Here is the manifest of my app from 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

Any help to this problem from experts may reduce my headache and workload, Thanks in advance!

Kind regards, Gohar


Solution

  • The bot must be a member of the channel where the message is being sent — please make sure to invite the bot into that channel and it should begin receiving those message events.

    Also, this is somewhat incidental to your question, but as a security precaution, please only request the scopes necessary for your bot to function. You risk creating a token with far too permissive a number of scopes. You likely don't need user scopes for this app. The same holds true for events — consider only subscribing to events your app actually requires.