slacklet

description

License

License

GroupId

GroupId

org.riversun
ArtifactId

ArtifactId

slacklet
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

slacklet
description
Project URL

Project URL

https://github.com/riversun/slacklet
Source Code Management

Source Code Management

https://github.com/riversun/slacklet

Download slacklet

How to add to project

<!-- https://jarcasting.com/artifacts/org.riversun/slacklet/ -->
<dependency>
    <groupId>org.riversun</groupId>
    <artifactId>slacklet</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/org.riversun/slacklet/
implementation 'org.riversun:slacklet:1.0.4'
// https://jarcasting.com/artifacts/org.riversun/slacklet/
implementation ("org.riversun:slacklet:1.0.4")
'org.riversun:slacklet:jar:1.0.4'
<dependency org="org.riversun" name="slacklet" rev="1.0.4">
  <artifact name="slacklet" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.riversun', module='slacklet', version='1.0.4')
)
libraryDependencies += "org.riversun" % "slacklet" % "1.0.4"
[org.riversun/slacklet "1.0.4"]

Dependencies

compile (6)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.6.2
org.threeten : threetenbp jar 1.3.1
org.apache.httpcomponents : httpclient jar 4.4
org.apache.httpcomponents : httpmime jar 4.4
javax.websocket : javax.websocket-api jar 1.1
org.glassfish.tyrus.bundles : tyrus-standalone-client jar 1.13

Project Modules

There are no modules declared in this project.

Overview

Slacklet is a easy slack bot library based on simple slack api.
You can make your interactive Slack Bot easily and rapidly.

It is licensed under MIT.

Maven Central

Get your bot token from Slack

First of all,get your bot token from here.
https://my.slack.com/services/new/bot

Quick Examples

1.Reply to user posted message

public class Example00 {

	public static void main(String[] args) throws IOException {

		String botToken = "[YOUR_BOT_TOKEN]";

		SlackletService slackService = new SlackletService(botToken);

		slackService.addSlacklet(new Slacklet() {

			@Override
			public void onMessagePosted(SlackletRequest req, SlackletResponse resp) {
				// user posted message and BOT intercepted it

				// get message content
				String content = req.getContent();

				// reply to the user
				resp.reply("You say '" + content + "'.");
			}
		});

		slackService.start();

	}

}

How to Run

  1. First, Invite your bot to #random channel.

  1. Run Example00

  2. You type 'Hi!' and bot replies this.

In the example above, it responds to messages that came to all channels the BOT is joining.
If you want to reply only to messages that came to the random channel, change as follows.

@Override
public void onMessagePosted(SlackletRequest req, SlackletResponse resp) {
	//  user posted message and BOT intercepted it

	SlackChannel channel = req.getChannel();

	if ("random".equals(channel.getName())) {

		// get message content
		String content = req.getContent();

		// reply to the user
		resp.reply("You say '" + content + "'.");

	}

}

2.Receive direct Message and mentioned Message

public class Example01 {

	public static void main(String[] args) throws IOException {

		String botToken ="[YOUR_BOT_TOKEN]";

		SlackletService slackService = new SlackletService(botToken);

		slackService.addSlacklet(new Slacklet() {

			@Override
			public void onDirectMessagePosted(SlackletRequest req, SlackletResponse resp) {
				// BOT received direct message from user

				// get message content
				String content = req.getContent();

				// reply to the user
				resp.reply("You say '" + content + "'.");
			}

			@Override
			public void onMentionedMessagePosted(SlackletRequest req, SlackletResponse resp) {
				// BOT received message mentioned to the BOT such like "@bot How are you?"
				// from user.

				String content = req.getContent();

				// get 'mention' text formatted <@user> of sender user.
				String mention = req.getUserDisp();
				resp.reply("Hi," + mention + ". You say '" + content + "'.");
			}

		});

		slackService.start();

	}

}

How to Run

  1. Run Example01
  2. You type 'Hello' as a direct message to bot.
  3. onDirectMessagePosted is called and it replies like this.

  1. You type 'How r u' on the random channel.
  2. onMentionedMessagePosted is called and it replies like this.


3.Sending Direct Message to user

public class Example02 {

	public static void main(String[] args) throws IOException {

		String botToken = "[YOUR_BOT_TOKEN]";

		SlackletService slackService = new SlackletService(botToken);
		slackService.start();

		String userName = "riversun";
		slackService.sendDirectMessageTo(userName, "Hello!");

		slackService.stop();

	}

}

How to Run

  1. Run Example02
  2. Direct message will be send to user specified by user name.


4.Sending message to specified channel

public class Example03 {

	public static void main(String[] args) throws IOException {

		String botToken = "[YOUR_BOT_TOKEN]";

		SlackletService slackService = new SlackletService(botToken);
		slackService.start();

		String channelName = "random";
		slackService.sendMessageTo(channelName, "Hi to random!");

		slackService.stop();

	}

}

How to Run

  1. Run Example03
  2. Posted message will be send to channel specified by channel name.


5.Sending a image

You can send image to slack by using attachment.

public class Example04 {

	public static void main(String[] args) throws IOException {

		String botToken = "[YOUR_BOT_TOKEN]";

		SlackletService slackService = new SlackletService(botToken);
		slackService.start();

		final String imageUrl = "https://riversun.github.io/img/riversun_144.png";

		final SlackAttachment attchImage = new SlackAttachment();
		attchImage.setTitle("");
		attchImage.setText("");
		attchImage.setFallback("");
		attchImage.setColor("#ffffff");
		attchImage.setImageUrl(imageUrl);

		String channelName = "random";
		slackService.sendMessageTo(channelName, "Hello!",attchImage);

		slackService.stop();

	}

}

6.Use session specific to the user

If you want to make conversational bot , the session will be useful for you.
When interacting with the user, it is necessary to keep the context for each user.
Then it establishes a conversation with the user.

public class Example05 {

	public static void main(String[] args) throws IOException {

		Logger.setEnalbed(false);

		String botToken = "[YOUR_BOT_TOKEN]";

		SlackletService slackService = new SlackletService(botToken);

		slackService.addSlacklet(new Slacklet() {

			@Override
			public void onDirectMessagePosted(SlackletRequest req, SlackletResponse resp) {
				// BOT received direct message from user

				// get message content
				String content = req.getContent();

				// get session for this sender user.
				SlackletSession session = req.getSession();

				Integer num = (Integer) session.getAttribute("num", 1);

				resp.reply("No." + num + " You say '" + content + "'.");

				// count up
				num++;

				session.setAttribute("num", num);

			}

		});

		slackService.start();

	}

}

How to Run

  1. Run Example04
  2. Type like as follows.SlackletSession is unique context for each user like servlet session.
    You can store user specific in it.

Integrate with Watson

As in the example below, watson handles the conversation.

Source code is in the following repository.

https://github.com/riversun/watson-java-slackbot

Maven

<dependency>
	<groupId>org.riversun</groupId>
	<artifactId>slacklet</artifactId>
	<version>1.0.4</version>
</dependency>

Versions

Version
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0