// OneBot — Bot Status Sign. Polls /status for hover text + color. Scopes: Status. string NOTECARD = "OneBot config"; string apiBase = "https://getonebot.com"; integer botId; string apiToken = ""; float pollSec = 60.0; integer configLine; key configQuery; integer configReady; integer botOnline = FALSE; string botRegion = ""; list apiHeaders(string method) { return [ HTTP_METHOD, method, HTTP_CUSTOM_HEADER, "Authorization", "Bearer " + apiToken, HTTP_CUSTOM_HEADER, "X-SecondLife-Owner-Key", (string)llGetOwner() ]; } string apiUrl(string suffix) { return apiBase + "/api/v1/bot/" + (string)botId + "/" + suffix; } 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 == "poll_seconds") { pollSec = (float)v; if (pollSec < 15.0) { pollSec = 15.0; } } } updateSign() { string t = "OneBot"; vector col = <0.45, 0.45, 0.45>; if (botOnline) { t += "\nOnline"; if (botRegion != "") { t += "\n" + botRegion; } col = <0.15, 0.8, 0.35>; } else { t += "\nOffline"; col = <0.7, 0.2, 0.2>; } llSetText(t, <1.0, 1.0, 1.0>, 1.0); llSetColor(col, ALL_SIDES); } beginLoadConfig() { configReady = FALSE; if (llGetInventoryType(NOTECARD) != INVENTORY_NOTECARD) { llOwnerSay("OneBot: missing '" + NOTECARD + "' notecard."); return; } configLine = 0; configQuery = llGetNotecardLine(NOTECARD, configLine); } finishConfig() { if (botId < 1 || apiToken == "") { llOwnerSay("OneBot: config needs bot_id and token."); return; } configReady = TRUE; llSetTimerEvent(pollSec); llHTTPRequest(apiUrl("status"), apiHeaders("GET"), ""); updateSign(); } parseStatus(string body) { botOnline = (llSubStringIndex(body, "\"online\":true") >= 0); integer r = llSubStringIndex(body, "\"region\":\""); if (r >= 0) { string tail = llGetSubString(body, r + 11, -1); integer end = llSubStringIndex(tail, "\""); if (end > 0) { botRegion = llGetSubString(tail, 0, end - 1); } } else { botRegion = ""; } updateSign(); } 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) { llHTTPRequest(apiUrl("status"), apiHeaders("GET"), ""); } } http_response(key id, integer status, list meta, string body) { if (!configReady) { return; } if (status != 200) { botOnline = FALSE; botRegion = ""; updateSign(); llOwnerSay("OneBot status HTTP " + (string)status); return; } if (llSubStringIndex(body, "error:") == 0) { llOwnerSay(llGetSubString(body, 6, -1)); return; } parseStatus(body); } changed(integer c) { if (c & (CHANGED_INVENTORY | CHANGED_OWNER)) { llResetScript(); } } }