Add message queueing capabilities to MessageSender

This commit is contained in:
Kristóf Tóth 2018-06-27 15:48:32 +02:00
parent 83dd6ae836
commit 9eacf3f79c

View File

@ -1,32 +1,46 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from datetime import datetime
from tfw.networking.event_handlers import ServerUplinkConnector from tfw.networking.event_handlers import ServerUplinkConnector
class MessageSender: class MessageSender:
""" """
Provides a mechanism to send messages to our frontend messaging component which Provides mechanisms to send messages to our frontend messaging component.
displays messages with the key "message".
""" """
def __init__(self, custom_key: str = None): def __init__(self):
self.server_connector = ServerUplinkConnector() self.server_connector = ServerUplinkConnector()
self.key = custom_key or 'message' self.key = 'message'
self.queue_key = 'queueMessages'
def send(self, originator, message): def send(self, originator, message):
""" """
Sends a message to the key specified in __init__. Sends a message.
:param originator: name of sender to be displayed on the frontend :param originator: name of sender to be displayed on the frontend
:param message: message to send :param message: message to send
""" """
data = { data = {
'originator': originator, 'originator': originator,
'timestamp': datetime.now().isoformat(),
'message': message 'message': message
} }
self.server_connector.send({ self.server_connector.send({
'key': self.key, 'key': self.key,
'data': data 'data': data
}) })
def queue_messages(self, originator, messages):
"""
Queues a list of messages to be displayed in a chatbot-like manner.
:param originator: name of sender to be displayed on the frontend
:param messages: list of messages to queue
"""
data = {
'messages': [
{'message': message, 'originator': originator}
for message in messages
]
}
self.server_connector.send({
'key': self.queue_key,
'data': data
})