MoonBots By [Starries]
Back to API Docs

LSL Example Scripts

Ready made scripts for your Second Life objects that talk to your bot. Open one, copy it, and drop it into an object in world.

To use one, make a new script inside an object, paste the code in, and put your API key and bot name at the top. Then save it and you're done.
Chat Command Console Advanced

Drive the bot from local chat: say, im, teleport, home and status from a single script.

Advanced Needs Messaging, Movement, Status Bot must be online Premium bot
LSL
// ============================================================
//  MoonBots API Example  |  Chat Command Console
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  Lets the owner drive the bot by typing simple commands in
//  local chat on a private channel. Shows off several API
//  actions from one script:
//
//     /9 say Hello everyone      -> bot says it in local chat
//     /9 im <uuid> <message>     -> bot sends an instant message
//     /9 tp Region/128/128/25    -> bot teleports there
//     /9 home                    -> bot teleports to its home
//     /9 status                  -> prints the bot's status
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Create an API key, assign your bot, and grant the
//     permissions for the commands you will use: Messaging,
//     Movement, and Status (or "Full Access").
//  4. Paste the key into mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey  : your API key
//  - mbBotName : your bot's SL name, "Firstname Lastname"
//  - CHANNEL   : the chat channel you type commands on
//
//  REQUIREMENTS
//  - Only the owner can drive the bot (the listen is filtered
//    to the owner).
//  - Sending instant messages ("im") is a PREMIUM bot feature.
//    Local chat, teleport and status work on any bot type.
//  - The bot must be ONLINE for everything except "status".
//    Each command uses 1 credit.
// ============================================================

string  mbApiKey  = "YOUR_API_KEY";
string  mbBotName = "Firstname Lastname";
integer CHANNEL   = 9;

integer gListen;

mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

default
{
    state_entry()
    {
        // Listen only to the owner on the chosen channel.
        gListen = llListen(CHANNEL, "", llGetOwner(), "");
        llOwnerSay("Console ready. Type commands on channel /" + (string)CHANNEL);
    }

    on_rez(integer start_param)
    {
        llResetScript();
    }

    changed(integer change)
    {
        if (change & CHANGED_OWNER)
            llResetScript();
    }

    listen(integer channel, string name, key id, string message)
    {
        list    parts = llParseString2List(message, [" "], []);
        string  cmd   = llToLower(llList2String(parts, 0));

        if (cmd == "say")
        {
            string text = llDumpList2String(llList2List(parts, 1, -1), " ");
            mbApi("say_chat_channel", ["message", text]);
        }
        else if (cmd == "im")
        {
            string target = llList2String(parts, 1);
            string text   = llDumpList2String(llList2List(parts, 2, -1), " ");
            mbApi("im", ["uuid", target, "message", text]);
        }
        else if (cmd == "tp")
        {
            mbApi("teleport", ["location", llList2String(parts, 1)]);
        }
        else if (cmd == "home")
        {
            mbApi("tp_home", []);
        }
        else if (cmd == "status")
        {
            mbApi("status", []);
        }
        else
        {
            llOwnerSay("Commands: say <text> | im <uuid> <text> | tp Region/x/y/z | home | status");
        }
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        llOwnerSay("API: " + body);
    }
}
Visitor Greeter + Auto Invite Greeting

Scans for nearby avatars, sends a welcome IM and an optional group invite, once per visitor.

Intermediate Needs Messaging, Groups Bot must be online Premium bot
LSL
// ============================================================
//  MoonBots API Example  |  Visitor Greeter + Auto Invite
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  Scans for nearby avatars on a timer. The first time it sees
//  someone, your MoonBots bot sends them a welcome instant
//  message and (optionally) a group invite. Each visitor is
//  remembered so they are only greeted once per session.
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Create an API key, assign your bot, and grant the
//     "Messaging" and "Groups" permissions (or "Full Access").
//  4. Paste the key into mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey   : your API key
//  - mbBotName  : your bot's SL name, "Firstname Lastname"
//  - GREETING   : the welcome message
//  - DO_INVITE  : TRUE to also send a group invite
//  - GROUP_UUID : the group UUID (only used if DO_INVITE)
//
//  REQUIREMENTS
//  - Key permissions: Messaging (for the IM), Groups (for the
//    invite).
//  - Sending instant messages is a PREMIUM bot feature, so the
//    greeting IM requires a Premium bot. Group invites work on
//    any bot type.
//  - The bot must be ONLINE. Each API call uses 1 credit, so
//    keep SCAN_RANGE and DO_INVITE modest on busy parcels.
// ============================================================

