Readme

Automatically answers issues from NationStates using AI sentiment analysis, for free!

Requires:

  • Huggingface token (HF_TOKEN) for the sentiment analysis of options. Generate a read-only token at Huggingface after making an account to use.
  • Email address (EMAIL) for user agent. Required as per NationStates ToS
  • Autologin token (AUTOLOGIN) for logging into NationStates. See NationStates API docs for more info.
  • Nation name (NATION_ID). Put your nation name, the one you use when you login to your account.

Selects the option with the highest positivity score. Feel free to adapt the algorithm to your own tastes.

See the Republic of Positivum for an example of this val in action.

Runs every 6 hrs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const issue_regex =
/<ISSUE id="(\d+)">\s*<TITLE>(.*?)<\/TITLE>\s*<TEXT>(.*?)<\/TEXT>\s*<AUTHOR>(.*?)<\/AUTHOR>\s*(?:<EDITOR>(.*?)<\/EDITOR>\s*)?(?:<PIC1>(.*?)<\/PIC1>\s*)?(?:<PIC2>(.*?)<\/PIC2>\s*)?(?:<OPTION id="(\d+)">(.*?)<\/OPTION>\s*)*<\/ISSUE>/;
const option_regex = /<OPTION id="(\d+)">(.*?)<\/OPTION>\s*/g;
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function query(data) {
const response = await fetch(
"https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment-latest",
{
headers: { Authorization: `Bearer ${Deno.env.get("HF_TOKEN")}` },
method: "POST",
body: JSON.stringify(data),
},
);
let result = await response.json();
if (result.error !== undefined) {
await sleep(20000);
result = await query(data);
}
return result;
}
export default async function(interval: Interval) {
const httpHeaders = {
"User-Agent": `${Deno.env.get("EMAIL")} AI Issue Answering`,
"X-Autologin": Deno.env.get("AUTOLOGIN"),
};
const myHeaders = new Headers(httpHeaders);
console.log("Starting...");
let response = await fetch(
`https://www.nationstates.net/cgi-bin/api.cgi?nation=${Deno.env.get("NATION_ID")}&q=issues`,
{
headers: myHeaders,
method: "GET",
},
);
myHeaders.set("X-Pin", response.headers.get("X-Pin"));
myHeaders.delete("X-Autologin");
let result = await response.text();
console.log(result);
let issue = result.match(issue_regex);
console.log(issue);
if (issue === null) {
console.log("No unanswered issues!");
return;
}
let issue_id = issue[1];
let options = [...issue[0].matchAll(option_regex)];
console.log(options);
let selected_option = "0";
let selected_option_positivity = 0;
// Process options and wait for all sentiment analysis to complete
const optionPromises = options.map(async (option) => {
let option_id = option[1];
let option_text = option[2];
let sentiment = await query({ "inputs": option_text });
console.log(sentiment);
let positivity = sentiment[0].find(entry => entry.label === "positive").score;
if (positivity > selected_option_positivity) {
selected_option = option_id;
selected_option_positivity = positivity;
}
});
await Promise.all(optionPromises);
let execute_issue = await fetch(
`https://www.nationstates.net/cgi-bin/api.cgi?nation=${
Deno.env.get("NATION_ID")
}&c=issue&issue=${issue_id}&option=${selected_option}`,
{
headers: myHeaders,
method: "GET",
},
);
result = await execute_issue.text();
console.log(result);
console.log("Finished");
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 26, 2024