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

!752

static boolean clearingAllowed = false;

static int maxMultiMulti = 10;
static int autoSlurpDelay = 20; // seconds

static Map<S, PersistentLog<SlackMsg>> bulk = new TreeMap; // key = name (with "#")

static new L<S> autoSlurpChannels;

p {
  load("autoSlurpChannels");
  makeBot("Slack Slurper!");
  
  thread "slack auto-slurp" {
    backgroundLoop();
  }
}

answer {
  if "auto slurp channels" {
    ret structure(autoSlurpChannels);
  }
  
  if "auto slurp delay"
    ret n(autoSlurpDelay, "second");
  
  if (match("please suck channel *", s, m)) {
    S channelName = formatChannelName(m.unq(0));
    suck(channelName);
    ret "OK, msg count now: " + size(channelName);
  }
  
  if match "please suck all channels" {
    L<SlackMsg> msgs = suckAll();
    int channels = countDistinct(msgs, "channelID");
    ret "OK, " + n(l(msgs), "new msg") + " sucked in " + n(channels, "channel") + ".";
  }
  
  if (!master()) ret null;
  
  if "please clear channel *" {
    if (!clearingAllowed) ret "Clearing has been disabled to keep tag sanity";
    S channelName = m.unq(0);
    synchronized(main.class) {
      PersistentLog log = bulk.get(formatChannelName(channelName));
      if (log != null)
        log.clear();
    }
    ret "OK, cleared " + channelName;
  }
  
  if "please auto slurp *" {
    S name = formatChannelName(m.unq(0));
    synchronized(main.class) {
      setAdd(autoSlurpChannels, name);
      save("autoSlurpChannels");
      ret "Alrighty! Now slurping: " + structure(autoSlurpChannels);
    }
  }
  
  if "nix auto slurp *" {
    S name = formatChannelName(m.unq(0));
    synchronized(main.class) {
      autoSlurpChannels.remove(name);
      save("autoSlurpChannels");
      ret "Alrighty! Now slurping: " + structure(autoSlurpChannels);
    }
  }
}

static synchronized int size(S channelName) {
  ret getLog(channelName).size();
}

static synchronized PersistentLog<SlackMsg> getLog(S channelName) {
  PersistentLog log = bulk.get(formatChannelName(channelName));
  if (log == null)
    bulk.put(channelName, log = new PersistentLog(parseChannelName(channelName) + ".msgs"));
  ret log;
}

static synchronized L<SlackMsg> suck(S channelName) {
  ret suck(channelName, slackGetChannelIDs(devChannelToken()).get(channelName));
}

static synchronized L<SlackMsg> suck(S channelName, S channelID) {
  //print("Sucking: " + channelName + "/" + channelID);
  PersistentLog<SlackMsg> log = getLog(channelName);
  new L<SlackMsg> newMsgs;
  for (int i = 0; i < maxMultiMulti; i++) {
    S oldest = null;
    if (!empty(log)) oldest = last(log).ts;
    print("MultiMultiSlurp " + i);
    L<SlackMsg> msgs = multiSlurp(channelName, devChannelToken(), oldest, null);
    log.addAll(msgs);
    if (empty(msgs))
      break;
    else {
      print("  got " + l(msgs));
      newMsgs.addAll(msgs);
    }
  }
  ret newMsgs;
}

static synchronized L<SlackMsg> suckAll() {
  new L<SlackMsg> newMsgs;
  Map<S, S> channels = slackGetChannelIDs(devChannelToken());
  for (S channelName : keys(channels)) pcall {
    S channelID = channels.get(channelName);
    newMsgs.addAll(suck(channelName, channelID));
  }
  ret newMsgs;
}

static void backgroundLoop() {
  while (licensed()) {
    L<S> channels;
    synchronized(main.class) {
      channels = cloneList(autoSlurpChannels);
    }
    
    pcall {
      //print("Slurping: " + structure(autoSlurpChannels));
      for (S channel : autoSlurpChannels)
        answer(format("please suck channel *", channel));
    }
    
    sleepSeconds(autoSlurpDelay);
  }
}

static S html(S uri, Map<S, S> params) {
  ret serveTextFile(uri, "talkingbots.msgs");
}

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: 762 / 2938
Referenced in: #1002190 - Slack Answerer
#1002192 - Slack Tagger
#1002277 - Tagging Compo Test (depends on getBot)
#1002391 - Slack Analyzer (parses slack lines)
#1003006 - getTBLog - get log of #talkingbots
#1003042 - Load Slack log & tags
#1003048 - Load Slack log & tags (include)
#3000169 - Answer for stefanreich
#3000174 - Answer for stefanreich
#3000175 - Answer for stefanreich
#3000176 - Answer for stefanreich
#3000177 - Answer for stefanreich
#3000179 - Answer for stefanreich
#3000180 - Answer for stefanreich
#3000181 - Answer for stefanreich
#3000182 - Answer for stefanreich
#3000184 - Answer for stefanreich
#3000185 - Answer for stefanreich
#3000195 - Answer for stefanreich (>> y)
#3000196 - Answer for stefanreich (>> y)
#3000197 - Answer for stefanreich (>> program data sizes)
#3000198 - Answer for stefanreich (>> program data sizes)
#3000199 - Answer for stefanreich (>> program data sizes)
#3000200 - Answer for stefanreich (>> program data sizes)
#3000201 - Answer for stefanreich (>> list all files)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000203 - Answer for stefanreich (>> timing hello bot)
#3000208 - Answer for bgrgndz (>> list sub bots)
#3000209 - Answer for bgrgndz (>> list sub bots)
#3000238 - Answer for stefanreich (>> t power bot)
#3000369 - Answer for Stefan (>> list sub bots)
#3000376 - Answer for stefanreich (>> list sub bots)
#3000377 - Answer for stefanreich (>> timing eleu?)
#3000380 - Answer for stefanreich (>> list sub bots)
#3000381 - Answer for speedforce (>> list sub bots)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)