From 5789c141646b5571d52804aa6b593a165bee21d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20D=C4=99bski?= <m.debski@livechat.com> Date: Tue, 28 Nov 2023 13:49:00 +0100 Subject: [PATCH 1/3] Introduced max-content length within the requests param --- livechat/utils/httpx_logger.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/livechat/utils/httpx_logger.py b/livechat/utils/httpx_logger.py index bb90e11..abfd3f6 100644 --- a/livechat/utils/httpx_logger.py +++ b/livechat/utils/httpx_logger.py @@ -10,6 +10,8 @@ class HttpxLogger: ''' Logger for httpx requests. ''' + MAX_CONTENT_LENGTH_TO_LOG = 1000 + def __init__(self, disable_logging: bool = False): self.disable_logging = disable_logging @@ -30,6 +32,9 @@ def log_request(self, request: httpx.Request) -> None: dict(request.headers.items()), indent=4, ) + if len(request_params) > self.MAX_CONTENT_LENGTH_TO_LOG: + request_params = f'{request_params[:self.MAX_CONTENT_LENGTH_TO_LOG]}... (Truncated)' + request_debug = f'Request params:\n{request_params}\n' \ f'Request headers:\n{request_headers}' From 994c8d723d169ba7eb5c8fb3af828fd0c7432da7 Mon Sep 17 00:00:00 2001 From: kacperf531 <k.frackowski@livechatinc.com> Date: Mon, 27 Nov 2023 12:20:31 +0100 Subject: [PATCH 2/3] Add author_id param to send_event in agent-api rtm --- changelog.md | 5 +++++ livechat/agent/rtm/api/v33.py | 9 +++++++-- livechat/agent/rtm/api/v34.py | 9 +++++++-- livechat/agent/rtm/api/v35.py | 9 +++++++-- livechat/agent/rtm/api/v36.py | 9 +++++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/changelog.md b/changelog.md index 71f1dcb..db2ded7 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,11 @@ # Change Log All notable changes to this project will be documented in this file. +## [0.3.8] - TBA + +### Bugfixes +- Allow sending rtm events as a bot by adding `author_id` param. + ## [0.3.7] - 2023-09-26 ### Added diff --git a/livechat/agent/rtm/api/v33.py b/livechat/agent/rtm/api/v33.py index 46e7707..028feff 100644 --- a/livechat/agent/rtm/api/v33.py +++ b/livechat/agent/rtm/api/v33.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.3. ''' -from typing import Any +from typing import Any, Optional from livechat.utils.helpers import prepare_payload from livechat.utils.structures import RtmResponse @@ -356,6 +356,7 @@ def send_event(self, chat_id: str = None, event: dict = None, attach_to_last_thread: bool = None, + author_id: Optional[str] = None, payload: dict = None) -> RtmResponse: ''' Sends an Event object. @@ -364,6 +365,7 @@ def send_event(self, event (dict): Event object. attach_to_last_thread (bool): Flag which states if event object should be added to last thread. The flag is ignored for active chats. + author_id (optional str): Provide if the event should be sent on behalf of a bot. payload (dict): Custom payload to be used as request's data. It overrides all other parameters provided for the method. @@ -371,9 +373,12 @@ def send_event(self, RtmResponse: RTM response structure (`request_id`, `action`, `type`, `success` and `payload` properties) ''' + opts = {} + if author_id: + opts['author_id'] = author_id if payload is None: payload = prepare_payload(locals()) - return self.ws.send({'action': 'send_event', 'payload': payload}) + return self.ws.send({'action': 'send_event', 'payload': payload, **opts}) def send_rich_message_postback(self, chat_id: str = None, diff --git a/livechat/agent/rtm/api/v34.py b/livechat/agent/rtm/api/v34.py index dc459c4..1fca262 100644 --- a/livechat/agent/rtm/api/v34.py +++ b/livechat/agent/rtm/api/v34.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.4. ''' -from typing import Any +from typing import Any, Optional from livechat.utils.helpers import prepare_payload from livechat.utils.structures import RtmResponse @@ -322,6 +322,7 @@ def send_event(self, chat_id: str = None, event: dict = None, attach_to_last_thread: bool = None, + author_id: Optional[str] = None, payload: dict = None) -> RtmResponse: ''' Sends an Event object. @@ -330,6 +331,7 @@ def send_event(self, event (dict): Event object. attach_to_last_thread (bool): Flag which states if event object should be added to last thread. The flag is ignored for active chats. + author_id (optional str): Provide if the event should be sent on behalf of a bot. payload (dict): Custom payload to be used as request's data. It overrides all other parameters provided for the method. @@ -337,9 +339,12 @@ def send_event(self, RtmResponse: RTM response structure (`request_id`, `action`, `type`, `success` and `payload` properties) ''' + opts = {} + if author_id: + opts['author_id'] = author_id if payload is None: payload = prepare_payload(locals()) - return self.ws.send({'action': 'send_event', 'payload': payload}) + return self.ws.send({'action': 'send_event', 'payload': payload, **opts}) def send_rich_message_postback(self, chat_id: str = None, diff --git a/livechat/agent/rtm/api/v35.py b/livechat/agent/rtm/api/v35.py index 3581de8..7ee5934 100644 --- a/livechat/agent/rtm/api/v35.py +++ b/livechat/agent/rtm/api/v35.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.5. ''' -from typing import Any +from typing import Any, Optional from livechat.utils.helpers import prepare_payload from livechat.utils.structures import RtmResponse @@ -322,6 +322,7 @@ def send_event(self, chat_id: str = None, event: dict = None, attach_to_last_thread: bool = None, + author_id: Optional[str] = None, payload: dict = None) -> RtmResponse: ''' Sends an Event object. @@ -330,6 +331,7 @@ def send_event(self, event (dict): Event object. attach_to_last_thread (bool): Flag which states if event object should be added to last thread. The flag is ignored for active chats. + author_id (optional str): Provide if the event should be sent on behalf of a bot. payload (dict): Custom payload to be used as request's data. It overrides all other parameters provided for the method. @@ -337,9 +339,12 @@ def send_event(self, RtmResponse: RTM response structure (`request_id`, `action`, `type`, `success` and `payload` properties) ''' + opts = {} + if author_id: + opts['author_id'] = author_id if payload is None: payload = prepare_payload(locals()) - return self.ws.send({'action': 'send_event', 'payload': payload}) + return self.ws.send({'action': 'send_event', 'payload': payload, **opts}) def send_rich_message_postback(self, chat_id: str = None, diff --git a/livechat/agent/rtm/api/v36.py b/livechat/agent/rtm/api/v36.py index 2a0b2e2..de91b58 100644 --- a/livechat/agent/rtm/api/v36.py +++ b/livechat/agent/rtm/api/v36.py @@ -1,6 +1,6 @@ ''' Module containing Agent RTM API client implementation for v3.6. ''' -from typing import Any +from typing import Any, Optional from livechat.utils.helpers import prepare_payload from livechat.utils.structures import RtmResponse @@ -322,6 +322,7 @@ def send_event(self, chat_id: str = None, event: dict = None, attach_to_last_thread: bool = None, + author_id: Optional[str] = None, payload: dict = None) -> RtmResponse: ''' Sends an Event object. @@ -330,6 +331,7 @@ def send_event(self, event (dict): Event object. attach_to_last_thread (bool): Flag which states if event object should be added to last thread. The flag is ignored for active chats. + author_id (optional str): Provide if the event should be sent on behalf of a bot. payload (dict): Custom payload to be used as request's data. It overrides all other parameters provided for the method. @@ -337,9 +339,12 @@ def send_event(self, RtmResponse: RTM response structure (`request_id`, `action`, `type`, `success` and `payload` properties) ''' + opts = {} + if author_id: + opts['author_id'] = author_id if payload is None: payload = prepare_payload(locals()) - return self.ws.send({'action': 'send_event', 'payload': payload}) + return self.ws.send({'action': 'send_event', 'payload': payload, **opts}) def send_rich_message_postback(self, chat_id: str = None, From c27b67e56411eed82c3d93f679ef21eff1a2ffb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20D=C4=99bski?= <m.debski@livechat.com> Date: Tue, 28 Nov 2023 14:23:15 +0100 Subject: [PATCH 3/3] Updated changelog --- changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.md b/changelog.md index db2ded7..87d4046 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. ## [0.3.8] - TBA +### Changed +- Implemented truncation of request params in logging for large data. + ### Bugfixes - Allow sending rtm events as a bot by adding `author_id` param.