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

137
LINES

< > BotCompany Repo | #1002185 // Slack Slurper

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

Libraryless. Click here for Pure Java version (3530L/25K/78K).

1  
!752
2  
3  
static boolean clearingAllowed = false;
4  
5  
static int maxMultiMulti = 10;
6  
static int autoSlurpDelay = 20; // seconds
7  
8  
static Map<S, PersistentLog<SlackMsg>> bulk = new TreeMap; // key = name (with "#")
9  
10  
static new L<S> autoSlurpChannels;
11  
12  
p {
13  
  load("autoSlurpChannels");
14  
  makeBot("Slack Slurper!");
15  
  
16  
  thread "slack auto-slurp" {
17  
    backgroundLoop();
18  
  }
19  
}
20  
21  
answer {
22  
  if "auto slurp channels" {
23  
    ret structure(autoSlurpChannels);
24  
  }
25  
  
26  
  if "auto slurp delay"
27  
    ret n(autoSlurpDelay, "second");
28  
  
29  
  if (match("please suck channel *", s, m)) {
30  
    S channelName = formatChannelName(m.unq(0));
31  
    suck(channelName);
32  
    ret "OK, msg count now: " + size(channelName);
33  
  }
34  
  
35  
  if match "please suck all channels" {
36  
    L<SlackMsg> msgs = suckAll();
37  
    int channels = countDistinct(msgs, "channelID");
38  
    ret "OK, " + n(l(msgs), "new msg") + " sucked in " + n(channels, "channel") + ".";
39  
  }
40  
  
41  
  if (!master()) ret null;
42  
  
43  
  if "please clear channel *" {
44  
    if (!clearingAllowed) ret "Clearing has been disabled to keep tag sanity";
45  
    S channelName = m.unq(0);
46  
    synchronized(main.class) {
47  
      PersistentLog log = bulk.get(formatChannelName(channelName));
48  
      if (log != null)
49  
        log.clear();
50  
    }
51  
    ret "OK, cleared " + channelName;
52  
  }
53  
  
54  
  if "please auto slurp *" {
55  
    S name = formatChannelName(m.unq(0));
56  
    synchronized(main.class) {
57  
      setAdd(autoSlurpChannels, name);
58  
      save("autoSlurpChannels");
59  
      ret "Alrighty! Now slurping: " + structure(autoSlurpChannels);
60  
    }
61  
  }
62  
  
63  
  if "nix auto slurp *" {
64  
    S name = formatChannelName(m.unq(0));
65  
    synchronized(main.class) {
66  
      autoSlurpChannels.remove(name);
67  
      save("autoSlurpChannels");
68  
      ret "Alrighty! Now slurping: " + structure(autoSlurpChannels);
69  
    }
70  
  }
71  
}
72  
73  
static synchronized int size(S channelName) {
74  
  ret getLog(channelName).size();
75  
}
76  
77  
static synchronized PersistentLog<SlackMsg> getLog(S channelName) {
78  
  PersistentLog log = bulk.get(formatChannelName(channelName));
79  
  if (log == null)
80  
    bulk.put(channelName, log = new PersistentLog(parseChannelName(channelName) + ".msgs"));
81  
  ret log;
82  
}
83  
84  
static synchronized L<SlackMsg> suck(S channelName) {
85  
  ret suck(channelName, slackGetChannelIDs(devChannelToken()).get(channelName));
86  
}
87  
88  
static synchronized L<SlackMsg> suck(S channelName, S channelID) {
89  
  //print("Sucking: " + channelName + "/" + channelID);
90  
  PersistentLog<SlackMsg> log = getLog(channelName);
91  
  new L<SlackMsg> newMsgs;
92  
  for (int i = 0; i < maxMultiMulti; i++) {
93  
    S oldest = null;
94  
    if (!empty(log)) oldest = last(log).ts;
95  
    print("MultiMultiSlurp " + i);
96  
    L<SlackMsg> msgs = multiSlurp(channelName, devChannelToken(), oldest, null);
97  
    log.addAll(msgs);
98  
    if (empty(msgs))
99  
      break;
100  
    else {
101  
      print("  got " + l(msgs));
102  
      newMsgs.addAll(msgs);
103  
    }
104  
  }
105  
  ret newMsgs;
106  
}
107  
108  
static synchronized L<SlackMsg> suckAll() {
109  
  new L<SlackMsg> newMsgs;
110  
  Map<S, S> channels = slackGetChannelIDs(devChannelToken());
111  
  for (S channelName : keys(channels)) pcall {
112  
    S channelID = channels.get(channelName);
113  
    newMsgs.addAll(suck(channelName, channelID));
114  
  }
115  
  ret newMsgs;
116  
}
117  
118  
static void backgroundLoop() {
119  
  while (licensed()) {
120  
    L<S> channels;
121  
    synchronized(main.class) {
122  
      channels = cloneList(autoSlurpChannels);
123  
    }
124  
    
125  
    pcall {
126  
      //print("Slurping: " + structure(autoSlurpChannels));
127  
      for (S channel : autoSlurpChannels)
128  
        answer(format("please suck channel *", channel));
129  
    }
130  
    
131  
    sleepSeconds(autoSlurpDelay);
132  
  }
133  
}
134  
135  
static S html(S uri, Map<S, S> params) {
136  
  ret serveTextFile(uri, "talkingbots.msgs");
137  
}

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: #1002185
Snippet name: Slack Slurper
Eternal ID of this version: #1002185/1
Text MD5: 2b49eca26c4d4c99480cead91a4f4f04
Transpilation MD5: ec99f831e40272e67b3c276e5237b941
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-25 16:04:08
Source code size: 3754 bytes / 137 lines
Pitched / IR pitched: No / No
Views / Downloads: 763 / 2940
Referenced in: [show references]