def readChat(): global initTime, activeChat, user_id, websocket, chat_socket activeChat = [] msgLocalID = 0 goodbye = False announce_users = {} session = requests.Session() activeChat = [] websocket = yield from websockets.connect(endpoint) content = [channel, user_id, authkey] yield from messages.sendMsg(websocket, content, is_auth=True) while True: time_pre_recv = datetime.now().strftime("%M") result = yield from websocket.recv() schedule.registerWebsocket(websocket) announce.registerWebsocket(websocket) if result == None: continue try: result = json.loads(result) except TypeError as e: continue if "event" in result: # Otherwise it crashes when type = response event = result["event"] if "username" in result["data"]: result_user = result["data"]["username"] elif "user_name" in result["data"]: result_user = result["data"]["user_name"] cur_time = int(datetime.now().strftime("%M")) if event == "UserJoin": yield from announce.userJoin(result, result_user) if event == "UserLeave": yield from announce.userLeave(result, result_user) elif event == "ChatMessage": msg = result["data"] msg_id = msg["id"] user_roles = msg["user_roles"] user_name = msg["user_name"] user_id = msg["user_id"] user_msg = msg["message"]["message"] meta = msg["message"]["meta"] print("User:\t\t", user_name, "-", user_id) msg_text = "" if len(user_msg) > 1: # There's an emoticon in there for section in user_msg: if section["type"] == "text" and section["data"] != "": msg_text += section["data"] elif section["type"] == "emoticon": # Emoticon msg_text += section["text"] elif section["type"] == "link": # Link/URL msg_text += section["text"] else: # Updated form /me handling - to be released Oct 18-19 by Beam if "meta" in user_msg[0]: # /me message if "me" in user_msg[0]["meta"]: msg_text += user_msg[0]["message"] elif user_msg[0]["type"] == "text" and user_msg[0]["data"] != "": msg_text += user_msg[0]["data"] print("Message:\t", msg_text, end="\n\n") if user_name not in activeChat: activeChat.append(user_name) response, goodbye = commands.prepCMD(msg, msgLocalID, websocket, user_name, user_roles, user_id) if goodbye: # If goodbye is set to true, bot is supposed to turn off yield from messages.sendMsg(websocket, response) # Send the message yield from messages.sendMsg(websocket, "See ya later!") # Send goodbye msg yield from messages.close(websocket) print("Bot quit", str(datetime.now().strftime("%H.%M.%S"))) quit() if response == None or response == "": # Make sure response isn't nothing continue else: # ---------------------------------------------------------- # Send the message # ---------------------------------------------------------- ret_msg = yield from messages.sendMsg(websocket, response) ret_msg = json.loads(ret_msg) # Convert response to JSON print("ret_msg:\t", ret_msg) print("Response:\t", ret_msg["data"]) if "error" in ret_msg: if ret_msg["error"] != None: print("Error:\t", ret_msg["error"]) print("Code:\t", ret_msg["id"])
def readChat(): global initTime, activeChat activeChat = [] msgLocalID = 0 session = requests.Session() if os.path.exists("data/blacklist.p"): msgs_acted = pickle.load(open("data/blacklist.p", "rb")) else: msgs_acted = [] pickle.dump(msgs_acted, open("data/blacklist.p", "wb")) activeChat = [] if os.path.exists("data/bannedUsers.p"): bannedUsers = pickle.load(open("data/bannedUsers.p", "rb")) else: bannedUsers = [] pickle.dump(bannedUsers, open("data/bannedUsers.p", "wb")) websocket = yield from websockets.connect(endpoint) packet = {"type": "method", "method": "auth", "arguments": [channel, user_id, authkey], "id": 0} response = yield from websocket.send(json.dumps(packet)) while True: timeCur = datetime.now().strftime("%S") result = yield from websocket.recv() if result != None: result = json.loads(result) next print("result:\t", result, "\n") if "event" in result: if result["event"] == "ChatMessage": msg = result["data"] msgID = msg["id"] userName = msg["user_name"] if userName not in activeChat: activeChat.append(userName) response, goodbye = commands.prepCMD(msg, bannedUsers, msgLocalID, msgs_acted) if goodbye: # If goodbye is set to true, bot is supposed to turn off yield from websocket.send(json.dumps(response)) # Send the message yield from websocket.close() quit() if response != None: # Make sure response isn't nothing # ---------------------------------------------------------- # Send the message # ---------------------------------------------------------- # Create the packet packet = {"type": "method", "method": "msg", "arguments": [response], "id": msgLocalID} msgLocalID += 1 # Increment the msg number variable yield from websocket.send(json.dumps(packet)) # Send the message ret_msg = yield from websocket.recv() # Get the response ret_msg = json.loads(ret_msg) # Convert response to JSON print("ret_msg:\t", ret_msg) if msgID not in msgs_acted: # Don't add duplicates msgs_acted.append(msgID) # Make sure we don't act on messages again # Dump the list of msgs_acted into the blacklist.p pickle file so we don't act on those # messages again. f = open("data/.blist_temp.p", "wb") pickle.dump(msgs_acted, f) f.flush() os.fsync(f.fileno()) f.close() os.rename("data/.blist_temp.p", "data/blacklist.p")