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

// TODO: stop when removed!!

!7

lib 1002612 // websocket (tyrus)

import javax.websocket.*;
import org.glassfish.tyrus.client.*;

static PersistentLog<S> log;
static volatile long started, triedStarting;

p {
  log = new PersistentLog("log");
  thread "Slack RTM Bot" {
    background();
  }
}

synchronized answer {
  if "rtm started"
    ret yn(started != 0);
  if "rtm log size"
    ret lstr(log);
    
  if (master() && match("rtm last event", s))
    ret structure(last(log));
    
  if (master() && match("rtm clear", s)) {
    ret confirm("Clear RTM log?", new ClearLog);
  }
  
  try answer status_answer(s);
}

static class ClearLog {
  S answer(S s) {
    log.clear();
    ret "OK, cleared RTM log.";
  }
}

static void background() ctex {
  triedStarting = now();
  
  S token = devChannelToken();
  S url = "https://slack.com/api/rtm.start";
  Map postData = litmap("token", token, "no_unreads", "1");
  S data = doPost(postData, url);
  Map map = cast jsonDecode(data);
  //printStructure(map);
  
  S websocketURL = getString(map, "url");
  print("Websocket URL: " + websocketURL);
  assertTrue(nempty(websocketURL));
  
  final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();

  ClientManager client = ClientManager.createClient();
  client.connectToServer(new Endpoint() {
    public void onOpen(Session session, EndpointConfig config) ctex {
      session.addMessageHandler(new MessageHandler.Whole<String>() {
          public void onMessage(String message) {
            incomingMessage(message);
          }
      });
    }
  }, cec, new URI(websocketURL));
  
  started = now();
}

static synchronized void incomingMessage(S msg) {
  print("RTM incoming: " + msg);
  Map map = jsonDecodeMap(msg);
  if (!eq(getString(map, "type"), "reconnect_url")) // ignore those
    log.add(msg);
}

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: 805 / 960
Version history: 1 change(s)
Referenced in: [show references]