User:Antony Fairport/Scripts/Pushover Example

From Second Life Wiki
Jump to navigation Jump to search

The following is a simple example script that shows how you can use LSL and Pushoverto have objects in-world send notifications to devices such as tablets and phones.

To make use of all of this you'll need an Android phone or tablet or an iPhone (or, I imagine, an iPad) and a copy of the Pushover client app installed. As well as having an account you'll also need to create an application on their site so that you have an application key (it's free if you're going to be sending yourself fewer than 7,500 notifications in a month).

Once done all that's left is to make use of llHTTPRequest to POST a message. Here's the test script I wrote, with a dressed-up function for sending a message that could be useful in any number of scripts:

//////////////////////////////////////////////////////////////////////
// Set your app and user token here. Note that you can, on the 
// PushOver site, create delivery groups so you can deliver a single
// notice to multiple users. The delivery group key is then used as
// the user key.
string APP_TOKEN = "<paste your app token here>";
string APP_USER  = "<paste your user token here>";

//////////////////////////////////////////////////////////////////////
// Constants for the PushOver parameters.
string PUSHOVER_DEVICE    = "device";
string PUSHOVER_TITLE     = "title";
string PUSHOVER_URL       = "url";
string PUSHOVER_URL_TITLE = "url_title";
string PUSHOVER_PRIORITY  = "priority";
string PUSHOVER_TIMESTAMP = "timestamp";
string PUSHOVER_SOUND     = "sound";

//////////////////////////////////////////////////////////////////////
// Send a push message via PushOver.
key SendPushMessage( string sToken, string sUser, string sMessage, list lExtra )
{
    // Main part of the data.
    string sData = "token="   + APP_TOKEN + 
                  "&user="    + APP_USER + 
                  "&message=" + llEscapeURL( sMessage );

    // Add any optional parameters.
    integer iExtra = llGetListLength( lExtra );
    integer i;
    for ( i = 0; i < iExtra; i += 2 )
    {
        sData += "&" + llList2String( lExtra, i ) + "=" + 
                 llEscapeURL( llList2String( lExtra, i + 1 ) );
    }

    // Send the push message.                      
    return llHTTPRequest( "https://api.pushover.net/1/messages.json", [
        HTTP_METHOD,   "POST",
        HTTP_MIMETYPE, "application/x-www-form-urlencoded"
    ], sData );
}

//////////////////////////////////////////////////////////////////////
// Default state.
default
{
    /////////////////////////////////////////////////////////////////
    // Respond to touch.
    touch_start( integer _ )
    {
        SendPushMessage( APP_TOKEN, APP_USER, 
            "I was just touched by " + llDetectedName( 0 ) + ".\n\n" +
            "They were " + (string) llVecDist( llDetectedPos( 0 ), llGetPos() ) + "m away.", [
            PUSHOVER_TITLE,     "Touch alert!",
            PUSHOVER_SOUND,     "bugle",
            PUSHOVER_URL,       "https://wiki.secondlife.com/wiki/User:Antony_Fairport",
            PUSHOVER_URL_TITLE, "Antony Fairport"
        ] );
    }
}

All this does is send a push notification to all my devices when someone touches the object it's inside of, telling me how far away they were at the time. Not terribly clever, but a simple example that could be built on.