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

63
LINES

< > BotCompany Repo | #1002190 // Slack Answerer

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

Libraryless. Click here for Pure Java version (1858L/13K/41K).

!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 O mainBot;

p {
  makeBot("Slack Answerer (about msgs)");
}

synchronized answer {
  O slurper = getBot("#1002185");
  
  if (match("what was the * msg in #*", s, m)
    || match("what was msg * in #*", s, m)) exceptionToUser {
    S what = m.unq(0);
    S channelName = "#" + m.unq(1);
    List log = cast call(slurper, "getLog", channelName);
    if (empty(log))
      ret "The channel is empty or unslurped.";
    O e;
    try {
      int i = elementIndexFromNL(log, what);
      e = get(log, i);
    } catch (Throwable x) {
      ret exceptionToUser(x);
    }
    ret e == null ? "No msg found, sorry" : structure(e); // structure of msg
  }
  
  if "how many messages in *" exceptionToUser {
    S channelName = "#" + m.unq(0);
    List log = cast call(slurper, "getLog", channelName);
    ret lstr(log);
  }
}

static int elementIndexFromNL(L l, S s) {
  new Matches m;
  
  // matches stuff like "5" or "5th" or "52nd"
  S n = get(javaTok(s), 1);
  if (isInteger(n))
    ret parseInt(n)-1;
  
  // matching on s
  
  if "first" ret 0;
  if "last" ret l(l)-1;
  if "second" ret 1;
  if "second to last" ret l(l)-2;

  throw fail("What is " + quote(s) + "?");
}

Author comment

Began life as a copy of #1002185

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: #1002190
Snippet name: Slack Answerer
Eternal ID of this version: #1002190/1
Text MD5: e749b075d44900e2c93efac044070f07
Transpilation MD5: e23dccc9631a0a344946a539217f0939
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-20 20:29:23
Source code size: 1506 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 665 / 1951
Referenced in: [show references]