Skip to content

Instantly share code, notes, and snippets.

@ignasi35
Last active January 4, 2019 18:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ignasi35/9351ef4bdc5f7768e27182d06bfc8c01 to your computer and use it in GitHub Desktop.
Save ignasi35/9351ef4bdc5f7768e27182d06bfc8c01 to your computer and use it in GitHub Desktop.
A PoC client for Slack's "Custom Integrations" implemented as a Lagom Service.
package com.example.hello.api
import akka.NotUsed
import com.example.hello.api.SlackCustomIntegration.Message
import com.lightbend.lagom.scaladsl.api.{Descriptor, Service, ServiceCall}
import com.lightbend.lagom.scaladsl.api.Service._
import com.lightbend.lagom.scaladsl.api.transport.Method
import play.api.libs.json._
import play.api.libs.functional.syntax._
trait SlackService extends Service {
// Slack's custom integrations create an incoming webhook with the pattern:
// https://hooks.slack.com/services/T000000/B000000/XXXXXXXXXX
// I'm not familiar with the meaning of the codes so I made up this names
// T000000 --> slackId
// B000000 --> slackTeamCode
// XXXXXXXXXX --> slackTeamKey
//
// Go to https://api.slack.com/custom-integrations for details on getting custom integrations webhooks for your team.
override def descriptor: Descriptor =
named("slack").withCalls(
restCall(Method.POST,
"/services/T000000/B000000/XXXXXXXXXX", // TODO: read these 3 values from conf.
sendMessage _)
)
def sendMessage(): ServiceCall[Message, NotUsed]
// alternatively, delegate the use of slackId, slackTeamCode and slackTeamKey to every invocation
// def sendMessageWithIds(slackId: String,
// slackTeamCode: String,
// slackTeamKey: String): ServiceCall[Message, NotUsed]
// Then replace the restCall above with:
// restCall(Method.POST, "/services/:id/:team/:key", sendMessageWithIds _)
}
object SlackCustomIntegration {
// TODO: use stronger typing on title_link
case class Attachment(titles: String, title_link: String, test: String)
case class Message(text: String,attachments: Option[Attachment] = None,
channel: Option[String] = None, // use #channel or @user for DM's
username: Option[String] = None, icon_emoji: Option[String] = None
)
object Message{ implicit val format: Format[Message] = Json.format }
object Attachment{ implicit val format: Format[Attachment] = Json.format }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment