Created
January 31, 2016 19:51
-
-
Save chfoo/3761ff85f316358c898e to your computer and use it in GitHub Desktop.
Simple Beam.pro chat script
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
import json | |
import datetime | |
import tornado.gen | |
import tornado.websocket | |
import tornado.ioloop | |
@tornado.gen.coroutine | |
def main(): | |
conn = yield tornado.websocket.websocket_connect('wss://chat2-dal07.beam.pro:443') | |
conn.write_message(json.dumps({ | |
"type": "method", | |
"method": "auth", | |
"arguments": [50772], | |
"id": 1 | |
})) | |
while True: | |
msg = yield conn.read_message() | |
if msg is None: | |
break | |
doc = json.loads(msg) | |
if doc['type'] == 'event' and doc['event'] == 'ChatMessage': | |
chat_data = doc['data'] | |
message_parts = chat_data['message']['message'] | |
username = chat_data['user_name'] | |
text = '' | |
for message_part in message_parts: | |
if message_part['type'] == 'text': | |
text += message_part['data'] | |
else: | |
text += message_part['text'] | |
print(datetime.datetime.utcnow(), username, text) | |
if __name__ == '__main__': | |
tornado.ioloop.IOLoop.current().run_sync(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment