// OneBot — Group Joiner Kiosk (touch invite + hover status). Notecard "OneBot config". // Scopes: Groups, Status. string NOTECARD = "OneBot config"; string apiBase = "https://getonebot.com"; integer botId; string apiToken = ""; string groupUuid = ""; integer cooldownSec = 300; float statusPollSec = 60.0; integer configLine; key configQuery; integer configReady; integer botOnline; list cooldownKeys; list cooldownUntil; list apiHeaders(string method) { list h = [ HTTP_METHOD, method, HTTP_CUSTOM_HEADER, "Authorization", "Bearer " + apiToken, HTTP_CUSTOM_HEADER, "X-SecondLife-Owner-Key", (string)llGetOwner() ]; if (method == "POST") { h += [HTTP_MIMETYPE, "text/plain"]; } return h; } string apiUrl(string suffix) { return apiBase + "/api/v1/bot/" + (string)botId + "/" + suffix; } integer cooldownActive(key avatar) { integer i; integer n = llGetListLength(cooldownKeys); for (i = 0; i < n; i++) { if (llList2Key(cooldownKeys, i) == avatar) { if (llGetUnixTime() < llList2Integer(cooldownUntil, i)) { return TRUE; } cooldownKeys = llDeleteSubList(cooldownKeys, i, i); cooldownUntil = llDeleteSubList(cooldownUntil, i, i); return FALSE; } } return FALSE; } setCooldown(key avatar) { cooldownKeys += [avatar]; cooldownUntil += [llGetUnixTime() + cooldownSec]; while (llGetListLength(cooldownKeys) > 24) { cooldownKeys = llDeleteSubList(cooldownKeys, 0, 0); cooldownUntil = llDeleteSubList(cooldownUntil, 0, 0); } } applyConfigLine(string line) { line = llStringTrim(line, STRING_TRIM); if (line == "" || llSubStringIndex(line, "//") == 0) { return; } integer eq = llSubStringIndex(line, "="); if (eq < 1) { return; } string k = llToLower(llStringTrim(llGetSubString(line, 0, eq - 1), STRING_TRIM)); string v = llStringTrim(llGetSubString(line, eq + 1, -1), STRING_TRIM); if (k == "bot_id") { botId = (integer)v; } else if (k == "token") { apiToken = v; } else if (k == "api_base") { apiBase = v; } else if (k == "group") { groupUuid = v; } else if (k == "cooldown") { cooldownSec = (integer)v; } } updateHover() { string t = "OneBot Group"; if (botOnline) { t += "\nBot online — touch to join"; llSetColor(<0.2, 0.75, 0.35>, ALL_SIDES); } else { t += "\nBot offline — try again soon"; llSetColor(<0.55, 0.15, 0.15>, ALL_SIDES); } llSetText(t, <1.0, 1.0, 1.0>, 1.0); } beginLoadConfig() { configReady = FALSE; if (llGetInventoryType(NOTECARD) != INVENTORY_NOTECARD) { llOwnerSay("OneBot: missing '" + NOTECARD + "' notecard."); return; } botId = 0; apiToken = ""; groupUuid = ""; configLine = 0; configQuery = llGetNotecardLine(NOTECARD, configLine); } finishConfig() { if (botId < 1 || apiToken == "" || groupUuid == "") { llOwnerSay("OneBot: config needs bot_id, token, and group."); return; } configReady = TRUE; llSetTimerEvent(statusPollSec); llHTTPRequest(apiUrl("status"), apiHeaders("GET"), ""); updateHover(); } default { state_entry() { beginLoadConfig(); } on_rez(integer p) { llResetScript(); } dataserver(key q, string data) { if (q != configQuery) { return; } if (data == EOF) { finishConfig(); return; } applyConfigLine(data); configLine++; configQuery = llGetNotecardLine(NOTECARD, configLine); } timer() { if (!configReady) { return; } llHTTPRequest(apiUrl("status"), apiHeaders("GET"), ""); } touch_start(integer n) { if (!configReady) { return; } if (!botOnline) { llRegionSayTo(llDetectedKey(0), 0, "The bot is offline right now."); return; } key who = llDetectedKey(0); if (cooldownActive(who)) { llRegionSayTo(who, 0, "Please wait before another invite."); return; } string body = "group|" + groupUuid + "|avatar|" + (string)who; llHTTPRequest(apiUrl("group/invite?format=text"), apiHeaders("POST"), body); setCooldown(who); llRegionSayTo(who, 0, "Group invite sent via OneBot."); } http_response(key id, integer status, list meta, string body) { if (!configReady) { return; } if (status != 200) { llOwnerSay("OneBot status/invite HTTP " + (string)status); return; } if (llSubStringIndex(body, "error:") == 0) { llOwnerSay(llGetSubString(body, 6, -1)); return; } if (llSubStringIndex(body, "online") >= 0) { botOnline = (llSubStringIndex(body, "\"online\":true") >= 0); updateHover(); } } changed(integer c) { if (c & (CHANGED_INVENTORY | CHANGED_OWNER)) { llResetScript(); } } }