Example #1
0
    public static function get_response($in_input)
    {
        if (JxBotConverse::user_input_looks_strange($in_input)) {
            return 'Your last comment looks a bit strange.';
        }
        // ** configurable?
        /* cap general server requests (safety); should be configurable
        		as people have different host specs; 300 recommended for small shared host */
        $cap_bot_ipm = JxBotConfig::option('sys_cap_bot_ipm', 300);
        if ($cap_bot_ipm > 0) {
            $stmt = JxBotDB::$db->prepare('SELECT COUNT(*) FROM log 
				WHERE stamp >= DATE_SUB(NOW(), INTERVAL 1 MINUTE)');
            $stmt->execute();
            $last_min_total = intval($stmt->fetchAll(PDO::FETCH_NUM)[0][0]);
            if ($last_min_total >= $cap_bot_ipm) {
                return 'Sorry, I\'m too busy to chat right now.  Please come back later.';
            }
        }
        /* count interaction */
        JxBotDB::$db->exec('UPDATE stats SET interactions = interactions + 1');
        /* start timer */
        $start_time = microtime(true);
        /* initalize tracking variables */
        JxBotConverse::$match_time = 0.0;
        JxBotConverse::$service_time = 0.0;
        JxBotConverse::$iq_score = 0.0;
        JxBotConverse::$category_stack = array();
        $fault = false;
        /* run the bot */
        try {
            $output = JxBotConverse::srai($in_input);
        } catch (Exception $err) {
            $output = $err->getMessage();
            $fault = true;
        }
        /* end timer */
        $end_time = microtime(true);
        //print 'IQ '.JxBotConverse::$iq_score. '<br>';
        /* log this interaction */
        if (trim($output) == '') {
            $fault = true;
        }
        if ($fault) {
            JxBotConverse::$iq_score = -1;
        }
        JxBotConverse::log($in_input, $output, $end_time - $start_time, JxBotConverse::$match_time, JxBotConverse::$service_time, JxBotConverse::$iq_score);
        /* return the bot response */
        if ($fault) {
            return 'I do apologise.  I seem to be experiencing a positronic malfunction.';
        }
        return $output;
    }