All Projects → slack-scala-client → Slack Scala Client

slack-scala-client / Slack Scala Client

Licence: mit
A scala library for interacting with the slack api and real time messaging interface

Programming Languages

java
68154 projects - #9 most used programming language
scala
5932 projects

Projects that are alternatives of or similar to Slack Scala Client

Rocket.chat
The communications platform that puts data protection first.
Stars: ✭ 31,251 (+17656.25%)
Mutual labels:  hacktoberfest, slack
Busy Beaver
The Chicago Python Community Engagement Slack bot
Stars: ✭ 66 (-62.5%)
Mutual labels:  hacktoberfest, slack
Rocket.chat.electron
Official OSX, Windows, and Linux Desktop Clients for Rocket.Chat
Stars: ✭ 1,108 (+529.55%)
Mutual labels:  hacktoberfest, slack
Alertmanager
Prometheus Alertmanager
Stars: ✭ 4,574 (+2498.86%)
Mutual labels:  hacktoberfest, slack
Slack Orb
Create custom Slack notifications for CircleCI job statuses
Stars: ✭ 129 (-26.7%)
Mutual labels:  hacktoberfest, slack
Themer
themer is inspired by trevordmiller/nova and chriskempson/base16.
Stars: ✭ 4,483 (+2447.16%)
Mutual labels:  hacktoberfest, slack
Auto
Generate releases based on semantic version labels on pull requests.
Stars: ✭ 1,120 (+536.36%)
Mutual labels:  hacktoberfest, slack
Fantasy Football Metrics Weekly Report
Command line application to create weekly reports (containing stats, metrics, and rankings) for Fantasy Football leagues on the following platforms: Yahoo, Fleaflicker, Sleeper, ESPN.
Stars: ✭ 62 (-64.77%)
Mutual labels:  hacktoberfest, slack
Irslackd
Self-hosted IRC gateway to Slack
Stars: ✭ 128 (-27.27%)
Mutual labels:  hacktoberfest, slack
Cultofthepartyparrot.com
PARTY OR DIE
Stars: ✭ 1,254 (+612.5%)
Mutual labels:  hacktoberfest, slack
Rigel
🌌 Colorscheme for vim, terminal, vscode and slack - based on the star Rigel ✨.
Stars: ✭ 324 (+84.09%)
Mutual labels:  hacktoberfest, slack
Emberclear
Encrypted Chat. No History. No Logs.
Stars: ✭ 157 (-10.8%)
Mutual labels:  hacktoberfest, slack
Ferdi
🧔🏽 Ferdi helps you organize how you use your favourite apps by combining them into one application
Stars: ✭ 4,089 (+2223.3%)
Mutual labels:  hacktoberfest, slack
Notify
A dead simple Go library for sending notifications to various messaging services.
Stars: ✭ 727 (+313.07%)
Mutual labels:  hacktoberfest, slack
Ex mustang
✨ A simple, clueless bot
Stars: ✭ 67 (-61.93%)
Mutual labels:  hacktoberfest, slack
Slackbotapi
node.js Slack RTM API module
Stars: ✭ 135 (-23.3%)
Mutual labels:  hacktoberfest, slack
Matrix Appservice Slack
A Matrix <--> Slack bridge
Stars: ✭ 164 (-6.82%)
Mutual labels:  hacktoberfest, slack
Video trimmer
Flutter video trimmer package
Stars: ✭ 173 (-1.7%)
Mutual labels:  hacktoberfest
Gino
GINO Is Not ORM - a Python asyncio ORM on SQLAlchemy core.
Stars: ✭ 2,299 (+1206.25%)
Mutual labels:  hacktoberfest
K8spin Operator
K8Spin multi-tenant operator - OSS
Stars: ✭ 175 (-0.57%)
Mutual labels:  hacktoberfest

slack-scala-client

Build Status

A Scala library for interacting with the Slack API and real time messaging interface

Installation

SBT

Add SBT dependency:

