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

140
LINES

< > BotCompany Repo | #1013777 // Electron Slack Bot [dev.]

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

Download Jar. Libraryless. Click here for Pure Java version (10722L/75K).

!7

!include #1013776 // Slack Bot

sbool doSlack = true;

!include #1008066 // SelectBot with guess_match

static Set<S> botNames = asSet(splitAtSpace("time greet greetbot random thanks thanksbot bye byebot byeall"));

static O extensionModule;
static Throwable extensionModuleError;

extend SelectBot {
  S name;
}

concept ExtensionModule {
  S snippetID;
}

p {
  // DB start & migration
  //bootstrapConceptsFrom(#1007829);
  db();
  if (countConcepts(SelectBot) == 1)
    cset(uniq(SelectBot), name := "greet");
    
  reloadExtensionModule();
    
  slackSpeed = 1000;
  if (doSlack) initSlackBot();
  dediSay("Back online!");
  if (doSlack) slackBotLoop();
}

answer {
  s = trim(s);
  if (startsWith(s, "!data:", m)) ret struct(getBot($1));
  if (startsWith(s, "!plus:", m)) ret struct(keysWithValue(getBot($1).examples, true));
  if (startsWith(s, "!minus:", m)) ret struct(keysWithValue(getBot($1).examples, false));
  
  if (eq(s, "!dedicated")) ret yesno(dedicated());
  if (eq(s, "!channel")) {
    Map map = objectToMap(channel!);
    map.remove("token");
    ret structForUser(map);
  }
  
  // Train bots
  
  for (S bot : botNames) {
    if (startsWithWord(s, "!" + bot, m)) { getBot(bot).put($1, true); ret "ok"; }
    if (startsWithWord(s, "!no" + bot, m)) { getBot(bot).put($1, false); ret "ok"; }
  }
  
  // general
  
  if (eq(s, "!reload")) {
    actionsAfterPost.add(f restart);
    ret "krasser reload";
  }
  
  if (eq(s, "!source")) ret progLink();
  if (eq(s, "!help")) ret botAutoHelp();
  
  // for debugging
  if (eq(s, "!username")) ret slackUserName!;
  
  // extension module
  if (eq(s, "!ext")) ret "Yo: " + joinWithSpace(collect(list(ExtensionModule), "snippetID"));
  if (eq(s, "!ext reload")) {
    reloadExtensionModule();
    ret "OK";
  }
  if (eq(s, "!authed")) ret yn(authed());
  
  if (authed()) {
    if (startsWithWord(s, "!ext", m)) {
      cset(uniq(ExtensionModule), snippetID := fsI($1));
      reloadExtensionModule();
      ret "OK";
    }
  }

  // match on bots
  
  if (isMatch("greet", s))
    ret s + " => hi!";
  if (isMatch("time", s))
    ret s + " => It is " + hmsWithColons();
  if (isMatch("random", s))
    ret s + " => " + aGlobalID();
  if (isMatch("thanks", s))
    ret s + " => " + "You're welcome.";
  if (isMatch("bye", s) || isMatch("byeall", s))
    ret s + " => " + "Bye bye!";
    
  try answer callStaticAnswerMethod(extensionModule, s);
}

static bool isMatch(S bot, S s) {
  ret dedicated() && isMatch2(bot, s) || isMatch2(bot + "bot", s);
}

static bool isMatch2(S bot, S s) {
  ret isTrue(getBot(bot).get(s));
}

static SelectBot getBot(S name) {
  ret uniq(SelectBot, +name);
}

static L<S> botAutoHelp_commands() {
  L<S> cmds = _botAutoHelp_commands();
  for (S bot : botNames)
    addAll(cmds, "!" + bot, "!no" + bot);
  ret cmds;
}

static bool dedicated() {
  Channel c = channel!;
  ret c == null ? false : c.dedicated;
}

sbool authed() { ret eq(slackUserName!, "drjava"); }

svoid reloadExtensionModule {
  cleanUp(extensionModule);
  extensionModule = null;
  extensionModuleError = null;
  for (ExtensionModule m) {
    try {
      extensionModule = run(m.snippetID);
    } catch e {
      extensionModuleError = e;
    }
    break;
  }
}

Author comment

Began life as a copy of #1008059

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1013777
Snippet name: Electron Slack Bot [dev.]
Eternal ID of this version: #1013777/2
Text MD5: 85ee2eed5eac6d0aef720736c851e198
Transpilation MD5: fa82478e57e36fb427f958f789336b68
Author: stefan
Category: javax / slack bots
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-02-27 11:35:34
Source code size: 3371 bytes / 140 lines
Pitched / IR pitched: No / No
Views / Downloads: 269 / 885
Version history: 1 change(s)
Referenced in: [show references]