// OneBot — Gift Giver. Touch to give inventory item (UUID in config "item"). Scopes: Inventory. string NOTECARD = "OneBot config"; string apiBase = "https://getonebot.com"; integer botId; string apiToken = ""; string itemUuid = ""; integer cooldownSec = 120; integer configLine; key configQuery; integer configReady; 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 == "item") { itemUuid = v; } else if (k == "cooldown") { cooldownSec = (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 == "" || itemUuid == "") { llOwnerSay("OneBot: config needs bot_id, token, and item (inventory UUID)."); return; } configReady = TRUE; llSetText("Touch for gift", <1.0, 1.0, 1.0>, 1.0); } 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); } touch_start(integer n) { if (!configReady) { return; } key who = llDetectedKey(0); if (cooldownActive(who)) { llRegionSayTo(who, 0, "Please wait before another gift."); return; } string body = "item|" + itemUuid + "|avatar|" + (string)who; llHTTPRequest(apiUrl("give?format=text"), apiHeaders("POST"), body); setCooldown(who); llRegionSayTo(who, 0, "Gift requested from OneBot."); } 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); } } } changed(integer c) { if (c & (CHANGED_INVENTORY | CHANGED_OWNER)) { llResetScript(); } } }