libraryDependencies += "com.github.slack-scala-client" %% "slack-scala-client" % "0.2.14"

Maven

    <dependency>
        <groupId>com.github.slack-scala-client</groupId>
        <artifactId>slack-scala-client_${scala.version}</artifactId>
        <version>0.2.14</version>
    </dependency>

API Client Usage

There are two different API clients, one exposing an asynchronous interface and the other exposing a synchronous interface. They can be imported from the slack.api package:

import slack.api.SlackApiClient          // Async
import slack.api.BlockingSlackApiClient  // Blocking

Creating an instance of either client simply requires passing in a Slack api token:

val token = "<Your Token Here>"
val client = SlackApiClient(token)

Calling any api functions requires an implicit ActorSystem... one can be created simply:

implicit val system = ActorSystem("slack")

The async client returns futures as the result of each of its API functions:

val client = SlackApiClient(token)
val res = client.listChannels() // => Future[Seq[Channel]]

res.onComplete {
    case Success(channels) =>  //...
    case Failure(err) => // ...
}

...while the blocking client will block the current thread until the API response has been received:

val client = BlockingSlackApiClient(token)  // Default timeout of 5 seconds
val channels = client.listChannels()  // => Seq[Channel]

The API clients implement the full Slack API. A full list of the available endpoints can be found directly on the classes: SlackApiClient and BlockingSlackApiClient

RTM Client Usage

The real time messaging client is implemented using akka and requires having an implicit ActorSystem in scope. Either an ActorSystem or ActorContext will work:

import slack.rtm.SlackRtmClient
import akka.actor.ActorSystem

implicit val system = ActorSystem("slack")

Creating an instance of the RTM client requires an API token, just like the API clients:

val token = "<Your Token Here>"
val client = SlackRtmClient(token)

Based on the stream of events coming in, the client maintains active state that contains things like channels and users. It can also be used to look up the ID of a user or channel by name:

val state = client.state
val selfId = state.self.id
val chanId = state.getChannelIdForName("general") // => Option[String]

Sending a message is pretty simple:

val generalChanId = state.getChannelIdForName("general").get
client.sendMessage(generalChanId, "Hello!")

Messages can be received very simply as well:

client.onMessage { message =>
    println(s"User: ${message.user}, Message: ${message.text}")
}

Additionally, the client can be used to receive any event sent from Slack:

client.onEvent {
    case e: Message => ...
    case e: UserTyping => ...
    case e: ChannelDeleted => ...
}

A full list of events can be found in Events.scala. One thing to note is the two above functions return an ActorRef which is a handle to the underlying actor running the above handler function. This can be used to terminate the handler by terminating the actor: system.stop(handler), or unregistering it as a listener: client.removeEventListener(handler)

An Akka actor can be manually registered as an event listener and all events will be sent to that actor:

val actor = system.actorOf(Props[SlackEventHandler])
client.addEventListener(actor)
// Time Passes...
client.removeEventListener(actor)

Finally, an RTM client can easily be terminated and cleaned up by calling close:

client.close()

Simple Bot Example

This is a full implementation of a Slack bot that will listen for anyone mentioning it in a message and will respond to that user.

val token = "..."
implicit val system = ActorSystem("slack")
implicit val ec = system.dispatcher

val client = SlackRtmClient(token)
val selfId = client.state.self.id

client.onMessage { message =>
  val mentionedIds = SlackUtil.extractMentionedIds(message.text)

  if(mentionedIds.contains(selfId)) {
    client.sendMessage(message.channel, s"<@${message.user}>: Hey!")
  }
}

Caveat Emptor

  • The Slack API contains a lot methods and not every implemented API method has been executed (i.e. some may not work; pull requests accepted!)
  • Responses to RTM messages sent out are not currently checked to verify they were successfully received
  • Investigate a way to ensure all missed messages are received during a disconnection
  • A small number of response types have yet to be fleshed out

Changelog

Changelog can be found here

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].