Quote Bot

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

?>