Reputation: 272
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
Reputation: 129
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.
Note:
Methods Access: app.client. chat.postEphemeral
Required Scopes in Slack App:
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