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