GithubHelp home page GithubHelp logo

slacklet's Introduction

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>

slacklet's People

Contributors

riversun avatar ethgartaboola avatar

Stargazers

Kenichi Inoue avatar Bob Plotts avatar Andrei Sasu avatar Armen avatar 伊欧 avatar Pine Mizune avatar  avatar Friedhold Matz avatar Pavlo Pastushok avatar Eugene Kortov avatar R.F avatar yamap55 avatar Kento Takeuchi avatar

Watchers

James Cloos avatar Friedhold Matz avatar  avatar

Forkers

ethgard

slacklet's Issues

Slack reconnecting process

Hi,
How can I implement reconnecting process on Slack connection drop?
Should I use addSlackDisconnectedListener?

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.