Bhavesh Mevada
Bhavesh Mevada

Reputation: 272

How to send private messages in slack channel?

I am currently sending a message to the slack channel using below function. But I want to send a private message which should be visible to selected member of the slack channel.

How can I do that ?

async function sendSlackMessage() {
    const url = 'https://slack.com/api/chat.postMessage';
    const inputBody = {
        channel: "Slack_Channel_ID",
        text: `Hey Welcome to the slack`,
    };

    const slackHeaders = {
        'Content-Type': 'application/json;charset=utf-8',
        'Authorization': 'Slack_Token',
    };

    const slackRes = await axios.post(url, inputBody, { headers: slackHeaders });
    console.log(slackRes)
}

sendSlackMessage()

Upvotes: 2

Views: 1433

Answers (1)

naren_srini
naren_srini

Reputation: 129

Solution using Boltjs for Javascript:

To send a private message visible only to a specific user on a channel on Slack, we may use a different method chat.postEphemeral from Bolt for JavaScript. Using the above method you can send an ephemeral message to a users in a channel that is visible only to a specific user that you can choose to display.

Note: I have offered my solution as simple blocks, you need to encapsulate it within the function you need this feature to operate on.

Requirements: For using the chat.postEphemeral you are required to send the following arguments to work.

  • token: Slack_Bot_Token {Authentication token bearing required scopes. Tokens should be passed as an HTTP Authorization header or alternatively, as a POST parameter.}
  • channel: {Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name}
  • user: {id of the user who will receive the ephemeral message. The user should be in the channel specified by the channel argument.}
  • text: "Anything you need here"
  • blocks: "Pack and Send from Block Kit Message Builder, not necessary though"

Note:

  • extract the channel id from the function or pass it as args to the async function
  • extract the user id from the function or pass it as args to the async function
  • text field is not enforced when blocks are used.

Methods Access: app.client. chat.postEphemeral

Required Scopes in Slack App:

  • Bot Tokens
  • User Tokens

Example Code:

// Building the args object from body (Can also use, action, context, and few other slack parameters from Bolt API for js)
  const args = {
    user: body.user.id,
    channel: body.container.channel_id,
    team: body.user.team_id,
    token: body.user.id,
    trigger: body.trigger_id,
    url: body.response_url,
  };

Slack App Code:

 try {
  // Call the chat.postEphemeral method using the WebClient
  const result = await client.chat.postEphemeral({
    channel: channelId,
    user: userId,
    token: userToken,
    text: "Shhhh only you can see this :shushing_face:"
  });

  console.log(result);
}
catch (error) {
  console.error(error);
}

Documentation:

View this documentation for more Information: Slack API for Methods

Check here to Create Message Block Kits for Slack: Slack Block Kit Builder

Upvotes: 2

Related Questions