mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-25 14:21:32 +00:00
Simplify sending bot messages
This commit is contained in:
parent
4b7510e704
commit
f8ff0bcbb4
@ -1,48 +1,30 @@
|
|||||||
class MessageSender:
|
class MessageSender:
|
||||||
"""
|
|
||||||
Provides mechanisms to send messages to our frontend messaging component.
|
|
||||||
"""
|
|
||||||
def __init__(self, uplink):
|
def __init__(self, uplink):
|
||||||
self.uplink = uplink
|
self.uplink = uplink
|
||||||
self.key = 'message'
|
|
||||||
self.queue_key = 'queueMessages'
|
|
||||||
|
|
||||||
def send(self, originator, message):
|
def send(self, message, originator=None):
|
||||||
"""
|
|
||||||
Sends a message.
|
|
||||||
:param originator: name of sender to be displayed on the frontend
|
|
||||||
:param message: message to send
|
|
||||||
"""
|
|
||||||
message = {
|
message = {
|
||||||
'key': self.key,
|
'key': 'message.send',
|
||||||
'data': {
|
'message': message
|
||||||
'originator': originator,
|
|
||||||
'message': message
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if originator:
|
||||||
|
message['originator'] = originator
|
||||||
self.uplink.send_message(message)
|
self.uplink.send_message(message)
|
||||||
|
|
||||||
def queue_messages(self, originator, messages):
|
def queue_messages(self, messages, originator=None):
|
||||||
"""
|
message_queue = {
|
||||||
Queues a list of messages to be displayed in a chatbot-like manner.
|
'key': 'message.queue',
|
||||||
:param originator: name of sender to be displayed on the frontend
|
'value': []
|
||||||
: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)
|
for message in messages:
|
||||||
|
next_message = {'message': message}
|
||||||
|
if originator:
|
||||||
|
next_message['originator'] = originator
|
||||||
|
message_queue['value'].append(next_message)
|
||||||
|
self.uplink.send_message(message_queue)
|
||||||
|
|
||||||
@staticmethod
|
def set_originator(self, originator):
|
||||||
def generate_messages_from_queue(queue_message):
|
self.uplink.send_message({
|
||||||
for message in queue_message['data']['messages']:
|
'key': 'message.config',
|
||||||
yield {
|
'originator': originator
|
||||||
'key': 'message',
|
})
|
||||||
'data': message
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user