My Collab Lab

mycollablab.org

Yo Mama Bot

February 15, 2017 PHP Webex

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;
}

?>

Update for Line Presence Appearance

Quote Bot


Jon Snipes
LinkedIn

My career progressed from head butcher to Collaboration CCIE. There isn’t much technically that carries over between professions, but 8 years of direct customer service experience and squeezing margin out of processes provided a solid base and direction for the rest of my career. My focus today is deep into collaboration messaging, voice and video with the expertise to develop processes and programmatic solutions to complex business problems.

Principal Architect at Cloverhound
CCIE Collaboration 51786
Cisco Webex Expert
2018-2021 Cisco Champion

Categories
  • Cloud (4)
  • Contact Center (1)
  • Development (1)
  • PHP (10)
  • Python (3)
  • Uncategorized (1)
  • Video (2)
  • Voice (12)
  • Voicemail (5)
  • Webex (7)
Recent Posts
  • Azure OAuth2.0 User Authentication December 4, 2021
  • Pass-through Gateway Routing and Dial Peer Groups December 4, 2021
  • Get Started Coding with Python: System Setup and Running Scripts December 4, 2021
  • Exporting CUCM to an Offline Local DB with Python December 4, 2021
  • Connecting to UCCX INFORMIX DB with Python on Linux December 4, 2021
Proudly powered by WordPress | Theme: Doo by ThemeVS.