diff --git a/solvable/src/pipe_io_auxlib.py b/solvable/src/pipe_io_auxlib.py index 75c831e..a06ce06 100644 --- a/solvable/src/pipe_io_auxlib.py +++ b/solvable/src/pipe_io_auxlib.py @@ -24,7 +24,7 @@ class SignMessagePipeIOEventHandler(PipeIOEventHandlerBase): super().__init__(None, in_pipe_path, out_pipe_path, permissions) def handle_event(self, message): - return + pass def handle_pipe_event(self, message_bytes): message = loads(message_bytes) @@ -46,7 +46,7 @@ class VerifyMessagePipeIOEventHandler(PipeIOEventHandlerBase): super().__init__(None, in_pipe_path, out_pipe_path, permissions) def handle_event(self, message): - return + pass def handle_pipe_event(self, message_bytes): message = loads(message_bytes) @@ -54,6 +54,43 @@ class VerifyMessagePipeIOEventHandler(PipeIOEventHandlerBase): self.pipe_io.send_message(str(validity).lower().encode()) +class BotPipeIOEventHandler(PipeIOEventHandlerBase): + """ + Sends bot messages to the frontend. + If you assign @originator, it will be the default message sender. + When you write a line to the pipe, it will be considered as a single + message and gets appended to the queue until an empty line is received, + which triggers forwarding the messages to the TFW server. + """ + + def __init__( + self, in_pipe_path, out_pipe_path, permissions=DEFAULT_PERMISSIONS, + originator='avataobot' + ): + self.queue = [] + self.originator = originator + super().__init__(None, in_pipe_path, out_pipe_path, permissions) + + def handle_event(self, message): + pass + + def handle_pipe_event(self, message_bytes): + if message_bytes == b"": + if self.queue: + self.server_connector.send({ + 'key': 'queueMessages', + 'data': { + 'messages': self.queue + } + }) + self.queue = [] + else: + self.queue.append({ + 'originator': self.originator, + 'message': message_bytes.decode().replace('\\n', '\n') + }) + + class DeployPipeIOEventHandler(PipeIOEventHandlerBase): """ Manages deployment in the IDE. diff --git a/solvable/src/pipe_io_main.py b/solvable/src/pipe_io_main.py index f39fabd..056b152 100644 --- a/solvable/src/pipe_io_main.py +++ b/solvable/src/pipe_io_main.py @@ -7,13 +7,14 @@ from tfw.components import PipeIOEventHandler from pipe_io_auxlib import ( SignMessagePipeIOEventHandler, VerifyMessagePipeIOEventHandler, + BotPipeIOEventHandler, DeployPipeIOEventHandler, IdePipeIOEventHandler, FSMPipeIOEventHandler ) if __name__ == '__main__': - ''' + """ Creates general purpose pipes. The first parameter associates the receiving pipe with a key, which is an empty string in this case. It has a special meaning, you can @@ -23,7 +24,7 @@ if __name__ == '__main__': You can send/receive JSON messages to/from the TFW server as any user, because we gave read+write permissions, without that parameter, only the owner has access to the pipes. - ''' + """ json_pipe = PipeIOEventHandler( '', '/tmp/tfw_json_send', @@ -42,6 +43,11 @@ if __name__ == '__main__': '/tmp/tfw_verify_recv' ) + bot_pipe = BotPipeIOEventHandler( + '/tmp/tfw_bot_send', + '/tmp/tfw_bot_recv' + ) + deploy_pipe = DeployPipeIOEventHandler( '/tmp/tfw_deploy_send', '/tmp/tfw_deploy_recv',