import websocket import json import threading import time ########## infos ########## channel=input('channel:\n>>') trip=input('passwd:\n>>') nick=input('nick:\n>>') if trip=='': pass else: nick=nick+'#'+trip on_message = [] #msgs from ur join to now online_users = [] #online since u joined on_join = [] on_leave= [] on_whisper = [] ########## ########## ########## DEFS ########## def _info(): while True: result = json.loads(ws.recv()) #get stuff if result["cmd"] == "chat" and not result["nick"] == nick: #if otheres send shit for handler in list(on_message): handler(result["text"], result["nick"]) print(result) elif result["cmd"] == "onlineAdd": online_users.append(result["nick"]) for handler in list(on_join): handler(result["nick"]) print(result) elif result["cmd"] == "onlineRemove": online_users.remove(result["nick"]) for handler in list(on_leave): handler(result["nick"]) print(result) elif result["cmd"] == "onlineSet": for nick in result["nicks"]: online_users.append(nick) print(result) elif result["cmd"]=="info" and result["type"]=="whisper": for handler in list(on_whisper): handler(result["text"],result["from"],result) print(result) #def send_to(target, msg): # _send({"cmd": "whisper", "nick": target, "text": msg}) def _send(pkg): encoded=json.dumps(pkg) ws.send(encoded) def _stay_in_chat(): while ws.connected: _send({"cmd": "ping"}) time.sleep(60) def _send_msg(msg): _send({"cmd": "chat", "text": msg}) ########## ########## ########## CONNECT ########## ws = websocket.create_connection('wss://hack.chat/chat-ws') _send({"cmd": "join", "channel": channel, "nick": nick}) threading.Thread(target=_stay_in_chat).start() #def _daemon(): # daemon_thread=threading.Thread(target=info) # daemon_thread.start() daemon_thread=threading.Thread(target=_info);daemon_thread.start()