2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-04-06 13:21:45 +00:00
|
|
|
from tfw.networking.event_handlers import ServerUplinkConnector
|
2018-01-29 15:48:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MessageSender:
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-06-27 13:48:32 +00:00
|
|
|
Provides mechanisms to send messages to our frontend messaging component.
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-06-27 13:48:32 +00:00
|
|
|
def __init__(self):
|
2018-01-29 15:48:32 +00:00
|
|
|
self.server_connector = ServerUplinkConnector()
|
2018-06-27 13:48:32 +00:00
|
|
|
self.key = 'message'
|
|
|
|
self.queue_key = 'queueMessages'
|
2018-01-29 15:48:32 +00:00
|
|
|
|
2018-01-30 16:24:17 +00:00
|
|
|
def send(self, originator, message):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-06-27 13:48:32 +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
|
|
|
|
"""
|
2018-02-02 15:49:15 +00:00
|
|
|
data = {
|
2018-01-30 16:24:17 +00:00
|
|
|
'originator': originator,
|
|
|
|
'message': message
|
|
|
|
}
|
2018-06-04 20:16:44 +00:00
|
|
|
self.server_connector.send({
|
|
|
|
'key': self.key,
|
|
|
|
'data': data
|
|
|
|
})
|
2018-06-27 13:48:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
2018-07-13 12:09:13 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2018-07-20 08:27:58 +00:00
|
|
|
def generate_messages_from_queue(queue_message):
|
|
|
|
for message in queue_message['data']['messages']:
|
2018-07-13 12:09:13 +00:00
|
|
|
yield {
|
|
|
|
'key': 'message',
|
|
|
|
'data': message
|
|
|
|
}
|