Touch Group Joiner
Touch to send a group invite through your bot. Confirms in local chat and throttles repeat invites per avatar.
Scopes: Groups
// OneBot — Touch Group Joiner. Requires notecard "OneBot config" (bot_id, token, group).
// API token scopes: Groups. Generate at your bot's API tab (portal).
string NOTECARD = "OneBot config";
string apiBase = "https://getonebot.com";
integer botId;
string apiToken = "";
string groupUuid = "";
integer cooldownSec = 300;
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];
integer maxPairs = 24;
while (llGetListLength(cooldownKeys) > maxPairs)
{
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;
if (cooldownSec < 30)
{
cooldownSec = 30;
}
}
}
beginLoadConfig()
{
configReady = FALSE;
if (llGetInventoryType(NOTECARD) != INVENTORY_NOTECARD)
{
llOwnerSay("OneBot: drop a notecard named '" + NOTECARD + "' with bot_id and token.");
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 (group UUID).");
return;
}
configReady = TRUE;
}
postGroupInvite(key avatar)
{
string body = "group|" + groupUuid + "|avatar|" + (string)avatar;
llHTTPRequest(apiUrl("group/invite?format=text"), apiHeaders("POST"), body);
}
handleHttp(integer status, string body)
{
if (status != 200)
{
llOwnerSay("OneBot HTTP " + (string)status + (body != "" ? (": " + body) : ""));
return;
}
if (llSubStringIndex(body, "error:") == 0)
{
llOwnerSay(llGetSubString(body, 6, -1));
}
}
default
{
state_entry()
{
llSetObjectDesc("OneBot group joiner");
beginLoadConfig();
}
on_rez(integer start)
{
llResetScript();
}
dataserver(key query_id, string data)
{
if (query_id != configQuery)
{
return;
}
if (data == EOF)
{
finishConfig();
return;
}
applyConfigLine(data);
configLine++;
configQuery = llGetNotecardLine(NOTECARD, configLine);
}
touch_start(integer n)
{
if (!configReady)
{
llRegionSayTo(llDetectedKey(0), 0, "Group joiner is not configured yet.");
return;
}
key who = llDetectedKey(0);
if (cooldownActive(who))
{
llRegionSayTo(who, 0, "Please wait before requesting another invite.");
return;
}
postGroupInvite(who);
setCooldown(who);
llRegionSayTo(who, 0, "Invite requested via OneBot — check your Groups offers.");
}
http_response(key request_id, integer status, list metadata, string body)
{
handleHttp(status, body);
}
changed(integer change)
{
if (change & (CHANGED_INVENTORY | CHANGED_OWNER))
{
llResetScript();
}
}
}