// OneBot — Group Notice Board. Owner chat channel (default 7): subject|message // Scopes: Groups. Config: bot_id, token, group, channel (optional). string NOTECARD = "OneBot config"; string apiBase = "https://getonebot.com"; integer botId; string apiToken = ""; string groupUuid = ""; integer noticeChannel = 7; integer configLine; key configQuery; integer configReady; integer listenHandle; 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; } 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 == "channel") { noticeChannel = (integer)v; } } 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 == "" || groupUuid == "") { llOwnerSay("OneBot: config needs bot_id, token, and group."); return; } configReady = TRUE; if (listenHandle) { llListenRemove(listenHandle); } listenHandle = llListen(noticeChannel, "", llGetOwner(), ""); llOwnerSay("Notice board listening on /" + (string)noticeChannel + " — format: subject|message"); } 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); } listen(integer chan, string name, key id, string msg) { if (!configReady || id != llGetOwner()) { return; } msg = llStringTrim(msg, STRING_TRIM); integer bar = llSubStringIndex(msg, "|"); if (bar < 1) { llOwnerSay("Use: subject|message"); return; } string subj = llStringTrim(llGetSubString(msg, 0, bar - 1), STRING_TRIM); string bodyMsg = llStringTrim(llGetSubString(msg, bar + 1, -1), STRING_TRIM); if (bodyMsg == "") { llOwnerSay("Message cannot be empty."); return; } string body = "group|" + groupUuid + "|subject|" + subj + "|message|" + bodyMsg; llHTTPRequest(apiUrl("group/notice?format=text"), apiHeaders("POST"), body); } http_response(key id, integer status, list meta, string body) { if (status != 200 || llSubStringIndex(body, "error:") == 0) { if (llSubStringIndex(body, "error:") == 0) { llOwnerSay(llGetSubString(body, 6, -1)); } else { llOwnerSay("OneBot HTTP " + (string)status); } return; } llOwnerSay("Group notice queued."); } changed(integer c) { if (c & (CHANGED_INVENTORY | CHANGED_OWNER)) { llResetScript(); } } }