 /// This is step 5 of the following guide:
 /// https://developers.facebook.com/docs/messenger-platform/guides/quick-start
 droplet.post("messenger", messengerSecret, "*") { request in
-    if let contentType = request.headers["Content-Type"], contentType.contains("application/json"), let bytes = request.body.bytes {
-        let json = try JSON(bytes: bytes)
-        droplet.console.info("Got JSON: \(json)")
+    guard request.json?["object"]?.string == "page" else {
+        throw Abort.custom(status: .badRequest, message: "Message not generated by a page.")
     }
     
-    return Response(status: .ok, headers: ["Content-Type": "text/plain"], body: "OK")
+    /// Let's prepare the response message text.
+    var response: String = ""
+    
+    let entries: [Polymorphic] = request.json?["entry"]?.array ?? []
+    
+    for entry in entries {
+        let pageID: String = entry.object?["id"]?.string ?? "0"
+        let messages: [Polymorphic] = entry.object?["messaging"]?.array ?? []
+        
+        for event in messages {
+            let message: [String: Polymorphic] = event.object?["message"]?.object ?? [:]
+            
+            let recipient: [String: Polymorphic] = event.object?["recipient"]?.object ?? [:]
+            let recipientID: String = recipient["id"]?.string ?? ""
+            let text: String = message["text"]?.string ?? ""
+            
+            /// Check if the message object is empty.
+            if message.isEmpty {
+                /// Set the response message text.
+                response = "Webhook received unknown event."
+            /// Check if the message text is empty
+            } else if text.isEmpty {
+                response = "I'm sorry but your message is empty 😢"
+            /// The message object and its text are not empty.
+            } else {
+                /// Set the response message text.
+                response = text.reversed(preserveFormat: true)
+                
+                let responseData = try JSON(node: ["recipient": JSON(node: ["id": recipientID]), "message": JSON(node: ["text": response])]).makeBytes()
+                
+                let facebookAPICall = try droplet.client.post("https://graph.facebook.com/v2.8/me/messages", headers: ["Content-Type": "application/json"], query: ["access_token": messengerToken], body: Body.data(responseData))
+            }
+        }
+    }
+    
+    return Response(status: .ok, headers: ["Content-Type": "application/json"])
 }
 
 /// Run the Droplet.

/// With a secret path to be sure that nobody else knows that URL.
/// https://core.telegram.org/bots/api#setwebhook
droplet.post("telegram", telegramSecret) { request in
    /// If Telegram secret key is missing throw a bad request error.
    guard telegramSecret != "" else {
        throw Abort.custom(status: .badRequest, message: "Missing Telegram secret key!")
    }
    
    /// Let's prepare the response message text.
    var response: String = ""
    

/// Read Telegram secret key from Config/secrets/app.json.
let telegramSecret = droplet.config["app", "telegram", "secret"]?.string ?? ""
let messengerSecret = droplet.config["app", "messenger", "secret"]?.string ?? ""
let messengerToken = droplet.config["app", "messenger", "token"]?.string ?? ""

guard telegramSecret != "" || (messengerSecret != "" && messengerToken != "") else {
    /// Show errors in console.
    droplet.console.error("Missing secret keys!")
    droplet.console.error("Add almost one in Config/secrets/app.json")