string  mbApiKey   = "YOUR_API_KEY";
string  mbBotName  = "Firstname Lastname";

string  GREETING   = "Welcome! Thanks for visiting. Enjoy your stay.";
integer DO_INVITE  = TRUE;
string  GROUP_UUID = "00000000-0000-0000-0000-000000000000";

float   SCAN_RANGE = 10.0;   // meters, max 96.0
float   SCAN_RATE  = 30.0;   // seconds between scans

list    gGreeted;            // avatars already greeted this session

mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

default
{
    state_entry()
    {
        // arc = PI scans a full sphere around the object.
        llSensorRepeat("", NULL_KEY, AGENT, SCAN_RANGE, PI, SCAN_RATE);
    }

    sensor(integer num_detected)
    {
        integer i;
        for (i = 0; i < num_detected; i++)
        {
            key av = llDetectedKey(i);
            if (llListFindList(gGreeted, [av]) == -1)
            {
                // Remember this visitor, keeping the most recent 200.
                gGreeted = [av] + llList2List(gGreeted, 0, 198);

                mbApi("im", [
                    "uuid",    (string)av,
                    "message", GREETING
                ]);

                if (DO_INVITE)
                {
                    mbApi("group_invite", [
                        "uuid",       (string)av,
                        "group_uuid", GROUP_UUID
                    ]);
                }
            }
        }
    }

    changed(integer change)
    {
        if (change & CHANGED_OWNER)
            llResetScript();
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        // Uncomment to debug API replies:
        // llOwnerSay("API: " + body);
        if (llSubStringIndex(body, "result=OK") == -1)
            llOwnerSay("MoonBots API error: " + body);
    }
}
Group Inviter (on touch) Groups

Touch the object and the bot sends that avatar a group invitation.

Beginner Needs Groups Bot must be online
LSL
// ============================================================
//  MoonBots API Example  |  Group Inviter (on touch)
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  When someone touches this object, your MoonBots bot sends
//  them a Second Life group invitation.
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Click "Create API Key", give it a name, assign the bot
//     you want to use, and grant the "Groups" permission
//     (or "Full Access").
//  4. Copy the generated key (you only see it once) into
//     mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey   : your API key
//  - mbBotName  : your bot's SL name, "Firstname Lastname"
//  - GROUP_UUID : the UUID of the group to invite people to
//
//  REQUIREMENTS
//  - Key permission: Groups
//  - The bot must be ONLINE and a member of the group, and it
//    must have the group ability to "Invite People".
//  - Each API call uses 1 credit. Leave about 3 seconds
//    between calls to respect Second Life throttles.
// ============================================================

string mbApiKey   = "YOUR_API_KEY";
string mbBotName  = "Firstname Lastname";
string GROUP_UUID = "00000000-0000-0000-0000-000000000000";

// Sends one command to the MoonBots API. params is a flat list
// of key, value, key, value pairs.
mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

default
{
    touch_start(integer num_detected)
    {
        integer i;
        for (i = 0; i < num_detected; i++)
        {
            key av = llDetectedKey(i);
            mbApi("group_invite", [
                "uuid",       (string)av,
                "group_uuid", GROUP_UUID
            ]);
        }
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        // The API replies with "result=OK" on success, or
        // "result=FAIL" plus a "resulttext" describing the error.
        if (llSubStringIndex(body, "result=OK") != -1)
            llOwnerSay("Group invite sent.");
        else
            llOwnerSay("Group invite failed: " + body);
    }
}
Group Inviter (on rez) Groups

When the object is rezzed, the bot invites the owner to your group.

