From ec80fa8784344e313ea06fe51fd8fc36ec243386 Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 27 Aug 2020 20:53:09 +0300 Subject: [PATCH 1/3] change socket file options from r to rb --- greenswitch/esl.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/greenswitch/esl.py b/greenswitch/esl.py index 6e67cce..88416b7 100644 --- a/greenswitch/esl.py +++ b/greenswitch/esl.py @@ -78,7 +78,7 @@ def receive_events(self): buf = '' while self._run: try: - data = self.sock_file.readline() + data = self.sock_file.readline().decode('utf-8') except Exception: self._run = False self.connected = False @@ -114,7 +114,7 @@ def _read_socket(sock, length): # FIXME(italo): if not data raise error data += sock.read(length - data_length) data_length = len(data) - return data + return data.decode('utf-8') def handle_event(self, event): if event.headers['Content-Type'] == 'auth/request': @@ -243,7 +243,7 @@ def connect(self): % self.timeout) self.connected = True self.sock.settimeout(None) - self.sock_file = self.sock.makefile() + self.sock_file = self.sock.makefile('rb') self.start_event_handlers() self._auth_request_event.wait() if not self.connected: From ffdddd58093c79d67fabd26ca97808b5dad8da7d Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 27 Aug 2020 21:45:20 +0300 Subject: [PATCH 2/3] add event body --- greenswitch/esl.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/greenswitch/esl.py b/greenswitch/esl.py index 88416b7..e4c6735 100644 --- a/greenswitch/esl.py +++ b/greenswitch/esl.py @@ -32,14 +32,30 @@ def parse_data(self, data): data = data.strip().splitlines() last_key = None value = '' + is_event = False + has_body = False + body = '' for line in data: + if has_body: + #TODO: Check multiline body + body += line + continue if ': ' in line: key, value = line.split(': ', 1) + #event can have own body. for example DETECTED_SPEECH + if key == 'Event-Name': + is_event = True + if is_event and key == 'Content-Length': + # body is the last header in event + has_body = True + continue last_key = key else: key = last_key value += '\n' + line self.headers[key.strip()] = value.strip() + if has_body and len(body) > 0: + self.headers["_body"] = body class ESLProtocol(object): From 0b40c50fbf0dd9f1499409bb985b44d18883201c Mon Sep 17 00:00:00 2001 From: alexey Date: Thu, 27 Aug 2020 21:52:47 +0300 Subject: [PATCH 3/3] add execute command to InboundESL --- greenswitch/esl.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/greenswitch/esl.py b/greenswitch/esl.py index e4c6735..9acea9e 100644 --- a/greenswitch/esl.py +++ b/greenswitch/esl.py @@ -272,6 +272,16 @@ def authenticate(self): if response.headers['Reply-Text'] != '+OK accepted': raise ValueError('Invalid password.') + def execute(self, uuid, app, app_args=None): + command = 'SendMsg {}\n'.format(uuid) + command += 'call-command: execute\n' + command += 'execute-app-name: {}'.format(app) + if app_args: + command += "\nexecute-app-arg: {}".format(app_args) + + return self.send(command) + + def __enter__(self): self.connect() return self