-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime_chat_display.py
More file actions
49 lines (39 loc) · 1.43 KB
/
Copy pathrealtime_chat_display.py
File metadata and controls
49 lines (39 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
#~*~ coding: utf-8 ~*~
from hackmud_chat import Account
from time import sleep
# 'Login' to hackmud chat API
acct = Account(token='yourtoken')
# OR
# acct = Account(passwd='yourpasshere')
# Get the user from which you want to monitor chat
user = acct.get_user('user')
# Setup a last index for monitoring new messages
last = 0
try:
while True:
# Get the number of messages received since last
new_msgs = user.messages_since(last)
# If we received new messages
if new_msgs > 0:
# Get the time of last message
current = user.messages[-1].time
# Extract new messages since last index
for message in user.messages[-new_msgs:]:
# Format text then output to stdout.
to = ''
if message.to_user:
to = ' -> {}'.format(message.to_user)
print '{time} - {_from}{to}: {msg}'.format(time=message.time,
_from=message.from_user,
to=to,
msg=message.text)
# Update the last message time
last = current
# Avoid overloading hackmud's server
sleep(1.5)
# Reload user's history
user.update_history()
except KeyboardInterrupt:
# Gracefully handle <ctrl+c> exit
pass