0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Scalaでチャットワークに通知する

Last updated at Posted at 2018-01-23

下記コマンドでプロジェクト新規作成

sbt new sbt/scala-seed.g8

Chatworkへの通知にはchatwork4sを利用する

"tv.kazu" %% "chatwork4s" % "0.2.5.2",

chatwork4sはscala2.11はサポートしているとの記載があるのでbuild.sbtの下記部分でバージョンを指定した。

scalaVersion := "2.11.8",

試しにRoom情報を取得してみる

 11 object Main {
 12   val token: String = sys.env("CHATWORK_TOKEN")
 13   val client = new ChatWorkApiClient(token)
 14   val f: Future[Room] = client.room([your room id])
 15
 16   f.onSuccess { case room: Room => println(room) }
 17   f.onFailure { case t: Throwable => println(t.getMessage()) }
 18   println(f.isCompleted)
 19
 20   Await.ready(f, Duration.Inf)
 21 }

Roomに対して投稿するのは下記の通り。

  3 import scala.concurrent._
  4 import ExecutionContext.Implicits.global
  5 import scala.concurrent.duration.Duration
  6 import tv.kazu.chatwork4s.ChatWorkApiClient
  7 import tv.kazu.chatwork4s.ChatWorkApiResponse
  8 import tv.kazu.chatwork4s.models._
  9 import scala.concurrent.duration.Duration
 10 import scala.util.{ Try, Success, Failure }
 11
 12 object Main with App {
 13   val token: String = sys.env("CHATWORK_TOKEN")
 14   val client = new ChatWorkApiClient(token)
...
 22
 23   val f: Future[ChatWorkApiResponse] = client.post("/rooms/[your room id]/messages", Seq(("body", "hoge")))
 24
 25   f.onSuccess { case res: ChatWorkApiResponse => println(res) }
 26   f.onFailure { case t: Throwable => println(t.getMessage()) }
 27   println(f.isCompleted)
 28
 29   Await.ready(f, Duration.Inf)
 30 }

無事に「hoge」を投稿することができた。

全部入りのjarファイルを作って実行したかったので、project/assebly.sbt に下記を追加してsbt-assemblyを追加。

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")

下記のsbtコマンドでjarパッケージを作成する。

sbt assembly

jarファイルを実行できることを確認

java -jar target/scala-2.11/Sample-assembly-0.1.0-SNAPSHOT.jar
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?