Coder Social home page Coder Social logo

zio-slack's Introduction

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.9.5"

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 com.github.dapperware.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 com.github.dapperware.slack.api.chats._
import com.github.dapperware.slack.access.AccessToken
import com.github.dapperware.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
}

zio-slack's People

Contributors

ashwinbhaskar avatar derpyplops avatar paulpdaniels avatar scala-steward avatar

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.