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).

sclass GazelleTalkToBot {
  long botID; // Gazelle post ID
  S cookie = "test_" + aGlobalID();
  int n; // number of messages loaded
  double serverTimeout = 10, networkTimeout = 10;
  new L<Msg> msgs;
  
  *(long *botID) {}

  swappable void onNewDialog() {}
  swappable void onNewMsg(Msg msg) {}
  
  void grab {
    S json = loadPageWithTimeoutAndParamsSilently(serverTimeout+networkTimeout, "https://gazelle.rocks/incremental",
      +cookie,
      _botConfig := "codePost=" + botID,
      json := 1,
      a := zeroToNull(n),
      timeout := iceil(serverTimeout));
    
    if (empty(json)) ret; // just timed out
    Map map = decodeJSONMap(json);
    onResult(map);
  }
  
  swappable void onResult(Map map) {
    if (map.containsKey("n")) n = toInt(map.get("n"));
    if (isTrue(map.get("newDialog"))) onNewDialog();
    fOr (Map m : (L<Map>) map.get("msgs")) {
      new Msg msg;
      msg.fromUser = isTrue(m.get("fromUser"));
      msg.text = (S) m.get("text");
      msg.time = toLong(m.get("time"));
      msgs.add(msg);
      print("Got msg: " + msg);
      pcall { onNewMsg(msg); }
    }
  }
  
  // send a message to bot
  void send(S message) {
    S response = loadPageWithTimeoutAndParamsSilently(networkTimeout, "https://gazelle.rocks/msg",
      +cookie,
      _botConfig := "codePost=" + botID,
      +message);
    if (!startsWith(response, "OK")) print("Server said: " + response);
  }
}

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: 224 / 488
Version history: 8 change(s)
Referenced in: [show references]