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

150
LINES

< > BotCompany Repo | #1002192 // Slack Tagger

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (3035L/21K/68K).

!752

// must be run together with sub-bot #1002185 (queries the
// slack slurper's database)

static class SlackMsg {
  S user, botName; // one of these is going to be set
  S userName; // looked up if user != null
  S type, text, ts;
  O reactions;
  S channelID;
}

static class McMsg {
  S miniChat, user, text;
  long when;
}

static PersistentMap<S, L<Tag>> tagsByTS;

static S editPW;

static class Tag {
  S name;
  //O addedBy; // not used yet
  
  *(S *name/*, O *addedBy*/) {}
  *() {}
}

p {
  tagsByTS = new PersistentMap("tagsByTS.log");
  editPW = loadSecretTextFileMandatory("edit-pw").trim();
  makeBot("Slack Tagger Bot!!");
}

synchronized answer {
  if match "get tags for msg *" {
    S msgID = m.unq(0);
    L<Tag> tags = tagsByTS.get(msgID);
    if (empty(tags))
      ret "No tags for that msg";
    else
      ret structure(tags);
  }
}

static S html(S uri, Map<S, S> params) {
  try answer serveTextFile(uri, "tagsByTS.log");
  
  L<S> keepFields = litlist("channel", "n", "q");
  
  S channelName = formatChannelName(or(params.get("channel"), "talkingbots"));
  O slurper = getBot("#1002185");
  if (slurper == null) ret "Slurper not found";
  L log = cast call(slurper, "getLog", channelName);
  log = filterLog(log, params.get("q")); // perform search
  
  boolean canEdit = master() || (!empty(editPW) && eq(editPW, params.get("edit-pw")));
  print("editPW has " + l(editPW) + " chars. canEdit: " + canEdit);
  
  if (canEdit)
    for (S key : keys(params))
      if (key.startsWith("tag_")) {
        S ts = dropPrefix("tag_", key);
        S val = params.get(key);
        new L<Tag> tags;
        for (S t : splitByJavaToken(val, ","))
          tags.add(new Tag(t.trim()));
        tagsByTS.put(ts, tags);
      }
      
  O mcBot = getBot("#1002433");

  new L data;
  int n;
  if (!empty(params.get("n")))
    n = parseInt(params.get("n"));
  else
    n = max(0, l(log)-1)/100*100;
  for (int i = n; i < min(n+100, l(log)); i++) {
    O o = log.get(i);
    S ts = getString(o, "ts");
    L<Tag> tags = unnull(tagsByTS.get(ts));
    L<S> names = asList(getSortedSet(tags, "name"));
    S tagNames = join(", ", names);
    
    // Pure text fields
    
    Map map = htmlencode(litmap(
      "Timestamp", ts,
      "User", get(o, "userName"),
      "Text", get(o, "text"),
      "#", i+1
    ));
    
    // Fields with actual HTML
    
    map.put("Tags",
      htag("input", "", "type", "text", "name", "tag_" + ts, "value", tagNames));
      
    if (mcBot != null) pcall {
      L mc = cast call(mcBot, "listMiniChat", "dc" + ts);
      new L<S> miniChat;
      for (O _m : mc) {
        McMsg m = cast restructure(_m);
        miniChat.add(htmlencode(m.user + ": " + m.text));
      }
      map.put("Mini-Chat", join("<br>", miniChat));
    }
      
    data.add(map);
  }
  
  new StringBuilder buf;
  S title = "Tagging " + channelName;
  buf.append(htag("title", title));
  buf.append(htag("h3", title));
  
  // navigation
  new L<S> l;
  for (int i = 0; i < l(log); i += 100)
    l.add(htag("a", str(i/100+1), "href", selfLink(params, keepFields, "n", str(i))));
  buf.append(htag("p", "Pages: " + join("\n", l)));
  
  S formContents = htmlTable(data, false);
  
  if (canEdit)
    formContents += htag("p","You are clear to edit.");
  
  formContents += htag("p", "Search term: " + htag("input", "", "type", "text", "name", "q", "value", params.get("q")));
  //formContents += htag("p", "Edit password: " + htag("input", "", "type", "password", "name", "edit-pw", "value", params.get("edit-pw")));
  formContents += htag("input", "", "type", "submit", "value", "Submit!");
  formContents += hiddenFields(params, minus(keepFields, "q"));
  buf.append(htag("form", formContents, "method", "POST"));
  ret str(buf);
}

static L filterLog(L log, S query) {
  query = trim(query);
  if (empty(query)) ret log;
  
  new L l;
  for (O o : log) {
    S text = getString(o, "text");
    if (indexOfIgnoreCase(text, query) >= 0)
      l.add(o);
  }
  ret l;
}

Author comment

Began life as a copy of #1002190

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002192
Snippet name: Slack Tagger
Eternal ID of this version: #1002192/1
Text MD5: 5516cbcd322f6f8f0e6fb49b7026aace
Transpilation MD5: c34898c4453149e49598d987f8412891
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-25 15:28:59
Source code size: 4145 bytes / 150 lines
Pitched / IR pitched: No / No
Views / Downloads: 773 / 14390
Referenced in: [show references]