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.
First of all,get your bot token from here.
https://my.slack.com/services/new/bot
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();
}
}
- First, Invite your bot to #random channel.
-
Run Example00
-
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 + "'.");
}
}
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();
}
}
- Run Example01
- You type 'Hello' as a direct message to bot.
- onDirectMessagePosted is called and it replies like this.
- You type 'How r u' on the random channel.
- onMentionedMessagePosted is called and it replies like this.
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();
}
}
- Run Example02
- Direct message will be send to user specified by user name.
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();
}
}
- Run Example03
- Posted message will be send to channel specified by channel name.
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();
}
}
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();
}
}
- Run Example04
- Type like as follows.SlackletSession is unique context for each user like servlet session.
You can store user specific in it.
As in the example below, watson handles the conversation.
Source code is in the following repository.
https://github.com/riversun/watson-java-slackbot
<dependency>
<groupId>org.riversun</groupId>
<artifactId>slacklet</artifactId>
<version>1.0.4</version>
</dependency>