!7 cmodule2 FollowSkypeBot > DynPrintLogAndEnabled { switchable bool uploadEnabled; switchable double answerInterval = 3.0; transient Q uploadQ; switchable int lastMessageID; // messages appear in skype.log twice!? File logFile() { ret userDir("dev/skype-bot/skype.log"); } start-thread { uploadQ = dm_startQ(); printFileInfo(logFile()); tailFileLinewise(logFile(), 1000, voidfunc(S line) { handleLine(line); }); print("Listening to log " + stringIf(uploadEnabled, " with upload")); dm_startThread(r postAnswers); } void postAnswers { while true { sleepSeconds(answerInterval); if (!enabled) continue; S file = "/root/dev/skype-bot/msgs-to-send.json"; pcall { L msgs = cast jsonDecode(loadPageSilently("https://bea.gazelle.rocks/beaHTML/127493?markSent=1")); if (nempty(msgs)) { print("Waiting for file to disappear: " + file); while (fileExists(file)) sleepSeconds(1); saveTextFile(file, jsonEncode(msgs)); print("Sent msgs to bot."); } } } } void processWholeLog { for (S line : linesFromFile(logFile())) handleLine(line); } void handleLine(S line) pcall { //if (!enabled) ret; new Matches m; if (startsWith(line, "Message: ", m)) { //print(m.rest()); Map map = jsonDecodeMap(m.rest()); int id = toInt(map.get("id")); if (id == lastMessageID) ret with print("Message seen twice: " + id); setField(lastMessageID := id); //pnl(map); Map resource = cast mapGet(map, "resource"); //pnl(resource); S displayName = cast mapGet(resource, "imdisplayname"); S content = cast mapGet(resource, "content"); if (nempty(content)) { S cleaned = cleanSkypeMsg(content); print("Cleaned: " + cleaned); if (uploadEnabled) uploadQ.add(r { S info = "Skype"; if (nempty(displayName)) info += " (User " + displayName + ")"; gazelleBEA_uploadInput(cleaned, info, eq(content, cleaned) ? null : content); }); } } } }