My Collab Lab

mycollablab.org

Quote Bot

February 20, 2017 PHP Webex

Quote bot is a multi-purpose bot that provides quotes from whatever author you want to create the bot as. It relies on a DB with a table of author and quote. You can build different bots and refer to the same script by just adjusting the trigger to include the author you want. This could also be used for just random information giving etc.

Also a script that sends random quote to people in 1:1 rooms. Simply create a cronjob and let her go.

Main Spark Bot Script


<?php
$help = "";

$author = $_GET['author'];

switch ($author) {
    case "yoda":
        $auth_token = "<auth token>";
        break;
}

include('./include/api.php');
$data = "?";

$json_data = json_decode(strip_tags(file_get_contents("php://input")),true);
$data_id = $json_data['data']['id'];
$room_id  = $json_data['data']['roomId'];

if ( $json_data['resource'] = "messages" ) {
    $spark_response = sendtoSpark("get","/v1/messages/".$data_id,$data);
    $message = $spark_response->text;

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

        //help settings
        if ( preg_match('/\/help/',$message) ) {
            $help  = "\n\n".$author."@sparkbot.io has inspirational $author quotes at your fingertips.  Collect them all.\n";
            $help .= "just @mention ".$author."@sparkbot.io in a room or just say something in a 1:1 room\n";
            $help .= "To add $author to your room just add ".$author."@sparkbot.io when you are adding a user\n\n";

        }
    }
}

$query = "SELECT quote FROM quotes WHERE author = '".$author."' 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);

$quote = (@pg_fetch_array($result, $i, PGSQL_ASSOC));


$data = array(  "text"=>$quote['quote'].$help
                //,"toPersonId"=>""
                //,"toPersonEmail"=>""
                ,"roomId"=>$room_id
                //,"markdown"=>""
                //,"files"=>""
                );
$spark_response = sendtoSpark("post","/v1/messages/",$data);


?>

Scheduled Sending of Quotes


<?php

$auth_token = "<auth token>";

include('./include/api.php');

$query = "SELECT quote FROM yoda 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);

$yoda = (@pg_fetch_array($result, $i, PGSQL_ASSOC));


$spark_response = sendtoSpark("get","/v1/rooms/","?type=direct");
foreach ($spark_response->items as $room) {
    $data = array(  "text"=>$yoda['quote']
                    //,"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;
}

?>

botphpquotesspark

Yo Mama Bot

XML Phone Directory


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.