Add N1netails Client
Install
Install the slack client by adding the following dependency:
Maven
<dependency>
<groupId>com.n1netails</groupId>
<artifactId>n1netails-slack-client</artifactId>
<version>0.2.0</version>
</dependency>
Gradle
dependencies {
implementation 'com.n1netails:n1netails-slack-client:0.2.0'
}
Usage
Here's how to use the client to send a message:
import com.n1netails.n1netails.slack.api.SlackClient;
import com.n1netails.n1netails.slack.internal.SlackClientImpl;
import com.n1netails.n1netails.slack.model.SlackMessage;
import com.n1netails.n1netails.slack.service.BotService;
public class Example {
public static void main(String[] args) {
// Your bot token
String token = "xoxb-your-bot-token";
// The channel you want to send the message to (e.g., "#general")
String channel = "#prototype"; // or "#channel-name"
// Create the bot service
BotService botService = new BotService(token);
// Create the Slack client
SlackClient slackClient = new SlackClientImpl(botService);
// Create the message
SlackMessage message = new SlackMessage();
message.setChannel(channel);
message.setText("Hello from the N1ne Tails Slack Client!");
try {
// Send the message
slackClient.sendMessage(message);
System.out.println("Message sent successfully!");
} catch (Exception e) {
System.err.println("Error sending message: " + e.getMessage());
}
}
}
Advanced Usage (Using Block Kit)
You can also send more complex messages using Slack's Block Kit.
import com.n1netails.n1netails.slack.api.SlackClient;
import com.n1netails.n1netails.slack.internal.SlackClientImpl;
import com.n1netails.n1netails.slack.model.SlackMessage;
import com.n1netails.n1netails.slack.service.BotService;
import com.slack.api.model.block.Blocks;
import com.slack.api.model.block.composition.BlockCompositions;
import java.util.Arrays;
public class AdvancedExample {
public static void main(String[] args) {
String token = "xoxb-your-bot-token";
String channel = "#prototype";
BotService botService = new BotService(token);
SlackClient slackClient = new SlackClientImpl(botService);
SlackMessage message = new SlackMessage();
message.setChannel(channel);
message.setText("This is a fallback message for notifications.");
message.setBlocks(Arrays.asList(
Blocks.section(section -> section.text(BlockCompositions.markdownText("*This is a message with blocks.*")))
));
try {
slackClient.sendMessage(message);
System.out.println("Advanced message sent successfully!");
} catch (Exception e) {
System.err.println("Error sending message: " + e.getMessage());
}
}
}
Example message output
