Nabaztag hacks

Recently I bought a Nabaztag.
A Nabaztag is a hump of plastics with ears that can move, with lights, an audio device and a WiFi interface.
This bunny can do all kinds of things triggered by the Violet (maker of these rabbits) servers. With some scripting, one can let your own buddy act on other things (e.g. not selected by the people from Violet) as well. A couple of those are described below.


Changelog

2007-10-07:motion: do not use 'on_motion_detected', use 'on_event_start' as the first one will trigger multiple messages per event
all: added 'ttlive' for each message sent to prevent your Nabaztag mailbox to fill up

Before you continue

For the following two examples one need to enable the "Nabazphere".
For that, login to the homepage of your bunny. Then, in the upper right box click on "My profile". A new screen appears where you can configure all kinds of settings of how the rabbit behaves. Now click on "my preferences" and scroll down to the "Nabazphere" section. There, tick the box before "I want to receive the services and applications of the Nabazphere on my Nabaztag". Now in that box you'll see the serial number of your bunny (starts with '0013') and a "token". Write down these two values, you'll need them to be able to interface your scripts to the Nabaztag website.

Use the Nabaztag as notifier of events

In the opensource world, there are quit a few interesting projects. Two of these are Motion and Nagios.
Motion can monitor a webcam and if it "sees" something moving, it can start recording images into a file on disk for later review.
Nagios can be used to monitor all kinds of things. Mostly it is used to see if servers are up and if, for example, they do not run out of diskspace and such.

Nagios

In this example I assume that Nagios is installed under "/usr/local/nagios". This is different when installing Nagios using Debian packages.
First add two entries to "etc/misccommands.cfg". The first one is for notification of failing services, the second one for complete hosts that stop responding to pings:
define command {
        command_name    notify-by-nabaztag
        command_line    /usr/local/nagios/libexec/send_nabaztag SERIAL TOKEN "$HOSTNAME$ $SERVICEDESC$ $SERVICESTATE$"
        }

define command {
        command_name    host-notify-by-nabaztag
        command_line    /usr/local/nagios/libexec/send_nabaztag SERIAL TOKEN "$HOSTNAME$ $HOSTSTATE$ $SERVICEOUTPUT$"
        }
You see here the words "SERIAL" and "TOKEN". You must replace these with the serial and token found on the Nabaztag website (the profile page).
Then, create a new contact (in "etc/contacts.cfg"). When that contact gets notified of anything by Nagios, the bunny then will say it:
define contact{
        use                             generic-contact
        contact_name                    nabaztag
        alias                           nabaztag
        pager                           none
        service_notification_commands   notify-by-nabaztag
        host_notification_commands      host-notify-by-nabaztag
        }
And finally, add the new contact (called "nabaztag") to "contactgroups.cfg" (this is an example):
define contactgroup{
        contactgroup_name       unix-admins
        alias                   UNIX administrators
        members                 folkert,nabaztag
        }
As you may have noticed I referred to a script "/usr/local/nagios/libexec/send_nabaztag" in the "misccommands.cfg"-file. This script looks like this (don't forget to make the script executable!):
#!/usr/bin/perl

use URI::URL;

$str = URI::URL->new($ARGV[2]);
$url = 'http://api.nabaztag.com/vl/FR/api.jsp?sn='.$ARGV[0].'&token='.$ARGV[1]."&tts=$str&ttlive=300";

system("/usr/bin/wget -q -O /dev/null '$url'");
The '&ttlive=300' causes the message to be stored in your Nabaztag mailbox for only 5 minutes before it will automatically be deleted. Normally messages stay in your mailbox for 2 months. 2 months may be too long if you send many messages a day. For this to work you need to have "wget" installed as well as Perl and the 'URI::URL' Perl package.

Motion

Add to "motion.conf" (which can be stored in e.g. "/etc/motion/" (Debian) or "/usr/local/etc") the following line:
on_event_start      /usr/local/sbin/intruder_alert
Then create the "/usr/local/sbin/intruder_alert" script:
#!/bin/sh

/usr/bin/wget -O /dev/null "http://api.nabaztag.com/vl/FR/api.jsp?sn=SERIAL&token=TOKEN&tts=intruder+alert&voice=lucy22s&ttlive=300"
As you can see, this script requires "wget". Do not forget to replace "SERIAL" and "TOKEN" with the ones you found while configuring the bunny via the bunny's homepage on the Nabaztag server.
The '&ttlive=300' causes the message to be stored in your Nabaztag mailbox for only 5 minutes before it will automatically be deleted. Normally messages stay in your mailbox for 2 months. 2 months may be too long if you send many messages a day. This example script lets the bunny say "intruder alert" when motion is detected. It uses the voice "lucy22s", other voices are: graham22s, heather22k, ryan22k, aaron22s and laura22s.

Web-form - let visitors of your website leave you a note

This code is based on some code I found somewhere on the net. Can't remember where though so I cannot give proper credit.
The web-form consists of 2 pieces of code. This first one lets the user enter a text and select a voice:
<FORM ACTION="do_bunny.php" METHOD="POST">
<TABLE>
<TR><TD>Your text:<TD><TD><INPUT TYPE="TEXT" NAME="text" WIDTH=128 SIZE=32><TD><TR>
<TR><TD ALIGN=RIGHT>voice:<TD><TD><SELECT NAME="voice">
<OPTION VALUE="graham22s">Graham<OPTION>
<OPTION VALUE="lucy22s">Lucy<OPTION>
<OPTION VALUE="heather22k">Heather<OPTION>
<OPTION VALUE="ryan22k">Ryan<OPTION>
<OPTION VALUE="aaron22s">Aaron<OPTION>
<OPTION VALUE="laura22s">Laura<OPTION>
<SELECT><TD><TR>
<TR><TD><TD><TD><INPUT TYPE="SUBMIT"><TD>TR>
<TABLE>
<FORM>
As you can see when the user presses the submit-button, a script "do_bunny.php" is invoked which looks like this:
<?
function say($nbz_serial, $nbz_token, $say_this, $voice) {

    $base             = 'http://api.nabaztag.com/vl/FR/api.jsp';
    $query_string     = '';

    $params = array(
        'sn'         => $nbz_serial,
        'token'      => $nbz_token,
        'tts'        => $say_this,
        'voice'      => $voice);

    foreach ($params as $key => $value) {
        $query_string .= "$key=" . urlencode($value) . "&";
    }

    $url = "$base?$query_string";

    $results = file_get_contents($url);

    return $results;

}

if ($_POST['text'] != '')
        $text = $_POST['text'];
else if ($_GET['text'] != '')
        $text = $_GET['text'];
else
        $text = '';

if ($_POST['voice'] != '')
        $voice = $_POST['voice'];
else if ($_GET['voice'] != '')
        $voice = $_GET['voice'];
else
        $voice = 'lucy22s';

$text = strip_tags($text);
$text = trim($text);
$text = str_replace("'", "", $text);

if ($text != '')
{
        $result = say('SERIAL', 'TOKEN', $text, $voice);
        print "Your text '$text' was spoken.<BR>n";
        print "<BR>n";
        print 'P.s. the server said: '.strip_tags($result)."<BR>n";
}
else
        print 'There was nothing to say.';
?>
Again, do not forget to replace "SERIAL" and "TOKEN".

You can try the script here.

Links

If you're interested in the Nagios monitoring software or the Nagios Incident Manager, please follow this link to the Nagios website: it pays the hosting costs for vanheusden.com. Thanks!

This page (by Violet) contains the full documentation of the Nabaztag API.





feedback