ponkotuy / scala-slack   0.4.0

MIT License GitHub

A Slack client (https://slack.com) for Scala.

Scala versions: 2.11

scala-slack

Build Status

scala-slack is a simple, extensible client library for Scala that provides an interface to the Slack API.

Note: scala-slack is a partial implementation of the Slack API. The Slack API is under heavy development, and this library is subject to frequent change.

Supported Methods

  • api.test
  • auth.test
  • channels.{history, list, setTopic, archive, unarchive, info, invite, join, kick, leave, mark, rename, setPurpose}
  • chat.{delete, postMessage, update}
  • im.{close, history, list, mark, open}
  • users.{info, list, getPresence, setArchive, setPresence}

Download

scala-slack is listed on Maven Central and can be included in your project by adding this line to your build.sbt:

libraryDependencies += "com.ponkotuy" %% "scala-slack" % "0.5.0"

scala-slack support only Scala 2.11.

Usage

First, instantiate a SlackClient object.

import com.ponkotuy.slack.SlackClient

val s = new SlackClient(<YOUR_API_TOKEN>)

You can then use Slack API methods:

s.chat.postMessage("#yourchannel", "Hello World!")

You can also edit and delete messages easily by using the returned PostMessageResponse object:

val response = s.chat.postMessage("#yourchannel", "Hello World!")

s.chat.update(response, "This is my update.")
s.chat.delete(response)

Message history can be retrieved with the channels.history and channels.historyStream methods:

val response = s.channels.list()
val channel = response.channels(0)

val history = s.channels.history(channel.id)
println(history.messages)

// get stream of messages in descending order
val historyStream = s.channels.historyStream(channel.id)

historyStream foreach println

All implemented API methods are documented in the code with Scaladoc.

Extending the library

scala-slack can easily be extended to accommodate new API methods and functionality.

For example, if Slack adds a chat.poke method, one could write a new version of the Chat class:

import com.ponkotuy.slack.HttpClient
import com.ponkotuy.slack.methods.Chat

class PokeChat(httpClient: HttpClient, apiToken: String) extends Chat(httpClient, apiToken) {

   def poke(userID: String) = {
      val response = httpClient.post(
        "chat.poke",
        Map("user_id" -> userID, "token" -> apiToken)
      )

      // handle poke JSON response here...
   }

}

and then you can incorporate this into a custom version of SlackClient:

import com.ponkotuy.slack.SlackClient

class PokeSlackClient(apiToken: String) extends SlackClient(apiToken) {

   override val chat = new PokeChat(httpClient, apiToken)

}

That being said, we hope that if you implement additional API methods yourself, you'll consider adding them to this project :)

License

This project is licensed under the MIT license.