zio-slack-client


License

License

Categories

Categories

CLI User Interface
GroupId

GroupId

com.github.dapperware
ArtifactId

ArtifactId

zio-slack-client_2.12
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

zio-slack-client
zio-slack-client
Project URL

Project URL

https://github.com/dapperware/zio-slack
Project Organization

Project Organization

com.github.dapperware
Source Code Management

Source Code Management

https://github.com/dapperware/zio-slack/

Download zio-slack-client_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.dapperware/zio-slack-client_2.12/ -->
<dependency>
    <groupId>com.github.dapperware</groupId>
    <artifactId>zio-slack-client_2.12</artifactId>
    <version>0.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.dapperware/zio-slack-client_2.12/
implementation 'com.github.dapperware:zio-slack-client_2.12:0.3.0'
// https://jarcasting.com/artifacts/com.github.dapperware/zio-slack-client_2.12/
implementation ("com.github.dapperware:zio-slack-client_2.12:0.3.0")
'com.github.dapperware:zio-slack-client_2.12:jar:0.3.0'
<dependency org="com.github.dapperware" name="zio-slack-client_2.12" rev="0.3.0">
  <artifact name="zio-slack-client_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.dapperware', module='zio-slack-client_2.12', version='0.3.0')
)
libraryDependencies += "com.github.dapperware" % "zio-slack-client_2.12" % "0.3.0"
[com.github.dapperware/zio-slack-client_2.12 "0.3.0"]

Dependencies

compile (8)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.10
com.github.dapperware : zio-slack-core_2.12 jar 0.3.0
io.circe : circe-generic_2.12 jar 0.12.3
dev.zio : zio-macros-core_2.12 jar 0.6.2
dev.zio : zio-macros-test_2.12 jar 0.6.0
com.softwaremill.sttp.client : core_2.12 jar 2.0.0-RC6
com.softwaremill.sttp.client : circe_2.12 jar 2.0.0-RC6
com.softwaremill.sttp.client : async-http-client-backend-zio_2.12 jar 2.0.0-RC6

test (2)

Group / Artifact Type Version
dev.zio : zio-test_2.12 jar 1.0.0-RC17
dev.zio : zio-test-sbt_2.12 jar 1.0.0-RC17

Project Modules

There are no modules declared in this project.

zio-slack

An idiomatic slack client using zio

Release Artifacts CircleCI

Installation

Add the following dependency to your project's build file

For Scala 2.12.x and 2.13.x

"com.github.dapperware" %% "zio-slack-api-web" % "0.7.0"

zio-slack is a library for interfacing with slack using an idiomatic and easily discoverable interface.

We define most of the methods defined here: https://api.slack.com/methods (Working toward 100% coverage!). If there is one missing or you find a bug in how it is implemented please submit an issue or PR (There are a lot of methods to cover and automated coverage isn't fully available yet).

Usage

Usage is quite simple. First you can define how you would like to interact with slack. For instance, say you periodically wanted to send chuck norris jokes to random channels (...please don't).

Since we will be sending messages we will be using the chats api which we can import like so:

import slack.api.chats._

Note that you can pull in the functionality piecemeal like above or all at once using slack.api.web._ you can even pull in individual methods! The beauty of ZIO is that all the methods just return effects so there is no need to instantiate anything until you are ready to execute it.

We'll use the STTP library since it is what zio-slack is built with. First lets makes a small request to fetch chuck norris jokes:

  val getJoke: Request[Either[ResponseError[circe.Error], String], Nothing] = basicRequest
    .get(uri"https://api.chucknorris.io/jokes/random")
    .response(asJson[Json])
    .mapResponseRight(_.hcursor.downField("value").as[String])

Next lets build a small program that would send these jokes to our slack workspace:

(for {
  resp <- SttpClient.send(getJoke)
  joke <- IO.fromEither(resp.body) >>= IO.fromEither
  _    <- postChatMessage("<channel_id>", joke)
} yield ()).repeat(Schedule.fixed(3.hours))

Almost there.

You may have noticed that this is now returning compiler errors saying that it expects a Clock with SlackEnvDefinition.this.SlackEnv this is expected, ZIO is telling us that we need to supply our Slack environment in order to run, lets add that now, for completeness we'll throw the whole thing into a ManagedApp:

import io.circe
import io.circe.Json
import slack.api.chats._
import slack.AccessToken
import slack.core.client.SlackClient
import sttp.client._
import sttp.client.asynchttpclient.zio.AsyncHttpClientZioBackend
import sttp.client.circe._
import zio.duration._
import zio.{ IO, App, Schedule, ExitCode }

object JokeApp extends App {

  // Builds a request which will fetch a new chuck norris joke
  val getJoke: Request[Either[ResponseError[circe.Error], Either[DecodingFailure, String]], Nothing] = basicRequest
    .get(uri"https://api.chucknorris.io/jokes/random")
    .response(asJson[Json])
    .mapResponseRight(_.hcursor.downField("value").as[String])
    
  // Creates our static environmental layer
  val envLayer = AsyncHttpClientZioBackend.layer() >>> SlackClient.live

  override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] =
    (for {
      resp <- SttpClient.send(getJoke)                    // Gets a new joke
      joke <- IO.fromEither(resp.body) >>= IO.fromEither  // Decodes the joke response
      _    <- postChatMessage("<your-channel-id>", joke)  // Sends the joke to the channel of your choice
      } yield ())
       .repeat(Schedule.fixed(3.hours)) // Repeat every three hours
       .provideCustomLayer(envLayer ++ AccessToken.make("xoxb-<your-token>").toLayer) // Add the token used to authorize requests to slack
       .exitCode
}
com.github.dapperware

Versions

Version
0.3.0
0.2.1
0.2