I'm trying to read messages posted in a private Slack channel using Slack Bolt Python SDK. The problem is that the method is not getting invoked at all when I post some messages to this channel.
from typing import Optional
import slack_sdk
import os
import logging
from pathlib import Path
from dotenv import load_dotenv
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
logging.basicConfig(level=logging.DEBUG)
env_path = Path('..') / '.env'
load_dotenv(dotenv_path=env_path)
SLACK_VERIFICATION_TOKEN = os.environ['SLACK_VERIFICATION_TOKEN']
SLACK_SIGNING_SECRET = os.environ['SLACK_SIGNING_SECRET']
SLACK_BOT_TOKEN = os.environ['SLACK_BOT_TOKEN']
SLACK_APP_TOKEN = os.environ['SLACK_APP_TOKEN']
app = App(token=SLACK_BOT_TOKEN, signing_secret=SLACK_SIGNING_SECRET)
@app.message("<Slack Channel Id>") # Replace "your-channel-id" with your actual channel ID
def handle_message(message, say):
user = message["user"]
text = message["text"]
channel = message["channel"]
# Do something with the received message
print(f"Received message '{text}' from user {user} in channel {channel}")
if __name__ == "__main__":
handler = SocketModeHandler(app, SLACK_APP_TOKEN)
handler.start()
I have enabled subscriptions as well to events as message.channels event
To receive messages from a private channel, you'll need to ensure a few key things are in place:
Permissions: Make sure the user (in this case, your bot or app) has the necessary permissions to access the channel's history. This involves having the channels:history
or groups:history
permissions granted to your bot's API token or user account to read messages in private channel.
Membership in the Channel: Your bot or app needs to be added or invited to the private channel. Since it's a private channel, access is restricted, and only approved members or bots can join. Ensure your bot has been installed or added to the private channel.