Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

48
LINES

< > BotCompany Repo | #1030351 // GazelleTalkToBot - web client for a Gazelle.rocks bot

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4233L/28K).

1  
sclass GazelleTalkToBot {
2  
  long botID; // Gazelle post ID
3  
  S cookie = "test_" + aGlobalID();
4  
  int n; // number of messages loaded
5  
  double serverTimeout = 10, networkTimeout = 10;
6  
  new L<Msg> msgs;
7  
  
8  
  *(long *botID) {}
9  
10  
  swappable void onNewDialog() {}
11  
  swappable void onNewMsg(Msg msg) {}
12  
  
13  
  void grab {
14  
    S json = loadPageWithTimeoutAndParamsSilently(serverTimeout+networkTimeout, "https://gazelle.rocks/incremental",
15  
      +cookie,
16  
      _botConfig := "codePost=" + botID,
17  
      json := 1,
18  
      a := zeroToNull(n),
19  
      timeout := iceil(serverTimeout));
20  
    
21  
    if (empty(json)) ret; // just timed out
22  
    Map map = decodeJSONMap(json);
23  
    onResult(map);
24  
  }
25  
  
26  
  swappable void onResult(Map map) {
27  
    if (map.containsKey("n")) n = toInt(map.get("n"));
28  
    if (isTrue(map.get("newDialog"))) onNewDialog();
29  
    fOr (Map m : (L<Map>) map.get("msgs")) {
30  
      new Msg msg;
31  
      msg.fromUser = isTrue(m.get("fromUser"));
32  
      msg.text = (S) m.get("text");
33  
      msg.time = toLong(m.get("time"));
34  
      msgs.add(msg);
35  
      print("Got msg: " + msg);
36  
      pcall { onNewMsg(msg); }
37  
    }
38  
  }
39  
  
40  
  // send a message to bot
41  
  void send(S message) {
42  
    S response = loadPageWithTimeoutAndParamsSilently(networkTimeout, "https://gazelle.rocks/msg",
43  
      +cookie,
44  
      _botConfig := "codePost=" + botID,
45  
      +message);
46  
    if (!startsWith(response, "OK")) print("Server said: " + response);
47  
  }
48  
}

Author comment

Began life as a copy of #1027721

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030351
Snippet name: GazelleTalkToBot - web client for a Gazelle.rocks bot
Eternal ID of this version: #1030351/9
Text MD5: fdfea49ae20acf02351d522a87d0eaa6
Transpilation MD5: 2c69f2d0e82a3515fe776ee4ef28d80c
Author: stefan
Category: javax / gazelle.rocks
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-05 22:34:43
Source code size: 1459 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 166 / 407
Version history: 8 change(s)
Referenced in: [show references]