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

78
LINES

< > BotCompany Repo | #1002614 // Slack RTM Bot

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

Uses 2017K of libraries. Click here for Pure Java version (4784L/31K/105K).

1  
// TODO: stop when removed!!
2  
3  
!7
4  
5  
lib 1002612 // websocket (tyrus)
6  
7  
import javax.websocket.*;
8  
import org.glassfish.tyrus.client.*;
9  
10  
static PersistentLog<S> log;
11  
static volatile long started, triedStarting;
12  
13  
p {
14  
  log = new PersistentLog("log");
15  
  thread "Slack RTM Bot" {
16  
    background();
17  
  }
18  
}
19  
20  
synchronized answer {
21  
  if "rtm started"
22  
    ret yn(started != 0);
23  
  if "rtm log size"
24  
    ret lstr(log);
25  
    
26  
  if (master() && match("rtm last event", s))
27  
    ret structure(last(log));
28  
    
29  
  if (master() && match("rtm clear", s)) {
30  
    ret confirm("Clear RTM log?", new ClearLog);
31  
  }
32  
  
33  
  try answer status_answer(s);
34  
}
35  
36  
static class ClearLog {
37  
  S answer(S s) {
38  
    log.clear();
39  
    ret "OK, cleared RTM log.";
40  
  }
41  
}
42  
43  
static void background() ctex {
44  
  triedStarting = now();
45  
  
46  
  S token = devChannelToken();
47  
  S url = "https://slack.com/api/rtm.start";
48  
  Map postData = litmap("token", token, "no_unreads", "1");
49  
  S data = doPost(postData, url);
50  
  Map map = cast jsonDecode(data);
51  
  //printStructure(map);
52  
  
53  
  S websocketURL = getString(map, "url");
54  
  print("Websocket URL: " + websocketURL);
55  
  assertTrue(nempty(websocketURL));
56  
  
57  
  final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
58  
59  
  ClientManager client = ClientManager.createClient();
60  
  client.connectToServer(new Endpoint() {
61  
    public void onOpen(Session session, EndpointConfig config) ctex {
62  
      session.addMessageHandler(new MessageHandler.Whole<String>() {
63  
          public void onMessage(String message) {
64  
            incomingMessage(message);
65  
          }
66  
      });
67  
    }
68  
  }, cec, new URI(websocketURL));
69  
  
70  
  started = now();
71  
}
72  
73  
static synchronized void incomingMessage(S msg) {
74  
  print("RTM incoming: " + msg);
75  
  Map map = jsonDecodeMap(msg);
76  
  if (!eq(getString(map, "type"), "reconnect_url")) // ignore those
77  
    log.add(msg);
78  
}

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: #1002614
Snippet name: Slack RTM Bot
Eternal ID of this version: #1002614/2
Text MD5: 23e97a7ff20d081edb6886c32c74d436
Transpilation MD5: 3405e1b4a4d7b012bbccc97e4485e99b
Author: stefan
Category: javax / eleu
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-15 19:19:17
Source code size: 1910 bytes / 78 lines
Pitched / IR pitched: No / No
Views / Downloads: 806 / 961
Version history: 1 change(s)
Referenced in: [show references]