baseimage-tutorial-framework/tfw/components/frontend/message_sender.py

49 lines
1.4 KiB
Python
Raw Normal View History

2018-01-29 15:48:32 +00:00
class MessageSender:
2018-04-18 17:44:26 +00:00
"""
Provides mechanisms to send messages to our frontend messaging component.
2018-04-18 17:44:26 +00:00
"""
def __init__(self, uplink):
self.uplink = uplink
self.key = 'message'
self.queue_key = 'queueMessages'
2018-01-29 15:48:32 +00:00
def send(self, originator, message):
2018-04-18 17:44:26 +00:00
"""
Sends a message.
2018-04-18 17:44:26 +00:00
:param originator: name of sender to be displayed on the frontend
:param message: message to send
"""
2019-05-26 16:26:33 +00:00
message = {
'key': self.key,
2019-05-26 16:26:33 +00:00
'data': {
'originator': originator,
'message': message
}
}
2019-06-28 15:34:22 +00:00
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
"""
2019-05-26 16:26:33 +00:00
message = {
'key': self.queue_key,
2019-05-26 16:26:33 +00:00
'data': {
'messages': [
{'message': message, 'originator': originator}
for message in messages
]
}
}
2019-06-28 15:34:22 +00:00
self.uplink.send_message(message)
@staticmethod
2018-07-20 08:27:58 +00:00
def generate_messages_from_queue(queue_message):
for message in queue_message['data']['messages']:
yield {
'key': 'message',
'data': message
}