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

1  
!752
2  
3  
// must be run together with sub-bot #1002185 (queries the
4  
// slack slurper's database)
5  
6  
static class SlackMsg {
7  
  S user, botName; // one of these is going to be set
8  
  S userName; // looked up if user != null
9  
  S type, text, ts;
10  
  O reactions;
11  
  S channelID;
12  
}
13  
14  
static class McMsg {
15  
  S miniChat, user, text;
16  
  long when;
17  
}
18  
19  
static PersistentMap<S, L<Tag>> tagsByTS;
20  
21  
static S editPW;
22  
23  
static class Tag {
24  
  S name;
25  
  //O addedBy; // not used yet
26  
  
27  
  *(S *name/*, O *addedBy*/) {}
28  
  *() {}
29  
}
30  
31  
p {
32  
  tagsByTS = new PersistentMap("tagsByTS.log");
33  
  editPW = loadSecretTextFileMandatory("edit-pw").trim();
34  
  makeBot("Slack Tagger Bot!!");
35  
}
36  
37  
synchronized answer {
38  
  if match "get tags for msg *" {
39  
    S msgID = m.unq(0);
40  
    L<Tag> tags = tagsByTS.get(msgID);
41  
    if (empty(tags))
42  
      ret "No tags for that msg";
43  
    else
44  
      ret structure(tags);
45  
  }
46  
}
47  
48  
static S html(S uri, Map<S, S> params) {
49  
  try answer serveTextFile(uri, "tagsByTS.log");
50  
  
51  
  L<S> keepFields = litlist("channel", "n", "q");
52  
  
53  
  S channelName = formatChannelName(or(params.get("channel"), "talkingbots"));
54  
  O slurper = getBot("#1002185");
55  
  if (slurper == null) ret "Slurper not found";
56  
  L log = cast call(slurper, "getLog", channelName);
57  
  log = filterLog(log, params.get("q")); // perform search
58  
  
59  
  boolean canEdit = master() || (!empty(editPW) && eq(editPW, params.get("edit-pw")));
60  
  print("editPW has " + l(editPW) + " chars. canEdit: " + canEdit);
61  
  
62  
  if (canEdit)
63  
    for (S key : keys(params))
64  
      if (key.startsWith("tag_")) {
65  
        S ts = dropPrefix("tag_", key);
66  
        S val = params.get(key);
67  
        new L<Tag> tags;
68  
        for (S t : splitByJavaToken(val, ","))
69  
          tags.add(new Tag(t.trim()));
70  
        tagsByTS.put(ts, tags);
71  
      }
72  
      
73  
  O mcBot = getBot("#1002433");
74  
75  
  new L data;
76  
  int n;
77  
  if (!empty(params.get("n")))
78  
    n = parseInt(params.get("n"));
79  
  else
80  
    n = max(0, l(log)-1)/100*100;
81  
  for (int i = n; i < min(n+100, l(log)); i++) {
82  
    O o = log.get(i);
83  
    S ts = getString(o, "ts");
84  
    L<Tag> tags = unnull(tagsByTS.get(ts));
85  
    L<S> names = asList(getSortedSet(tags, "name"));
86  
    S tagNames = join(", ", names);
87  
    
88  
    // Pure text fields
89  
    
90  
    Map map = htmlencode(litmap(
91  
      "Timestamp", ts,
92  
      "User", get(o, "userName"),
93  
      "Text", get(o, "text"),
94  
      "#", i+1
95  
    ));
96  
    
97  
    // Fields with actual HTML
98  
    
99  
    map.put("Tags",
100  
      htag("input", "", "type", "text", "name", "tag_" + ts, "value", tagNames));
101  
      
102  
    if (mcBot != null) pcall {
103  
      L mc = cast call(mcBot, "listMiniChat", "dc" + ts);
104  
      new L<S> miniChat;
105  
      for (O _m : mc) {
106  
        McMsg m = cast restructure(_m);
107  
        miniChat.add(htmlencode(m.user + ": " + m.text));
108  
      }
109  
      map.put("Mini-Chat", join("<br>", miniChat));
110  
    }
111  
      
112  
    data.add(map);
113  
  }
114  
  
115  
  new StringBuilder buf;
116  
  S title = "Tagging " + channelName;
117  
  buf.append(htag("title", title));
118  
  buf.append(htag("h3", title));
119  
  
120  
  // navigation
121  
  new L<S> l;
122  
  for (int i = 0; i < l(log); i += 100)
123  
    l.add(htag("a", str(i/100+1), "href", selfLink(params, keepFields, "n", str(i))));
124  
  buf.append(htag("p", "Pages: " + join("\n", l)));
125  
  
126  
  S formContents = htmlTable(data, false);
127  
  
128  
  if (canEdit)
129  
    formContents += htag("p","You are clear to edit.");
130  
  
131  
  formContents += htag("p", "Search term: " + htag("input", "", "type", "text", "name", "q", "value", params.get("q")));
132  
  //formContents += htag("p", "Edit password: " + htag("input", "", "type", "password", "name", "edit-pw", "value", params.get("edit-pw")));
133  
  formContents += htag("input", "", "type", "submit", "value", "Submit!");
134  
  formContents += hiddenFields(params, minus(keepFields, "q"));
135  
  buf.append(htag("form", formContents, "method", "POST"));
136  
  ret str(buf);
137  
}
138  
139  
static L filterLog(L log, S query) {
140  
  query = trim(query);
141  
  if (empty(query)) ret log;
142  
  
143  
  new L l;
144  
  for (O o : log) {
145  
    S text = getString(o, "text");
146  
    if (indexOfIgnoreCase(text, query) >= 0)
147  
      l.add(o);
148  
  }
149  
  ret l;
150  
}

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: 854 / 14493
Referenced in: [show references]