This client allows you to send messages to a Slack channel using a Slack App.
- Create a Slack App:
- Go to https://api.slack.com/apps and click "Create New App".
- Choose "From scratch", give your app a name, and select the workspace where you'll use it.
- Add Permissions:
- In your app's settings, go to "OAuth & Permissions".
- Scroll down to the "Scopes" section. Under "Bot Token Scopes", click "Add an OAuth Scope".
- Add the
chat:writescope. This allows your app to send messages to channels it's a member of.
- Install the App:
- At the top of the "OAuth & Permissions" page, click "Install App to Workspace".
- Follow the prompts to authorize the app.
- Get Your Bot Token:
- After installation, you'll see a "Bot User OAuth Token". It will start with
xoxb-. Copy this token.
- After installation, you'll see a "Bot User OAuth Token". It will start with
- Add the App to a Channel:
- In your Slack workspace, go to the channel where you want to send messages.
- Type
/invite @your-app-name(replaceyour-app-namewith the name of your app) and send the message.
Install the slack client by adding the following dependency:
<dependency>
<groupId>com.n1netails</groupId>
<artifactId>n1netails-slack-client</artifactId>
<version>0.3.0</version>
</dependency>Gradle (Groovy)
implementation 'com.n1netails:n1netails-slack-client:0.3.0'Here's how to use the client to send a message, pics or gifs:
import com.n1netails.n1netails.slack.api.SlackClient;
import com.n1netails.n1netails.slack.model.SlackMessage;
import com.n1netails.n1netails.slack.model.actions_element.ButtonElement;
import com.n1netails.n1netails.slack.model.block.TextBlock;
import com.n1netails.n1netails.slack.model.block.ImageBlock;
import com.n1netails.n1netails.slack.model.block.GifBlock;
import com.n1netails.n1netails.slack.model.block.ActionsBlock;
import com.n1netails.n1netails.slack.model.element.ButtonElement;
import com.n1netails.n1netails.slack.exception.SlackClientException;
public class Example {
public static void main(String[] args) {
// Your bot token
String token = "xoxb-your-bot-token";
// Channel to send the message to
String channel = "#prototype";
// Build the Slack client
SlackClient slackClient = SlackClient.builder()
.token(token)
.build();
// Build an ActionsBlock
ActionsBlock actionsBlock = ActionsBlock.builder()
.addElement(ButtonElement.link("Visit Website", "https://example.com"))
.addElement(ButtonElement.builder().text("Click me").actionId("YOUR ACTION ID").build()) //Elements support 2 types of creation
.build();
// Build the Slack message
SlackMessage message = SlackMessage.builder()
.channel(channel)
.text("New content π") // fallback message for notifications
.addBlock(TextBlock.builder().text("New content π").build())
.addBlock(ImageBlock.builder()
.imageUrl("https://n1netails.com/img/n1netails_icon_transparent.png")
.altText("N1netails token")
.build())
.addBlock(ImageBlock.of("https://n1netails.com/img/quickstart/n1netails-letter.jpg", "N1netails letter")) //Blocks support 2 types of creation
.addBlock(GifBlock.builder()
.gifUrl("https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExaDRhOWtpMnVsM2NiMzJ4aXpoOXpuamZzcHpudG4zbzIzenVlaHN0eSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xsE65jaPsUKUo/giphy.gif")
.altText("Fox GIF")
.build())
.addBlock(actionsBlock) // add the actions block with buttons
.build();
try {
// Send the message
slackClient.sendMessage(message);
System.out.println("Message sent successfully!");
} catch (SlackClientException e) {
System.err.println("Error sending message: " + e.getMessage());
}
}
}You can also send more complex messages using Slack's Block Kit.
import com.n1netails.n1netails.slack.api.SlackClient;
import com.n1netails.n1netails.slack.model.SlackMessage;
import com.slack.api.model.block.Blocks;
import com.slack.api.model.block.composition.BlockCompositions;
import com.n1netails.n1netails.slack.exception.SlackClientException;
import java.util.List;
public class AdvancedExample {
public static void main(String[] args) {
String token = "xoxb-your-bot-token";
String channel = "#prototype";
// Build the Slack client
SlackClient slackClient = SlackClient.builder()
.token(token)
.build();
// Build a Slack message with custom Block Kit blocks
SlackMessage message = SlackMessage.builder()
.channel(channel)
.text("This is a fallback message for notifications.") // fallback
.addRawBlock(
Blocks.section(section ->
section.text(BlockCompositions.markdownText("*This is a message with custom blocks.*"))
)
)
.build();
try {
slackClient.sendMessage(message);
System.out.println("Advanced message sent successfully!");
} catch (SlackClientException e) {
System.err.println("Error sending message: " + e.getMessage());
}
}
}Build the project using the following command
mvn clean installUse the following doc to get setup with publishing to the maven central repository https://central.sonatype.org/register/central-portal/#publishing
Maven install using release profile.
mvn clean install -P releaseMaven deploy to the maven central repository
mvn deploy -P releaseGenerate keys
gpg --full-generate-keyList keys
gpg --list-secret-keys --keyid-format LONGExport
gpg --export-secret-keys --armor YOUR_KEY_ID > private.ascgpg --export --armor YOUR_KEY_ID > public.ascImport
gpg --import private.asc
gpg --import public.ascList packets (good for validating keys were exported correctly)
gpg --list-packets public.ascFor community users, open an issue on GitHub or Join our Discord
Please use the following guidelines for contributions CONTRIBUTING
Thanks to all the amazing people who contributed to N1netails Slack Client! π

