· 6 years ago · Nov 01, 2019, 02:06 AM
1@name GBIO
2@inputs
3@outputs
4@persist [API_KEY API_URL REQ_PREFIX]:string RES_str:string RES:table
5@persist SHOULD_REQUEST:number
6runOnChat(1)
7runOnHTTP(1)
8if(first()) {
9 API_KEY = "Put your API key here"
10 REQUEST_TIME = 10 # Seconds between requests
11 API_URL = "http://api.grammarbot.io/v2/check?"
12 REQ_PREFIX = API_URL+"api_key="+API_KEY+"&language=en-US&text="
13 SHOULD_REQUEST = 1
14 #RES = jsonDecode("{\"software\": {\"name\": \"GrammarBot\",\"version\": \"4.3-SNAPSHOT\"}}")
15 #printTable(RES["software",table])
16}
17
18if(chatClk() && SHOULD_REQUEST && httpCanRequest() && lastSpoke():isPlayer() && lastSpoke() != owner() ) {
19 httpRequest(REQ_PREFIX+httpUrlEncode(lastSaid()))
20 SHOULD_REQUEST = 0
21 timer("ShouldRequest", REQUEST_TIME*1000)
22}
23
24if(httpSuccess() && httpClk()) {
25 RES_str = httpData()
26 RES = jsonDecode(RES_str)
27 MATCHES = RES["matches", table]
28 #printTable(MATCHES[1,table])
29 CorrectionString = ""
30 for(I = 1, MATCHES:count()) {
31 MATCH = MATCHES[I,table]
32 REPLACEMENTS = MATCH["replacements", table]
33 CORRECTION = REPLACEMENTS[1,table]["value", string]
34 if(CORRECTION:length()>1) {
35 CorrectionString += REPLACEMENTS[1,table]["value", string] + "* "
36 }
37 }
38
39 #print(CorrectionString)
40 if(CorrectionString:length() > 1) {
41 concmd("say " + CorrectionString)
42 }
43}
44
45if(clk("ShouldRequest")) {
46 SHOULD_REQUEST = 1
47 print("ready")
48}