Uses 0K of libraries. Click here for Pure Java version (11814L/87K).
1 | !7 |
2 | |
3 | lib 1400206 // jSlack 1.4.2 (fat jar) |
4 | |
5 | import com.github.seratch.jslack.*; |
6 | import com.github.seratch.jslack.api.rtm.*; |
7 | import com.github.seratch.jslack.api.model.User; |
8 | import com.github.seratch.jslack.api.methods.request.users.*; |
9 | import com.github.seratch.jslack.api.methods.request.chat.*; |
10 | import com.github.seratch.jslack.api.methods.response.chat.*; |
11 | import com.github.seratch.jslack.api.methods.response.users.*; |
12 | |
13 | static Slack slack; |
14 | sbool slackDump = true; |
15 | |
16 | // no cmodule because of doPost / loadPage |
17 | module SlackModule > DynPrintLogAndEnabled {
|
18 | switchable S token; |
19 | S botID; |
20 | |
21 | transient new ThreadLocal<Map> msgJSON; |
22 | transient RTMClient rtm; |
23 | |
24 | start {
|
25 | if (!enabled) ret with print("Not enabled");
|
26 | |
27 | vm_cleanPrints(); // avoid Swing Unicode problem |
28 | logModuleOutput(); // good idea for chat bots |
29 | |
30 | dm_registerAs('slackModule);
|
31 | |
32 | if (nempty(token)) |
33 | ret with startSlack(); |
34 | |
35 | inputText("Please enter Slack bot token", voidfunc(S s) {
|
36 | setField(token := s); |
37 | startSlack(); |
38 | }); |
39 | } |
40 | |
41 | void startSlack enter {
|
42 | ownResource(rtm = initSlackBot()); |
43 | } |
44 | |
45 | void getMyOwnUserInfo() ctex {
|
46 | User botUser = rtm.getConnectedBotUser(); |
47 | UsersInfoResponse response = slack.methods().usersInfo( |
48 | UsersInfoRequest.builder().token(token).user(botUser.getId()) |
49 | .build()); |
50 | //print(response); |
51 | setField(botID := response.getUser().getProfile().getBotId()); |
52 | print("My bot ID: " + botID);
|
53 | } |
54 | |
55 | RTMClient initSlackBot() ctex {
|
56 | if (slack != null) fail("Slack already started");
|
57 | fixContextClassLoader(); |
58 | |
59 | slack = new Slack; |
60 | RTMClient rtm = slack.rtm(token); |
61 | |
62 | rtm.addErrorHandler(error -> {
|
63 | print("Got Slack error: " + error);
|
64 | appendToTextFile("slack-errors.txt", "\n " + getStackTrace(error) + "\n");
|
65 | print("...reconnecting to be sure.");
|
66 | thread "Reconnect to Slack" {
|
67 | fixContextClassLoader(); |
68 | pcall { rtm.disconnect(); }
|
69 | print("Disconnected (hopefully)");
|
70 | print("Waiting 10, then reconnecting.");
|
71 | sleepSeconds(10); |
72 | print("Reconnecting to Slack");
|
73 | try {
|
74 | rtm.connect(); |
75 | print("Done");
|
76 | } catch e {
|
77 | printStackTrace(e); |
78 | print("Connect failed, restarting module.");
|
79 | dm_reloadModule(); |
80 | } |
81 | } |
82 | }); |
83 | |
84 | rtm.addMessageHandler((message) -> {
|
85 | temp enter(); |
86 | Map json = jsonDecodeMap(message); |
87 | if (slackDump) pnlStruct(json); |
88 | |
89 | if (eq(json.get("type"), "hello")) pcall {
|
90 | vmBus_send('slackConnected, this);
|
91 | getMyOwnUserInfo(); |
92 | } |
93 | |
94 | if (eq(json.get("type"), "message")) pcall {
|
95 | bool bot = eq(json.get("subtype"), "bot_message");
|
96 | S channelID = cast json.get("channel");
|
97 | S ts = cast json.get("ts");
|
98 | S text = cast json.get("text");
|
99 | |
100 | vmBus_send('incomingSlackMessage,
|
101 | litmapparams( |
102 | token := shorten(token, 10), |
103 | myBotID := botID, |
104 | module := dm_moduleID(), |
105 | fromBot := bot, |
106 | msgID := ts, |
107 | +channelID, |
108 | content := text, |
109 | +json |
110 | /*+isPrivate*/)); |
111 | } |
112 | }); |
113 | |
114 | // must connect within 30 seconds after issuing wss endpoint |
115 | rtm.connect(); |
116 | print("Slack connect initiated");
|
117 | |
118 | ret rtm; |
119 | } |
120 | } |
Began life as a copy of #1022385
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1022658 |
| Snippet name: | Slack Module for Bots [dev.] |
| Eternal ID of this version: | #1022658/27 |
| Text MD5: | 355171547a1e9c8602808697e810bbd7 |
| Transpilation MD5: | 94571b964f928d928b5e814cf0c15c18 |
| Author: | stefan |
| Category: | javax / slack bots |
| Type: | JavaX source code (Dynamic Module) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2019-04-16 18:23:25 |
| Source code size: | 3497 bytes / 120 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 621 / 1647 |
| Version history: | 26 change(s) |
| Referenced in: | [show references] |