Skip to content

Instantly share code, notes, and snippets.

@johandahlberg
Created June 23, 2016 17:17
Show Gist options
  • Save johandahlberg/e48732ebd03dbe7e542f3f1aa7db8bc9 to your computer and use it in GitHub Desktop.
Save johandahlberg/e48732ebd03dbe7e542f3f1aa7db8bc9 to your computer and use it in GitHub Desktop.
def rawbuffer2JsValue(rawBuffer: RawBuffer): Option[JsValue] = {
for {
bytes <- rawBuffer.asBytes()
} yield {
Json.parse(bytes.utf8String)
}
}
def receiveFacebookMessages = withVerifiedPayload {
Action (parse.raw) {
request => {
val response =
for {
json <- rawbuffer2JsValue(request.body)
} yield {
import MessageFormats.facebookPostingMessagesReads
val messages = json.validate[FacebookPostingMessages]
messages.fold(
errors => {
Logger.warn("Failed to validate message post...")
BadRequest(Json.obj("status" ->"KO", "message" -> JsError.toJson(errors)))
},
messages => {
Logger.info(s"Successfully parsed ${messages.messages().length} messages.")
Ok
}
)
}
response.getOrElse({
Logger.warn("Invalid request posted.")
BadRequest("Invalid message posting...")
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment