import java.math.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import java.security.spec.*;
import java.security.*;
import java.lang.management.*;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.List;
import java.util.zip.*;
import java.util.*;
public class main {
// a persistent list that only grows
// Note: don't put in static initializer (programID not set yet)
static class PersistentLog extends AbstractList {
List l = new ArrayList();
File file;
PersistentLog(String fileName) {
this(getProgramFile(fileName));
}
PersistentLog(String progID, String fileName) {
this(getProgramFile(progID, fileName));
}
PersistentLog(File file) {
this.file = file;
for (String s : scanLog(file)) try {
l.add((A) unstructure(s));
} catch (Throwable __e) { __e.printStackTrace(); }
}
public int size() {
return l.size();
}
public A get(int i) {
return l.get(i);
}
public boolean add(A a) {
l.add(a);
logQuoted(file, structure(a));
return true;
}
}
static String channelID = "C0FH9PY8J"; // relp, #talkingbots
static String token;
static PersistentLog msgs;
static Map lastSeenByChannel = new TreeMap();
public static void main(String[] args) throws Exception {
if (token == null)
token = loadSecretTextFileMandatory("#1001889", "relp-slack-botstuff-token").trim();
msgs = new PersistentLog("msgs");
for (SlackMsg msg : msgs) {
String s = lastSeenByChannel.get(msg);
if (s == null || msg.ts.compareTo(s) > 0)
lastSeenByChannel.put(channelID, msg.ts);
}
while (true) {
try {
int limit = 1000;
List newMsgs = slackReadChannel(channelID, token, limit,
lastSeenByChannel.get(channelID));
msgs.addAll(newMsgs);
if (!newMsgs.isEmpty()) {
print(l(newMsgs) + " new messages in " + channelID + ".");
lastSeenByChannel.put(channelID, last(newMsgs).ts);
}
} catch (Throwable __e) { __e.printStackTrace(); }
sleepSeconds(5);
}
}
static class SlackMsg {
String user, botName; // one of these is going to be set
String userName; // looked up if user != null
String type, text, ts;
Object reactions;
String channelID;
}
static List slackReadChannel(String channelID, String token, int limit) {
return slackReadChannel(channelID, token, limit, null);
}
static List slackReadChannel(String channelID, String token, int limit, String oldest) {
String url = "https://slack.com/api/channels.history";
String postData = "token=" + urlencode(token) + "&channel=" + urlencode(channelID) + "&count=" + limit
+ (oldest != null ? "&oldest=" + urlencode(oldest) : "");
String data = doPost(postData, url);
Map map = (Map) ( jsonDecode(data));
assertTrue(map.get("ok"));
List