mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 03:21:32 +00:00
52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
from .tfw_server_connector import TFWServerUplinkConnector
|
|
|
|
|
|
class MessageSender:
|
|
"""
|
|
Provides mechanisms to send messages to our frontend messaging component.
|
|
"""
|
|
def __init__(self):
|
|
self.uplink = TFWServerUplinkConnector()
|
|
self.key = 'message'
|
|
self.queue_key = 'queueMessages'
|
|
|
|
def send(self, originator, message):
|
|
"""
|
|
Sends a message.
|
|
:param originator: name of sender to be displayed on the frontend
|
|
:param message: message to send
|
|
"""
|
|
message = {
|
|
'key': self.key,
|
|
'data': {
|
|
'originator': originator,
|
|
'message': message
|
|
}
|
|
}
|
|
self.uplink.send_message(message)
|
|
|
|
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
|
|
"""
|
|
message = {
|
|
'key': self.queue_key,
|
|
'data': {
|
|
'messages': [
|
|
{'message': message, 'originator': originator}
|
|
for message in messages
|
|
]
|
|
}
|
|
}
|
|
self.uplink.send_message(message)
|
|
|
|
@staticmethod
|
|
def generate_messages_from_queue(queue_message):
|
|
for message in queue_message['data']['messages']:
|
|
yield {
|
|
'key': 'message',
|
|
'data': message
|
|
}
|