/
slack.scala
47 lines (38 loc) · 1.24 KB
/
slack.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package example
import java.time.Instant
import java.io.{InputStream, OutputStream}
import java.nio.charset.StandardCharsets.UTF_8
import cats.data.Xor._
import io.circe.generic.auto._, io.circe.parse.decode, io.circe.syntax._
import DateAndTime._
case class SlashCommand(
token : String,
team_id : String,
team_domain : String,
channel_id : String,
channel_name : String,
user_id : String,
user_name : String,
command : String,
text : String,
response_url : String
)
case class LongResponse(
text : String,
response_type : String = "in_channel"
)
class Slack {
def time(in: InputStream, out: OutputStream): Unit = {
val payload = scala.io.Source.fromInputStream(in).mkString("")
val reply = decode[SlashCommand](payload) match {
case Right(cmd) if cmd.text.isEmpty => s"Usage: /time brighton sydney new-york"
case Left(err) => s"Sorry, I don't understand that: $err"
case Right(cmd) =>
val now = Instant.now
val places = names(cmd.text)
val descriptions = places.map(name => describe(now, name, zoneIdOf(name.toLowerCase)))
LongResponse(descriptions.mkString("\n")).asJson.noSpaces
}
out.write(reply.getBytes(UTF_8))
}
}