Commit 5ff49c97 authored by Andreas Muttscheller's avatar Andreas Muttscheller

Fix slack url verification

parent 4e52345f
......@@ -8,9 +8,14 @@ import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.{APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent}
import com.typesafe.scalalogging.Logger
import de.codecentric.amuttsch.bahndelayinfo.aws
import de.codecentric.amuttsch.bahndelayinfo.slackbot.UrlVerification
import org.json4s._
import org.json4s.native.JsonMethods.parse
import org.json4s.native.Serialization.write
import scala.collection.JavaConverters._
import scala.util.{Failure, Success, Try}
class APISlackBotEventHandler {
implicit val logger: Logger = Logger(classOf[APISlackBotEventHandlerWorker])
implicit val jsonFormats: DefaultFormats.type = DefaultFormats
......@@ -21,6 +26,18 @@ class APISlackBotEventHandler {
logger.info(s"Got event ${event.getBody}")
if (event.getBody != null) {
// Handle url verification here
Try(parse(event.getBody)) match {
case Success(json) =>
json \ "type" match {
case JString("url_verification") =>
val urlVerificationMsg = json.extract[UrlVerification]
return urlVerification(urlVerificationMsg)
case _ =>
}
case Failure(_) =>
}
val payloadStr = "payload="
val body = if (event.getBody.startsWith(payloadStr)) {
URLDecoder.decode(event.getBody.substring(payloadStr.length), "UTF-8")
......@@ -37,4 +54,15 @@ class APISlackBotEventHandler {
new APIGatewayProxyResponseEvent()
.withStatusCode(200)
}
def urlVerification(urlVerification: UrlVerification): APIGatewayProxyResponseEvent = {
new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody(urlVerification.challenge)
.withHeaders(
Map(
"Content-type" -> "text/plain"
).asJava
)
}
}
\ No newline at end of file
......@@ -14,7 +14,6 @@ import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.util.{Failure, Success, Try}
......@@ -54,9 +53,6 @@ class APISlackBotEventHandlerWorker {
logger.info(compact(render(json)))
json \ "type" match {
case JString("url_verification") =>
val urlVerificationMsg = json.extract[UrlVerification]
urlVerification(urlVerificationMsg)
case JString("event_callback") =>
val eventId = (json \ "event_id").extract[String]
if (seenEvents.contains(eventId)) {
......@@ -90,17 +86,6 @@ class APISlackBotEventHandlerWorker {
}
}
def urlVerification(urlVerification: UrlVerification): APIGatewayProxyResponseEvent = {
new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody(urlVerification.challenge)
.withHeaders(
Map(
"Content-type" -> "text/plain"
).asJava
)
}
def parseInteractiveMessageAction(msg: InteractiveMessage): Unit = {
implicit var slackUser: SlackUser = getOrCreateUser(msg.user.id, msg.channel.id)
......
......@@ -66,7 +66,7 @@ class SNSChangeSlackReporter {
.filter(_.eva == newDelayInformation.eva)
.filter(!_.isHistory)
.filter(_.delayInMinutes > 2)
.cross(slackUserCache.get())
.cross(slackUserCache.get(Unit))
.filter { case (tti, sr) =>
!sr.seenTimetableInformation.contains(tti.id) ||
// Only send messages when time difference is greater than 2 minutes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment