Last active
September 2, 2020 13:49
-
-
Save bernikr/112f59d94c4b95b7052c2b7191917c12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Aurora Chat experiments | |
// @version 0.11 | |
// @description private | |
// @author bernikr | |
// @match https://aurora.iguw.tuwien.ac.at/* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let telegramBotToken = ""; | |
let myTelegramID = ""; | |
let telegramChannel = ""; | |
var trials = 0; | |
var offset = -1; | |
startBot(); | |
function startBot(){ | |
trials++; | |
if(trials > 10){ | |
log("Aborting Bot after 10 restarts"); | |
return; | |
} | |
log("(re)starting Bot --- Trial: " + trials); | |
offset = -1; | |
let active = false; | |
// Only start sending after 3s (prevents reposting history) | |
setTimeout(()=>{active = true;},3000); | |
getUpdates(()=>{}); | |
let s = new WebSocket("wss://aurora.iguw.tuwien.ac.at/ws/main"); | |
s.onmessage = (e) => { | |
let data = JSON.parse(e.data); | |
console.log(data); | |
var msgPriv = ""; | |
var msgPub = ""; | |
if(data.type == "chat-message"){ | |
let adminstar = data.user.is_staff ? "*" : ""; | |
if(active) msgPub = "<b>"+ sanitizeHTML(data.user.nick) + "</b>[<i>" + sanitizeHTML(data.user.name) + "</i>]<b>"+ adminstar + ":</b>\n" + sanitizeHTML(data.text); | |
}else if (data.type == "whoami"){ | |
log("Connection established with username '" + sanitizeHTML(data.username) + "'"); | |
}else{ | |
msgPriv = "<i>[Unkown message-type]</i>\n" + sanitizeHTML(e.data); | |
} | |
if(msgPriv !== "") sendTelegramMessage(myTelegramID, msgPriv); | |
if(msgPub !== "") sendTelegramMessage(telegramChannel, msgPub); | |
}; | |
var loop = setInterval(()=>{ | |
// get Messages from Telegram | |
getMessages((msg)=>{ | |
if(msg.from.id == myTelegramID && msg.chat.id == myTelegramID){ | |
if(msg.text && msg.text !== ""){ | |
if(msg.text.startsWith("/")){ | |
switch(msg.text.split(" ")[0].toLowerCase()){ | |
case "/reload": | |
location.reload(); | |
break; | |
case "/setsessionid": | |
let newSessionID = msg.text.split(" ")[1]; | |
document.cookie = "sessionid=" + newSessionID + "; max-age=864000; path=/"; | |
log("New sessionid set; /reload recomended"); | |
break; | |
default: | |
log("Command '" + msg.text + "' not found"); | |
} | |
}else{ | |
let auroraMsg = { | |
type: "chat-message", | |
text: msg.text | |
}; | |
if (s.readyState === WebSocket.OPEN) s.send(JSON.stringify(auroraMsg)); | |
} | |
} | |
}else{ | |
if(msg.chat.type == "group" || msg.chat.type == "supergroup"){ | |
$.post("https://api.telegram.org/bot" + telegramBotToken + "/leaveChat", {chat_id: msg.chat.id}); | |
}else{ | |
if(msg.chat.id == 11521167){ //Philipp | |
sendTelegramMessage(msg.chat.id, "Pls stop, Philipp"); | |
}else{ | |
sendTelegramMessage(msg.chat.id, "This Bot is private"); | |
} | |
forwardTelegramMessage(myTelegramID, msg); | |
sendTelegramMessage(myTelegramID, "<i>[recieved Bot Message]</i>\n" + sanitizeHTML(JSON.stringify(msg))); | |
} | |
} | |
}); | |
// check if websocket is still open | |
if (s.readyState === undefined || s.readyState > 1) { | |
clearInterval(loop); | |
log("Websocket offline, trying to reconnect..."); | |
console.log(s); | |
setTimeout(startBot,5000); | |
} | |
},1000); | |
s.onclose = () => { | |
clearInterval(loop); | |
log("Websocket closed, trying to reconnect..."); | |
console.log(s); | |
setTimeout(startBot,5000); | |
}; | |
} | |
function sendTelegramMessage(to, text){ | |
$.post("https://api.telegram.org/bot" + telegramBotToken + "/sendMessage", {chat_id: to, text: text, parse_mode:"HTML"},(data)=>{ | |
if(data.ok){ | |
if(to !== myTelegramID){ | |
forwardTelegramMessage(myTelegramID, data.result); | |
} | |
}else console.log(data); | |
}); | |
} | |
function forwardTelegramMessage(to, msg){ | |
$.post("https://api.telegram.org/bot" + telegramBotToken + "/forwardMessage", {chat_id: to, from_chat_id: msg.chat.id, message_id: msg.message_id}); | |
} | |
function getUpdates(callback){ | |
$.get("https://api.telegram.org/bot" + telegramBotToken + "/getUpdates", {offset: offset, allowed_updates: ["message"]}, (data) =>{ | |
if(data.ok){ | |
if(data.result.length > 0){ | |
offset = data.result[data.result.length-1].update_id + 1; | |
callback(data.result); | |
} | |
}else console.log("Error in recieving messenges"); | |
}); | |
} | |
function getMessages(callback){ | |
getUpdates((results) => { | |
results.forEach((r)=>{ | |
if(r.hasOwnProperty('message')){ | |
callback(r.message); | |
} | |
}); | |
}); | |
} | |
function sanitizeHTML(input){ | |
return input.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); | |
} | |
function log(text){ | |
console.log(text); | |
sendTelegramMessage(myTelegramID, "<i>[" + text + "]</i>"); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment