 /// https://developers.facebook.com/docs/messenger-platform/guides/quick-start
 droplet.get("messenger", messengerSecret, "*") { request in
     guard request.data["hub.mode"]?.string == "subscribe" && request.data["hub.verify_token"]?.string == messengerSecret, let challenge = request.data["hub.challenge"]?.string else {
-        throw Abort.custom(status: .badRequest, message: "Missing Messenger data!")
+        throw Abort.custom(status: .badRequest, message: "Missing Messenger verification data.")
     }
     
     return Response(status: .ok, headers: ["Content-Type": "text/plain"], body: challenge)

/// 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 = ""
    

/// This is step 5 of the following guide:
/// https://developers.facebook.com/docs/messenger-platform/guides/quick-start
droplet.post("messenger", messengerSecret, "*") { request in
    guard request.json?["object"]?.string == "page" else {
        throw Abort.custom(status: .badRequest, message: "Message not generated by a page.")
    }
    
    /// 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.