Beginner Needs Groups Bot must be online
LSL
// ============================================================
//  MoonBots API Example  |  Group Inviter (on rez)
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  As soon as this object is rezzed in world (for example a
//  "join our group" box a customer rezzes from a package),
//  your MoonBots bot invites the owner to your group.
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Create an API key, assign your bot, and grant the
//     "Groups" permission (or "Full Access").
//  4. Paste the key into mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey   : your API key
//  - mbBotName  : your bot's SL name, "Firstname Lastname"
//  - GROUP_UUID : the UUID of the group to invite people to
//
//  REQUIREMENTS
//  - Key permission: Groups
//  - The bot must be ONLINE, a member of the group, and have
//    the "Invite People" group ability.
//  - Each API call uses 1 credit.
//
//  NOTE
//  on_rez also fires when the object is attached or when you
//  log in wearing it, so if you attach this instead of rezzing
//  it, the owner will be re-invited on each login.
// ============================================================

string mbApiKey   = "YOUR_API_KEY";
string mbBotName  = "Firstname Lastname";
string GROUP_UUID = "00000000-0000-0000-0000-000000000000";

mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

inviteOwner()
{
    mbApi("group_invite", [
        "uuid",       (string)llGetOwner(),
        "group_uuid", GROUP_UUID
    ]);
    llOwnerSay("Sending your group invite, please watch for it.");
}

default
{
    on_rez(integer start_param)
    {
        // Fires once each time the object is rezzed or attached.
        inviteOwner();
    }

    changed(integer change)
    {
        // If the object changes owner, restart so the listen and
        // owner reference are refreshed for the new owner.
        if (change & CHANGED_OWNER)
            llResetScript();
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if (llSubStringIndex(body, "result=OK") != -1)
            llOwnerSay("Check your notifications for the group invite.");
        else
            llOwnerSay("Group invite failed: " + body);
    }
}
Teleport Lure Pad Movement

Touch the pad and the bot sends the visitor a teleport offer to its location.

Beginner Needs Movement Bot must be online
LSL
// ============================================================
//  MoonBots API Example  |  Teleport Lure Pad
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  When a visitor touches this pad, your MoonBots bot sends
//  them a teleport offer (a lure) with a short message. Handy
//  for events, stores, or bringing people to your location.
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Create an API key, assign your bot, and grant the
//     "Movement" permission (or "Full Access").
//  4. Paste the key into mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey  : your API key
//  - mbBotName : your bot's SL name, "Firstname Lastname"
//  - LURE_MSG  : the message shown with the teleport offer
//
//  REQUIREMENTS
//  - Key permission: Movement
//  - The bot must be ONLINE and standing where you want to
//    lure people to. Each API call uses 1 credit.
// ============================================================

string mbApiKey  = "YOUR_API_KEY";
string mbBotName = "Firstname Lastname";
string LURE_MSG  = "Touch accepted, come join me!";

mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

default
{
    touch_start(integer num_detected)
    {
        key av = llDetectedKey(0);
        mbApi("offer_teleport", [
            "uuid",    (string)av,
            "message", LURE_MSG
        ]);
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if (llSubStringIndex(body, "result=OK") != -1)
            llOwnerSay("Teleport offer sent.");
        else
            llOwnerSay("Teleport offer failed: " + body);
    }
}
Bot Status Sign Status

Floating text shows the bot ONLINE (with location) or OFFLINE. Shows how to parse an API reply.

Beginner Needs Status Works offline
LSL
// ============================================================
//  MoonBots API Example  |  Bot Status Sign
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  Every minute this object asks the API for your bot's status
//  and shows floating text above the prim: ONLINE (with the
//  bot's current location) in green, or OFFLINE in red. A good
//  example of reading and parsing an API response in LSL.
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Create an API key, assign your bot, and grant the
//     "Status" permission (or "Full Access").
//  4. Paste the key into mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey   : your API key
//  - mbBotName  : your bot's SL name, "Firstname Lastname"
//  - CHECK_EVERY: seconds between status checks
//
//  REQUIREMENTS
//  - Key permission: Status
//  - Works whether the bot is online or offline (status is one
//    of the few commands that does not require the bot to be
//    online). Each check uses 1 credit.
// ============================================================

string mbApiKey   = "YOUR_API_KEY";
string mbBotName  = "Firstname Lastname";
float  CHECK_EVERY = 60.0;

mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

// Reads one key=value field out of a MoonBots API response body.
// The body is key=value pairs separated by newlines.
string mbField(string body, string fieldName)
{
    list lines = llParseStringKeepNulls(body, ["\n"], []);
    integer i;
    integer n = llGetListLength(lines);
    for (i = 0; i < n; i++)
    {
        string line = llList2String(lines, i);
        integer eq = llSubStringIndex(line, "=");
        if (eq != -1)
        {
            if (llGetSubString(line, 0, eq - 1) == fieldName)
                return llGetSubString(line, eq + 1, -1);
        }
    }
    return "";
}

default
{
    state_entry()
    {
        llSetTimerEvent(CHECK_EVERY);
        mbApi("status", []);
    }

    timer()
    {
        mbApi("status", []);
    }

    changed(integer change)
    {
        if (change & CHANGED_OWNER)
            llResetScript();
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if (mbField(body, "result") != "OK")
        {
            llSetText("Bot status: error", <1.0, 0.5, 0.0>, 1.0);
            return;
        }

        string slname = mbField(body, "slname");
        if (mbField(body, "online") == "1")
        {
            string loc = mbField(body, "location");
            llSetText(slname + "\nONLINE\n" + loc, <0.2, 1.0, 0.2>, 1.0);
        }
        else
        {
            llSetText(slname + "\nOFFLINE", <1.0, 0.3, 0.3>, 1.0);
        }
    }
}
Product Unpacker + Group Join Vending

On touch the bot delivers an inventory item to the buyer and can invite them to your group.

Intermediate Needs Inventory, Groups Bot must be online
LSL
// ============================================================
//  MoonBots API Example  |  Product Unpacker + Group Join
//  https://moonbots.co/api/
// ============================================================
//  WHAT IT DOES
//  When a customer touches this vendor or package, your
//  MoonBots bot delivers an inventory item (or folder) from
//  the BOT's inventory to the customer, then optionally invites
//  them to your group. Great for single-script product givers
//  and unpackers.
//
//  HOW TO GET AN API KEY
//  1. Log in at https://moonbots.co
//  2. Open the API docs page and click "Manage API Keys".
//  3. Create an API key, assign your bot, and grant the
//     "Inventory" and "Groups" permissions (or "Full Access").
//  4. Paste the key into mbApiKey below.
//
//  WHAT YOU NEED TO SET
//  - mbApiKey   : your API key
//  - mbBotName  : your bot's SL name, "Firstname Lastname"
//  - ITEM_UUID  : the Inventory Item UUID of the item/folder
//                 that lives in the BOT's inventory. This must
//                 be the inventory item's UUID, not an Asset
//                 UUID.
//  - DO_INVITE  : TRUE to also send a group invite
//  - GROUP_UUID : the group UUID (only used if DO_INVITE)
//
//  REQUIREMENTS
//  - Key permissions: Inventory (to give the item), Groups (to
//    invite).
//  - The bot must be ONLINE and the item must already exist in
//    the bot's inventory. Each API call uses 1 credit.
// ============================================================

string  mbApiKey   = "YOUR_API_KEY";
string  mbBotName  = "Firstname Lastname";

string  ITEM_UUID  = "00000000-0000-0000-0000-000000000000";
integer DO_INVITE  = TRUE;
string  GROUP_UUID = "00000000-0000-0000-0000-000000000000";

mbApi(string command, list params)
{
    list query = [
        "action="  + command,
        "apikey="  + llEscapeURL(mbApiKey),
        "botname=" + llEscapeURL(mbBotName)
    ];
    integer i;
    integer n = llGetListLength(params);
    for (i = 0; i < n; i += 2)
    {
        query += [ llList2String(params, i) + "="
                 + llEscapeURL(llList2String(params, i + 1)) ];
    }
    llHTTPRequest("https://moonbots.co/api/bot.php",
        [ HTTP_METHOD, "POST",
          HTTP_MIMETYPE, "application/x-www-form-urlencoded" ],
        llDumpList2String(query, "&"));
}

default
{
    touch_start(integer num_detected)
    {
        key av = llDetectedKey(0);
        llInstantMessage(av, "Delivering your item now, please accept it.");

        mbApi("inventory_give", [
            "uuid", (string)av,
            "item", ITEM_UUID
        ]);

        if (DO_INVITE)
        {
            mbApi("group_invite", [
                "uuid",       (string)av,
                "group_uuid", GROUP_UUID
            ]);
        }
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if (llSubStringIndex(body, "result=OK") != -1)
            llOwnerSay("Delivery command accepted.");
        else
            llOwnerSay("Delivery failed: " + body);
    }
}
We use cookies
Your browser sent a Global Privacy Control signal and we have honoured it. Analytics and advertising are switched off, and we are not selling or sharing your information.

A few cookies keep you logged in and block bots, so those always run. Everything else stays off until you allow it: loading PayPal checkout, and any analytics we might add later. See our Cookie Policy.