Created
June 23, 2016 17:17
-
-
Save johandahlberg/e48732ebd03dbe7e542f3f1aa7db8bc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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