LoginSignup
1
2

More than 5 years have passed since last update.

ScalaでSlack連携 その1

Posted at

Incoming WebHooks URL を取得する

https://<Your Team Name>.slack.com/apps/build にアクセスし

  • Make a Custom Integration を選択
    2016-11-10_00h02_26.png

  • Incoming WebHooks を選択
    2016-11-10_00h03_00.png

  • でいろいろしてWebHooks URLを取得

Scala で post してみる

postするのに play-ws を利用する。
※ scalaVersionは2.11.7
それ以上のversionにするとjava.lang.NoClassDefFoundError: scala/Product$class
Compile Dependenciesをちゃんと見ろということですね。すいません。

build.scala
name := "slack-incoming"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "com.typesafe.play" % "play-ws_2.11" % "2.5.9"
)
Main.scala
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws._
import play.api.libs.ws.ahc.AhcWSClient

import scala.concurrent.Future

object Main {
  import scala.concurrent.ExecutionContext.Implicits._
  def main(args: Array[String]): Unit = {

    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    val wsClient = AhcWSClient()

    call(wsClient)
      .andThen { case _ => wsClient.close() }
      .andThen { case _ => system.terminate() }
  }

  def call(wsClient: WSClient) = {
    val postJson = """{"text": "This is a line of text in a channel.\nAnd this is another line of text."}"""
    val contentType = ("Content-Type" -> "application/json")

    wsClient.url("<Web Hooks URL>")
      .withHeaders(contentType)
      .post(postJson)
  }
}

Slackで確認

2016-11-10_00h07_30.png

すごく・・・かんたんです・・・

その2があるかは未定

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2