NOTE Since 0.2.5 this could be simplified a bit since you can use the Startup LoliCode Script to set up the global variables.
IMPORTANT Since I had to implement asynchronous locks, this snippet will only be valid from OB2 version 0.1.16 onwards.
If you want to have a session token (e.g. cookie) that is shared between all bots you can use global variables. The code is commented so read it thoroughly. Let’s assume you want to refresh the token every 1000 attempts.
// Make sure only 1 bot at a time enters this section of code
// by using an asynchronous lock
ACQUIRELOCK globals
TRY
// Assume we need a new token by default
bool needNewToken = true;
// Check if 'globals' has a 'Token' property
if (((IDictionary<String, object>)globals).ContainsKey("Token"))
{
// If it does, it means Token and Counter already exist and we can use them
// Check if the counter is below 1000
if (globals.Counter < 1000)
{
// Perfect, we don't need a new token
needNewToken = false;
}
}
// If we do need a new token
if (needNewToken)
{
// We need to set the Token and Counter properties of 'globals'
// We (re)set the Counter to 0
globals.Counter = 0;
// Logic to get the token goes here
// You need to set the token to the 'globals.Token' variable
globals.Token = "dummy token";
}
// Here we know for sure the Counter exists because we passed all the logic above
// Increase the counter by 1 for this attempt
globals.Counter++;
// Rethrow any exception
CATCH
throw;
// Exit the lock that we entered at the start
FINALLY
RELEASELOCK globals
END
// Normal config goes here, you are guaranteed to have a
// globals.Token you can use at this point, which is refreshed every 1000 attempts
LOG globals.Token
If you want to force the expiration of the token (e.g. if the webpage tells you the token expired) you can simply write this anywhere in your config to force a token refresh
ACQUIRELOCK globals
TRY
globals.Counter = 1000;
FINALLY
RELEASELOCK globals
END
Hey guys, expanding on the previous one.
Here’s another example of this, maybe a bit easier
// PREPARATION STAGE
// Lock so only 1 bot enters
ACQUIRELOCK globals
TRY
// Check if global token already set or if we need to refresh it
var globalsDict = ((IDictionary<String, object>)globals);
if (!globalsDict.ContainsKey("Token") || globals.NeedsRefresh)
{
// If not set or needs refresh, grab it
await GetToken();
globals.NeedsRefresh = false; // No need to refresh anymore
}
CATCH
throw;
FINALLY
RELEASELOCK globals
END
// --- START EDITING HERE
// BODY OF YOUR CONFIG GOES HERE
// You can use the token by using the globals.Token variable!
// Remember that you can invalidate the token and make it get a new one
// at any time by simply writing the following line in your check stage
// globals.NeedsRefresh = true;
// --- STOP EDITING HERE
// GET TOKEN FUNCTION BELOW
async Task GetToken()
{
// --- START EDITING HERE
// Put the blocks to grab the token here, for example
// Http Request block
// Keycheck block to see if the token is in the page, if not then just go to BAN
// Parse block to parse the token from data.SOURCE to a variable called Token
// --- STOP EDITING HERE
globals.Token = Token;
LOG "Saved globals.Token"
}
If you also need the cookies to be saved, you can edit the script like this
// PREPARATION STAGE
// Lock so only 1 bot enters
ACQUIRELOCK globals
TRY
// Check if global token already set or if we need to refresh it
var globalsDict = ((IDictionary<String, object>)globals);
if (!globalsDict.ContainsKey("Token") || globals.NeedsRefresh)
{
// If not set or needs refresh, grab it
await GetToken();
globals.NeedsRefresh = false; // No need to refresh anymore
}
else
{
// If already set, restore the cookies
data.COOKIES = globals.Cookies;
}
CATCH
throw;
FINALLY
RELEASELOCK globals
END
// --- START EDITING HERE
// BODY OF YOUR CONFIG GOES HERE
// You can use the token by using the globals.Token variable!
// Remember that you can invalidate the token and make it get a new one
// at any time by simply writing the following line in your check stage
// globals.NeedsRefresh = true;
// --- STOP EDITING HERE
// GET TOKEN FUNCTION BELOW
async Task GetToken()
{
// --- START EDITING HERE
// Put the blocks to grab the token here, for example
// Http Request block
// Keycheck block to see if the token is in the page, if not then just go to BAN
// Parse block to parse the token from data.SOURCE to a variable called Token
// --- STOP EDITING HERE
globals.Token = Token;
LOG "Saved globals.Token"
globals.Cookies = data.COOKIES;
LOG "Saved globals.Cookies"
}