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).

1  
!7
2  
3  
!include #1013776 // 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 #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: 280 / 910
Version history: 1 change(s)
Referenced in: [show references]