Search code examples
slackslack-api

Slack API - Sending a direct message to personal space (you)


I created a Slack app with a bot. I got the OAuth process working. The user completes OAuth process and I get both the regular token and bot token along with user_id.

When I try to send a api/chat.postMessage with bot token and channel = user_id that I received from OAuth, a message is created under "Slackbot".

How do I send a direct message to the user who authorized the OAuth? I want it under the (you) private/personal DM space.

Edit:

im.open creates a new channel between the bot and the authorized user. It does NOT post the message directly in the user's (you) private space.


Solution

  • Here is how you can send a message into your own personal user channel - e.g. for me it channel would be called "Erik Kalkoken (you)":

    Send a message with chat.postMessage

    • Use an access token (not a bot token)
    • Set channel with the user ID. Must be the same user who authed the access token
    • Set as_user = true

    Apparently this will only work if your app has an token authed by the specific user it wants to send a message to. So unless you gather tokens from all users in a workspace this will only work for the use who installed / authed your app.