Yo Mama Bot

I’m not saying that I came up with the idea, and the jokes were most likely stolen from a random website on the internets, but if I were to have created the Yo Mama sparkbot this is probably the rough script I would have started with. She’s not published for what should be obvious reason, but you can add her to a spark room by adding yomama@sparkbot.io.

Main Spark Bot Script


<?php

$auth_token = "<auth token>";

include('./include/api.php');
$data = "?";
$catagories = array("#misc","#crosseyed","#anime","#pokemon",
                    "#obama","#dirty","#math","#stupid","#crazy",
                    "#short","#lazy","#fat","#old","#poor","#tall",
                    "#chatty","#harrypotter","#bald","#videogames",
                    "#music","#hairy","#skinny","#got","#ugly","#scifi",
                    "#drwho","#politics","#white","#geek");

function get_burn($select_catagory) {
        $query = "SELECT catagory,joke FROM jokes WHERE catagory LIKE '".strtolower($select_catagory)."' ORDER BY random() LIMIT 1;";

        $dbconn = pg_connect("host=127.0.0.1 port=5432 dbname=yomama user=yomama")
            or die('Could not connect: ' . pg_last_error());

        $result = pg_query($query) or die('Query failed: ' . pg_last_error());
        pg_close($dbconn);

        return @pg_fetch_array($result, $i, PGSQL_ASSOC);
}

$json_data = json_decode(strip_tags(file_get_contents("php://input")),true);

$room_id  = $json_data['data']['roomId'];

if ( $json_data['resource'] == "messages" ) {

    $message_id = $json_data['data']['id'];

    $spark_response = sendtoSpark("get","/v1/messages/".$message_id,$data);
    $message = $spark_response->text;

    if ( $spark_response->personEmail == "yomama@sparkbot.io" ) {
    //If you talk to the sparkbot in a 1:1 room and the sparkbot replies, it then replies to its replyi, so if the message came ferom the bot, just kill the script
        die();
    }

    //help settings
    if ( preg_match('/\/help/',$message) ) {
        $spark_reply  = "yomama@sparkbot.io has over 1000 'Yo Mama' jokes ready for you to hurle at co-workers.  Collect them all.\n";
        $spark_reply  = "Things to Try:\n";
        $spark_reply .= "\t@yomama@sparkbot.io @person #catagory\n";
        $spark_reply .= "\t@yomama@sparkbot.io @person\n";
        $spark_reply .= "\t@yomama@sparkbot.io #catagory\n";
        $spark_reply .= "\t@yomama@sparkbot.io\n\n";
        $spark_reply .= "/catagories = ".implode(" ",$catagories)."\n\n";
        $spark_reply .= "To add to other rooms or a 1:1 just for fun add 'yomama@sparkbot.io' in the search bar";

    } elseif ( preg_match('/\/catagories/',$message) ) {
        $spark_reply .= "/catagories = ".implode(" ",$catagories)."\n\n";

    } else {

        if ( preg_match('/^YoMama/', $message ) ) {
            //@mention exists strip yomama
            $message = preg_replace('/YoMama/','',$message);

            if ( preg_match('/#/',$message) ){
            // # exists set catagory and strip catagory from string
                $catagory = preg_replace('/^.* (#.*)$/','${1}',$message);
                $message = preg_replace('/^(.*) #.*$/','${1}',$message);

                if  ( ! in_array($catagory,$catagories) ) {
                    $display_name  = sendtoSpark("get","/v1/people/","?email=".$spark_response->personEmail);
                    $spark_reply  = $display_name->items[0]->displayName."'s mama's kid is so dumb they can't even use 'Yo Mama' catagories right.\n\n";
                    $spark_reply .= "Next time look it up before you make a fool of yourself\n";
                    $spark_reply .= "/catagories = ".implode(" ",$catagories);
                    goto SPARKREPLY;
                }
            } else {
            // Set Catagory to anything
                $catagory = "%";
            }

            if ( preg_match('/[a-z]/',$message)) {
            // If user exists
                $name = $message."'s";
            } else {
            // else set to Yo
                $name = "Yo";
            }

        } else {
            if ( preg_match('/#/',$message) ){
                $name = "Yo";
                $catagory = preg_replace('/^(#.*)$/','${1}',$message);

                if  ( ! in_array($catagory,$catagories) ) {
                    $display_name  = sendtoSpark("get","/v1/people/","?email=".$spark_response->personEmail);
                    $spark_reply  = $display_name->items[0]->displayName."'s mama's kid is so dumb they can't even use 'Yo Mama' catagories right.\n\n";
                    $spark_reply .= "Next time look it up before you make a fool of yourself\n";
                    $spark_reply .= "/catagories = ".implode(" ",$catagories);
                    goto SPARKREPLY;
                }
            } else {
                $name = "Yo";
                $catagory = "%";
            }
        }

        $burn = get_burn($catagory);

        $spark_reply = $name." ".$burn['joke']."  ".$burn['catagory'];
    }

} elseif ( $json_data['resource'] == "memberships" ) {

    $burn = get_burn('%');

    $spark_reply = "For the record: Everyone in here's  ".$burn['joke']."  ".$burn['catagory'];
}
SPARKREPLY:

$data = array(  "text"=>$spark_reply
                //,"toPersonId"=>""
                //,"toPersonEmail"=>""
                ,"roomId"=>$room_id
                //,"markdown"=>""
                //,"files"=>""
                );
$spark_response = sendtoSpark("post","/v1/messages/",$data);

?>

Include Script for API Calls


<?php

  //$debug = true;

function sendtoSpark($method,$uri,$data) {

    global $auth_token;
    $api_url = "https://api.ciscospark.com";

    switch ($method) {
        case "get":
            $uri .= $data;
            $options = array(
                    'http' => array(
                        'header'  => "Authorization: Bearer ".$auth_token."",
                        'method'  => 'GET',
                    ),
                );
                break;
        case "post":
            $options = array(
                'http' => array(
                    'header'  => "Authorization: Bearer ".$auth_token." \r\nContent-type: application/json\r\n",
                    'method'  => 'POST',
                    'content' => json_encode($data),
                ),
            );
            break;
        case "put":
            $options = array(
                'http' => array(
                    'header'  => "Authorization: Bearer ".$auth_token." \r\nContent-type: application/json\r\n",
                    'method'  => 'PUT',
                    'content' => json_encode($data),
                ),
            );
            break;
        case "delete":
            $options = array(
                'http' => array(
                    'header'  => "Authorization: Bearer ".$auth_token." \r\nContent-type: application/json\r\n",
                    'method'  => 'DELETE',
                    'content' => json_encode($data),
                ),
            );
            break;
    }

    $context  = stream_context_create($options);
    $result = json_decode(file_get_contents($api_url.$uri, false, $context));

    return $result;
}

?>