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

140
LINES

< > BotCompany Repo | #1008059 // Celestia Slack Bot 2 [LIVE, multiple modules]

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

Libraryless. Click here for Pure Java version (9161L/63K/203K).

1  
!7
2  
3  
!include #1007827 // Slack Bot
4  
5  
sbool doSlack = true;
6  
7  
!include #1008066 // SelectBot with guess_match
8  
9  
static Set<S> botNames = asSet(splitAtSpace("time greet greetbot random thanks thanksbot bye byebot byeall"));
10  
11  
static O extensionModule;
12  
static Throwable extensionModuleError;
13  
14  
extend SelectBot {
15  
  S name;
16  
}
17  
18  
concept ExtensionModule {
19  
  S snippetID;
20  
}
21  
22  
p {
23  
  // DB start & migration
24  
  bootstrapConceptsFrom(#1007829);
25  
  db();
26  
  if (countConcepts(SelectBot) == 1)
27  
    cset(uniq(SelectBot), name := "greet");
28  
    
29  
  reloadExtensionModule();
30  
    
31  
  slackSpeed = 1000;
32  
  if (doSlack) initSlackBot();
33  
  dediSay("Back online!");
34  
  if (doSlack) slackBotLoop();
35  
}
36  
37  
answer {
38  
  s = trim(s);
39  
  if (startsWith(s, "!data:", m)) ret struct(getBot($1));
40  
  if (startsWith(s, "!plus:", m)) ret struct(keysWithValue(getBot($1).examples, true));
41  
  if (startsWith(s, "!minus:", m)) ret struct(keysWithValue(getBot($1).examples, false));
42  
  
43  
  if (eq(s, "!dedicated")) ret yesno(dedicated());
44  
  if (eq(s, "!channel")) {
45  
    Map map = objectToMap(channel!);
46  
    map.remove("token");
47  
    ret structForUser(map);
48  
  }
49  
  
50  
  // Train bots
51  
  
52  
  for (S bot : botNames) {
53  
    if (startsWithWord(s, "!" + bot, m)) { getBot(bot).put($1, true); ret "ok"; }
54  
    if (startsWithWord(s, "!no" + bot, m)) { getBot(bot).put($1, false); ret "ok"; }
55  
  }
56  
  
57  
  // general
58  
  
59  
  if (eq(s, "!reload")) {
60  
    actionsAfterPost.add(f restart);
61  
    ret "krasser reload";
62  
  }
63  
  
64  
  if (eq(s, "!source")) ret progLink();
65  
  if (eq(s, "!help")) ret botAutoHelp();
66  
  
67  
  // for debugging
68  
  if (eq(s, "!username")) ret slackUserName!;
69  
  
70  
  // extension module
71  
  if (eq(s, "!ext")) ret "Yo: " + joinWithSpace(collect(list(ExtensionModule), "snippetID"));
72  
  if (eq(s, "!ext reload")) {
73  
    reloadExtensionModule();
74  
    ret "OK";
75  
  }
76  
  if (eq(s, "!authed")) ret yn(authed());
77  
  
78  
  if (authed()) {
79  
    if (startsWithWord(s, "!ext", m)) {
80  
      cset(uniq(ExtensionModule), snippetID := fsI($1));
81  
      reloadExtensionModule();
82  
      ret "OK";
83  
    }
84  
  }
85  
86  
  // match on bots
87  
  
88  
  if (isMatch("greet", s))
89  
    ret s + " => hi!";
90  
  if (isMatch("time", s))
91  
    ret s + " => It is " + hmsWithColons();
92  
  if (isMatch("random", s))
93  
    ret s + " => " + aGlobalID();
94  
  if (isMatch("thanks", s))
95  
    ret s + " => " + "You're welcome.";
96  
  if (isMatch("bye", s) || isMatch("byeall", s))
97  
    ret s + " => " + "Bye bye!";
98  
    
99  
  try answer callStaticAnswerMethod(extensionModule, s);
100  
}
101  
102  
static bool isMatch(S bot, S s) {
103  
  ret dedicated() && isMatch2(bot, s) || isMatch2(bot + "bot", s);
104  
}
105  
106  
static bool isMatch2(S bot, S s) {
107  
  ret isTrue(getBot(bot).get(s));
108  
}
109  
110  
static SelectBot getBot(S name) {
111  
  ret uniq(SelectBot, +name);
112  
}
113  
114  
static L<S> botAutoHelp_commands() {
115  
  L<S> cmds = _botAutoHelp_commands();
116  
  for (S bot : botNames)
117  
    addAll(cmds, "!" + bot, "!no" + bot);
118  
  ret cmds;
119  
}
120  
121  
static bool dedicated() {
122  
  Channel c = channel!;
123  
  ret c == null ? false : c.dedicated;
124  
}
125  
126  
sbool authed() { ret eq(slackUserName!, "drjava"); }
127  
128  
svoid reloadExtensionModule {
129  
  cleanUp(extensionModule);
130  
  extensionModule = null;
131  
  extensionModuleError = null;
132  
  for (ExtensionModule m) {
133  
    try {
134  
      extensionModule = run(m.snippetID);
135  
    } catch e {
136  
      extensionModuleError = e;
137  
    }
138  
    break;
139  
  }
140  
}

Author comment

Began life as a copy of #1007829

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008059
Snippet name: Celestia Slack Bot 2 [LIVE, multiple modules]
Eternal ID of this version: #1008059/35
Text MD5: 49e3e9c43e59ef46e884811c28387b3f
Transpilation MD5: 7eca48a69b2753bb3ead8a3fe2267c7a
Author: stefan
Category: javax / slack bots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-05-06 16:34:28
Source code size: 3369 bytes / 140 lines
Pitched / IR pitched: No / No
Views / Downloads: 574 / 825
Version history: 34 change(s)
Referenced in: [show references]