bolt-python icon indicating copy to clipboard operation
bolt-python copied to clipboard

Slack Bolt - can not receive dm msg

Open kohei-miyata opened this issue 7 months ago • 3 comments

Channels where the created application exists can retrieve messages. However, some users can retrieve messages in DMs, while others cannot.

The application does not invite in DM.

I don't see any problem

Image

with authority, etc. Because some users can get them. Do we need anything else?

import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from dotenv import load_dotenv
import json
import emoji
import datetime

load_dotenv() 
app = App(token=os.environ.get("SLACK_TOKEN"))
domain = 'XXXX'
send_channel = 'DDDDD'
@app.event("message")
def handle_direct_mention(event, client):
    channel_type = event.get("channel_type")
    print(json.dumps(event, indent=2, ensure_ascii=False, sort_keys=True))
    text = event.get("text", "")
    user = event.get("user", "")
    channel = event.get("channel")
    ts = event.get("ts")
    channel_type = event.get("channel_type")
    thread_ts = event.get("thread_ts") if event.get("thread_ts") else None
    url = get_message_url(domain, channel, ts)
    if thread_ts:
        url += f"?thread_ts={thread_ts}"
    category = talk = "スレッド" if thread_ts else "チャンネル"
    url = f"<{url}|メッセージを確認する>"
    #print(url)
    #print(channiel)
    mention_user_ids = ['AAAA', 'BBBBB', 'CCCC']

    if any(f'@{user_id}' in text for user_id in mention_user_ids) and channel != send_channel:
        if channel_type == 'group':
            type = 'チャンネル'
        elif channel_type == 'im':
            type = 'DM'
        else:
            type = '不明'
        dt_now = datetime.datetime.now()
        user_info = client.users_info(user=user)
        username = user_info['user']['profile']['display_name'] or user_info['user']['real_name']

        print("\n--- debug in U030YMK1X61 ---")
        print(f"現在日時{dt_now}")
        print(json.dumps(event, indent=2, ensure_ascii=False, sort_keys=True))
        converted_text = emoji.emojize(text, language='alias') 
        client.chat_postMessage(
            channel=send_channel,
            text=f"メンションされました!\n\n===========\ntype:{type}\nトーク:{category}\nユーザー:{username}\n===========\n\n下記内容を確認の上ご対応お願いします:sparkles: \n\n```\n{converted_text}```\n\n{url}\n\n"
        )

def get_message_url(domain, channel_id, ts):
    ts_formatted = ts.replace(".", "")
    return f"https://{domain}.slack.com/archives/{channel_id}/p{ts_formatted}"


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


kohei-miyata avatar Jun 03 '25 09:06 kohei-miyata

Hi there!

If I understand correctly, this relates to the app receiving message events when users post in certain channels; message events are received as expected for regular channels where the app bot has been added as a member, but the app does not receive message events for messages posted in direct message (private) channels? This would be the expected behaviour as an app cannot listen to messages in a channel where the bot user doesn't have membership

vegeris avatar Jun 04 '25 02:06 vegeris

Yes, your understanding is correct.

However, we have observed that some users are able to receive message events from direct messages, even though the app was not explicitly added to the DM and has never interacted in the conversation.

Do you know why this might be happening?

If possible, we would like to be able to receive messages from all direct messages. Is there a way to achieve this?

kohei-miyata avatar Jun 04 '25 02:06 kohei-miyata

some users are able to receive message events from direct messages, even though the app was not explicitly added to the DM and has never interacted in the conversation.

Oh! You're right! With the message.im User Event subscription, you should be able to receive message events from DMs where at least one of the users have 'authorized' the app. It's the message.im Bot Event subscription where the bot user must be part of the conversation in order to receive events.

Assuming your app is subscribed to the User Event and the app was reinstalled after adding the subscription, it sounds like the issue would be that in some DMs one or both participants may not have installed the app

vegeris avatar Jun 05 '25 03:06 vegeris

👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.

github-actions[bot] avatar Jul 07 '25 00:07 github-actions[bot]

As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.

github-actions[bot] avatar Jul 21 '25 00:07 github-actions[bot]