Skip to content

Events API ‐ LSL example

Madpeter edited this page Jun 27, 2024 · 1 revision

HTTP example

string HTTP_url = ""; // example http://1.1.1.1:8080
string staticToken = "ThisIsMySecret";
key http_bot(string command,list values)
{
    integer unixtime = llGetUnixTime();
    string raw = command + llDumpList2String(values,"~#~");
    raw = raw + (string)unixtime;
    raw = raw + staticToken;
    // Hello1701386803examplecode
    // 1701386891examplecode
    llOwnerSay("raw="+raw);
    string hash = llSHA1String(raw);
    list args = ["commandName","unixtime","signing","args"];
    values = [command,unixtime,hash,llDumpList2String(values,"~#~")];
    string request = "";
    string addon = "";
    integer loop = 0;
    integer total = llGetListLength(values);
    while(loop < total)
    {
        string v = llList2String(values,loop);
        if(llStringLength(v) >= 1)
        {
            request = ""+request+""+addon+""+llList2String(args,loop)+"="+v+"";
             addon = "&";
        }
        loop++;
    }
    string url_out = HTTP_url+"/api/Run";
    key httpout = llHTTPRequest(url_out,
        [HTTP_BODY_MAXLENGTH,10000,HTTP_PRAGMA_NO_CACHE,TRUE,HTTP_VERBOSE_THROTTLE,FALSE,HTTP_VERIFY_CERT,FALSE,HTTP_METHOD,"POST",HTTP_MIMETYPE,"text/plain;charset=utf-8"],
        request
    );
    return httpout;
}
key helloworld = NULL_KEY;
key teleporttome = NULL_KEY;
key saycommand = NULL_KEY;
key rotcommand = NULL_KEY;
default
{
    state_entry()
    {
        helloworld = http_bot("Hello",[]);
    }
    http_response(key id,integer status,list meta,string body)
    {
        if(id == helloworld)
        {
            llOwnerSay("helloworld reply: "+body);
            vector pos = llGetPos();
            teleporttome = http_bot("Teleport",[llGetRegionName(),(integer)pos.x,(integer)pos.y,(integer)pos.z]);
        }
        else if(id == teleporttome)
        {
            llOwnerSay("Teleport reply: "+body);
            llSleep(10);
            saycommand = http_bot("Say",[0,"Hello world"]);
        }
        else if(id == saycommand)
        {
            llOwnerSay("Say reply: "+body);
            rotcommand = http_bot("RotateToFace",[llGetOwner()]);
        }
        else if(id == rotcommand)
        {
            llOwnerSay("rotate reply: "+body);
        }
    }
}