Skip to content

Repository files navigation

N1netails

N1ne Tails

License: MIT

Stars Issues Contributors Last Commit

Slack Client

This client allows you to send messages to a Slack channel using a Slack App.

Setup

  1. 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.
  2. 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:write scope. This allows your app to send messages to channels it's a member of.
  3. Install the App:
    • At the top of the "OAuth & Permissions" page, click "Install App to Workspace".
    • Follow the prompts to authorize the app.
  4. Get Your Bot Token:
    • After installation, you'll see a "Bot User OAuth Token". It will start with xoxb-. Copy this token.
  5. 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 (replace your-app-name with the name of your app) and send the message.

Install

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'

Usage

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());
        }
    }
}

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.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());
        }
    }
}

Example message output

N1netails slack message simple

Example media output

N1netails slack message simple

Develop

Build

Build the project using the following command

mvn clean install

Maven Central Repository

Use 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 release

Maven deploy to the maven central repository

mvn deploy -P release

GPG

Generate keys

gpg --full-generate-key

List keys

gpg --list-secret-keys --keyid-format LONG

Export

Export private key

gpg --export-secret-keys --armor YOUR_KEY_ID > private.asc

Export public key

gpg --export --armor YOUR_KEY_ID > public.asc

Import

gpg --import private.asc
gpg --import public.asc

List packets (good for validating keys were exported correctly)

gpg --list-packets public.asc

Support

For community users, open an issue on GitHub or Join our Discord

Join our Discord

Contributing

Please use the following guidelines for contributions CONTRIBUTING

N1netails Slack Client Contributors

Thanks to all the amazing people who contributed to N1netails Slack Client! πŸ’™

Contributors

About

A lightweight Java client for sending Slack notifications.

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages