!7 !include #1013776 // Slack Bot sbool doSlack = true; !include #1008066 // SelectBot with guess_match static Set 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 botAutoHelp_commands() { L 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; } }