I've been playing around with code again, and came up with this script to telegram new nations. It's very basic. There are far better apps out there (and I'd strongly recommend you use them rather than this), so I'm sharing this mostly for novelty value.
Check out these threads if you're looking for a proper app.
viewtopic.php?f=15&t=432124
viewtopic.php?f=15&t=388960
viewtopic.php?f=15&t=350586
Edit: I've updated to version 1.2. This version should keep trying to TG new nations until your PC goes to sleep.
What
Use this code (v1.2) to send recruitment telegrams to new nations.
How
You need a Client Key, and the telegram ID and Secret Key for your recruitment telegram. I'm not going to explain how to get them here. If you really want to send out recruitment TGs, you'll easily find out for yourself, and you'll also likely want to use better software!
On your PC or laptop, open a text editor, like Notepad. Copy the code (below) into the editor.
Put your own Client Key, telegram ID and Secret Key in the code (place indicated by all the asterisks).
Save the file as “TelegraphMachine.html”
Check the filename. If it saved as “TelegraphMachine.html.txt” or similar, rename it to “TelegraphMachine.html”.
When you open the file again, it should get opened in your browser by default. You should see a black page with “Wolfram & Hart” written across the top.
When you click the button, the code asks NS for a list of the 50 most recently created nations.
It then attempts to send a telegram to the first nation on the list.
Three minutes later, the next telegram gets sent out.
Then three minutes later, the next, and so on until the list of 50 nations is finished.
The code then gets a fresh list of the 50 most recently created nations, and repeats.
Possible Improvements
Add text boxes so users can input Client Key etc on the screen.
Add a text box so users can supply their own list of nations.
Add a radio button to control telegram speed. The default would be 3 minutes (the fastest rate currently allowed for recruitment TGs). The alternative would be 30 seconds (permitted for non-recruitment TGs).
Create a loop, so that when all 50 new nations have been telegrammed, the next 50 are chosen. Without sending duplicate TGs to the same nations!
Feedback
If you use this script, please drop [nation]Wolfram & Hart[/nation] a telegram. It would be interesting to know how widely this was used (if at all!). Any suggestions for improvements and enhancements are welcome.
- Code: Select all
<!DOCTYPE html>
<!-- Wolfram & Hart Telegraph Machine Script v1.2
Version | Date | Name | Comment
1.0 | 23 Dec 2017 | Wolfram & Hart | Created
1.2 | 03 Feb 2018 | Wolfram & Hart | Restructured to use Callback function with AJAX request.
| | | Loop added to keep looking for new nations.
| | | Bug fix for 1st and last nation name processing.
This code is not warranted in any way. Use at your own risk.
Use and modify this code as suits.
Make sure you comply with the NationStates API rules.
See: https://www.nationstates.net/pages/api.html
-->
<head>
<title>W&H Telegraph Machine</title>
<style>
body {background-color: black;}
div {
text-align:center;
color: white;
font-family: times;
}
</style>
</head>
<body>
<div>
<h1><span style="font-size:120%;">W</span>OLFRAM <span style="color:red; font-size:120%;">&</span> <span style="font-size:120%;">H</span>ART</h1>
<h3>Telegraph Machine</h3>
<br>
<button type="button" onclick="fMain()">Telegram New Nations</button>
<p id="err"></p>
<p id="msg"></p>
</div>
<!-- This is the JavaScript that will be run when the button is pressed -->
<script>
/* Get a list of the 50 most recently created nations.
For each nation:
If the nation accepts recruitment TGs, send one.
Move on to the next nation.
When all 50 nations are done, start again.
*/
/* *******INSERT YOUR OWN CLIENT KEY, TG ID, SECRET KEY AND REGION IN THIS SECTION****** */
var strClientKey = "abcd1234";
var strTgId = "12345678";
var strSecretKey = "123456abcdef";
var strRegion = "MyRegion";
/* ************************************************************************************* */
var intSpdTG = 180000; // Recruitment TGs can be sent every 180 seconds (note non-recruitment TGs can be sent every 30 seconds)
var intSpdAPI = 1200; // Regular API calls to NS can be sent every 1.2 seconds (actually 50 "hits" in 30 seconds, but this seems to work)
var astrNtns = []; // An array variable to store the list of new nations
var intNtn = 0; // Keep a note of which nation is being processed
var strErr = ""; // Keep a record of any HTTP errors
var strMsg = ""; // Keep a record of nations successfully processed
/* The fMain function is called when the button is pressed.
Reset displayed paragraphs to be blank, then request a list of new NS nations
*/
function fMain() {
strRegion = strRegion.replace(" ", "_");
strErr = "";
strMsg = "";
document.getElementById("err").innerHTML = strErr;
document.getElementById("msg").innerHTML = strMsg;
fGetNewNations();
}
/* The fGetNewNations function requests the list of new nations from NS */
function fGetNewNations() {
fGetXml("https://www.nationstates.net/cgi-bin/api.cgi?q=newnations", fProcessNewNations);
}
/* The fProcessNewNations function parses the list of new nations into an array
It then tries to recruit the first nation (0) on the list
*/
function fProcessNewNations(objXml) {
if (objXml.status == 200) {
astrNtns = objXml.responseXML.getElementsByTagName("NEWNATIONS")[0].childNodes[0].nodeValue.split(",");
intNtn = 0;
setTimeout(function(){ fGetCanRecruit() }, intSpdAPI);
} else {
strErr = "fGetNewNations Error " + objXml.status + ": " + objXml.responseText + "<br>" + strErr;
document.getElementById("err").innerHTML = strErr;
}
}
/* The fGetCanRecruit function checks if the nation accepts recruitment TGs
Sending the optional "from=(region_name)" parameter checks that you didn't send them a TG too recently.
*/
function fGetCanRecruit() {
/* First check that there are still nations to process */
if (intNtn >= astrNtns.length) {
fGetNewNations();
} else {
/* get the nation name from the list */
var strNtn = astrNtns[intNtn].replace(" ", "_");
var strUrl = "https://www.nationstates.net/cgi-bin/api.cgi?nation=" + strNtn + "&q=tgcanrecruit&from=" + strRegion
fGetXml(strUrl, fProcessCanRecruit);
}
}
/* The fProcessCanRecruit function:
If the response is "1", (Nation accepts recruitment TGs), send a TG
Else, try the next nation
*/
function fProcessCanRecruit(objXml) {
if (objXml.status == 200) {
var strMrkr = objXml.responseXML.getElementsByTagName("TGCANRECRUIT")[0].childNodes[0].nodeValue;
if (strMrkr == "1") {
setTimeout(function(){ fTelegram() }, intSpdAPI);
} else {
strMsg = intNtn + ": " + astrNtns[intNtn] + ": TG Blocked" + "<br>" + strMsg;
document.getElementById("msg").innerHTML = strMsg;
intNtn = intNtn + 1;
setTimeout(function(){ fGetCanRecruit() }, intSpdAPI);
}
} else {
strErr = "fGetCanRecruit Error " + objXml.status + ": " + objXml.responseText + "<br>" + strErr;
document.getElementById("err").innerHTML = strErr;
intNtn = intNtn + 1;
setTimeout(function(){ fGetCanRecruit() }, intSpdAPI);
}
}
/* The fTelegram function sends out the TG */
function fTelegram() {
var strNtn = astrNtns[intNtn].replace(" ", "_");
var strUrl = "https://www.nationstates.net/cgi-bin/api.cgi?a=sendTG&client="
+ strClientKey + "&tgid=" + strTgId + "&key=" + strSecretKey + "&to=" + strNtn;
fGetXml(strUrl, fProcessTelegram);
}
/* Log the response, then check the next nation */
function fProcessTelegram(objXml) {
if (objXml.status == 200) {
strMsg = intNtn + ": " + astrNtns[intNtn] + ": TG Sent" + "<br>" + strMsg;
document.getElementById("msg").innerHTML = strMsg;
} else {
strErr = "fTelegram Error " + objXml.status + ": " + objXml.responseText + "<br>" + strErr;
document.getElementById("err").innerHTML = strErr;
}
intNtn = intNtn + 1;
setTimeout(function(){ fGetCanRecruit() }, intSpdTG - intSpdAPI);
}
/* fGetXml needs to know the URL to get data from, and the function to run when the data is retrieved. */
function fGetXml(strUrl, objFn) {
var objXhttp = new XMLHttpRequest(); // Create an object to send the details to NS
objXhttp.onreadystatechange = function() {
if (this.readyState == 4) {
objFn(this);
}
};
objXhttp.open("GET", strUrl, true);
objXhttp.send();
}
</script>
</body>
</html>