function LivelyChatSupport_receive_sms()
{
    global $wpdb;
    $livelychatsupport = LivelyChatSupport_details();
    if ($livelychatsupport["twilio_auth"] != "") {
        if (isset($_GET["from_twilio"])) {
            if ($_REQUEST["AccountSid"] == $livelychatsupport["twilio_sid"]) {
                $convos_table = $wpdb->prefix . "livelychatsupport_convos";
                $body_split = explode(":", trim(urldecode($_REQUEST["Body"])));
                if (count($body_split) != 1) {
                    $mini_token = trim(strtolower($body_split[0]));
                    $body = trim($body_split[1]);
                    $convo = $wpdb->get_row("SELECT * FROM {$convos_table} WHERE mini_token = '{$mini_token}' ORDER BY updated_at DESC LIMIT 1");
                }
                if (!$convo) {
                    $convo = $wpdb->get_row("SELECT * FROM {$convos_table} ORDER BY updated_at DESC LIMIT 1");
                }
                if ($convo) {
                    if (!isset($body)) {
                        $body = trim(urldecode($_REQUEST["Body"]));
                    }
                    $return = LivelyChatSupport_create_message($convo->token, $body, 1);
                }
            }
            die("<Response></Response>");
        }
    }
}
function LivelyChatSupport_receive_sms()
{
    global $wpdb;
    $livelychatsupport = LivelyChatSupport_details();
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
    $headers .= "From: Dallas <*****@*****.**>" . "\r\n";
    wp_mail($to, $subject, $msg, $headers);
    if ($livelychatsupport["twilio_auth"] != "") {
        if (isset($_GET["from_twilio"])) {
            if ($_POST["AccountSid"] == $livelychatsupport["twilio_sid"]) {
                $convos_table = $wpdb->prefix . "livelychatsupport_convos";
                $body_split = explode(":", $_POST["Body"]);
                if (count($body_split) != 1) {
                    $mini_token = trim(strtolower($body_split[0]));
                    $body = trim($body_split[1]);
                    $convo = $wpdb->get_row("SELECT * FROM {$convos_table} WHERE mini_token = '{$mini_token}' ORDER BY updated_at DESC LIMIT 1");
                }
                if (!$convo) {
                    $convo = $wpdb->get_row("SELECT * FROM {$convos_table} ORDER BY updated_at DESC LIMIT 1");
                }
                if ($convo) {
                    if (!isset($body)) {
                        $body = trim($_POST["Body"]);
                    }
                    $return = LivelyChatSupport_create_message($convo->token, $body, 1);
                }
            }
            return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response></Response>";
        }
    }
}
function LivelyChatSupport_create_chatbox_message()
{
    $from_agent = isset($_POST["from_agent"]) ? 1 : 0;
    $not_initiated = isset($_POST["not_initiated"]) ? 1 : 0;
    LivelyChatSupport_create_message($_POST["convo_token"], filter_var($_POST["body"], FILTER_SANITIZE_STRING), $from_agent, $not_initiated);
}