Skip to content

pradeepyadav15may/simple-slack-api

 
 

Repository files navigation

Simple Slack API

This library allows an aplication to connect to Slack to receive and send messages from any channel as if it were a slack client.

The main purpose of this library is to build slack bots able to react to channel events without having to configure any web hook.

Short example

Slack connection :

The connection is made through the SlackSessionFactory class :

Direct connection :

SlackSession session = SlackSessionFactory.
  createWebSocketSlackSession("authenticationtoken",true);
session.connect();

Through a proxy :

SlackSession session = SlackSessionFactory.
  createWebSocketSlackSession("authenticationtoken", Proxy.Type.HTTP, "myproxy", 1234,true);
session.connect();

Listening to messages :

Slack messages can be retrieved by attaching a SlackMessageListener to the session provided by the factory :

  session.addMessagePostedListener(new SlackMessagePostedListener()
  {
      @Override
      public void onEvent(SlackMessagePosted event, SlackSession session)
      {
          session.sendMessageOverWebSocket(session.findChannelByName("general"), "Message sent : " + event.getMessageContent(), null);
      }
  });

(Since v0.4.0, lambda friendly version)

session.addMessagePostedListener((e, s) 
  -> s.sendMessageOverWebSocket(s.findChannelByName("general"), "Message sent : " + e.getMessageContent(), null));

Message operation :

The SlackSession interface provides various methods to send/modify and delete messages on a given channel :

SlackMessageHandle sendMessage(SlackChannel channel, String message, SlackAttachment attachment);
SlackMessageHandle sendMessage(SlackChannel channel, String message, SlackAttachment attachment, SlackChatConfiguration chatConfiguration);
SlackMessageHandle updateMessage(String timeStamp, SlackChannel channel, String message);
SlackMessageHandle deleteMessage(String timeStamp, SlackChannel channel)

Supported events :

The following events are handled :

  • message post on a channel / group / to the bot
  • message update on a channel / group / to the bot
  • message delete on a channel / group / to the bot
  • channel creation
  • channel deletion
  • channel archiving
  • channel unarchiving
  • channel renaming
  • private group joining

Full example :

Here's a full example with an echoing bot using this library :

public class Example
{

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

    final SlackSession session = SlackSessionFactory.
      createWebSocketSlackSession("authenticationtoken", Proxy.Type.HTTP, "myproxy", 1234, true);
    session.addMessagePostedListener(new SlackMessagePostedListener()      
    session.addMessageListener(new SlackMessageListener()
      {
        @Override
        public void onEvent(SlackMessagePosted event, SlackSession session)
        {
          //let's send a message
          SlackMessageHandle handle = session.sendMessage(slackMessage.getChannel(),
                              slackMessage.getMessageContent(), null);
          try
          {
              Thread.sleep(2000);
          } catch (InterruptedException e)
          {
              e.printStackTrace();
          }
          //2 secs later, let's update the message (I can only update my own messages)
          session.updateMessage(handle.getSlackReply().getTimestamp(),slackMessage.getChannel(),
                                slackMessage.getMessageContent()+" UPDATED");
          try
          {
              Thread.sleep(2000);
          } catch (InterruptedException e)
          {
              e.printStackTrace();
          }
          //2 secs later, let's now delete the message (I can only delete my own messages)
          session.deleteMessage(handle.getSlackReply().getTimestamp(),slackMessage.getChannel())
        }
      });
    session.connect();

    while (true)
    {
      Thread.sleep(1000);
    }
  }
}

License

This library is licensed under the Creative Commons CC0 1.0 Universal license with no warranty (expressed or implied) for any purpose.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%