Spaces:
Running
Running
File size: 1,118 Bytes
fe472d7 04b984b cc20725 11eabe8 5c4b237 fe472d7 5c4b237 cc20725 04b984b 11eabe8 04b984b 11eabe8 fe472d7 11eabe8 04b984b fe472d7 04b984b 11eabe8 fe472d7 11eabe8 fe472d7 11eabe8 fe472d7 11eabe8 |
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 |
from datetime import datetime
import random
import uuid
from dataclasses import dataclass
# Shared state to store messages
messages = []
# Dictionary to store user colors
user_colors = {}
@dataclass
class Data:
user_id: str
message: str
timestamp: str
def get_user_color(user_id):
if user_id not in user_colors:
user_colors[user_id] = f"#{random.randint(0, 0xFFFFFF):06x}"
return user_colors[user_id]
def chat(input_text):
global messages
# Parse the input to get user_id and message
user_id, message = input_text.split("; ")
# Add the new message to the shared state
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
user_color = get_user_color(user_id)
messages.append(Data(user_id=user_id, message=message, timestamp=timestamp))
# Print the updated chat history
for msg in messages:
print(f"User_{msg.user_id[:4]}: {msg.message} ({msg.timestamp})")
# Example usage
if __name__ == "__main__":
while True:
input_text = input("Enter your message (format: 'id here; text here'): ")
chat(input_text)
|