From d1cdc1e6a7b20ac614216b94aa81c6de5674af76 Mon Sep 17 00:00:00 2001 From: Mahesh Date: Thu, 30 Nov 2023 15:07:28 +0530 Subject: [PATCH] fixed test cases. --- .../business_messages/business_messages.py | 29 +++++++++---------- tests/unit_test/utility_test.py | 4 +-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/kairon/chat/handlers/channels/business_messages/business_messages.py b/kairon/chat/handlers/channels/business_messages/business_messages.py index 17245c9f9..bb09da434 100644 --- a/kairon/chat/handlers/channels/business_messages/business_messages.py +++ b/kairon/chat/handlers/channels/business_messages/business_messages.py @@ -72,17 +72,16 @@ async def handle_message(self): message = request_body['message']['text'] conversation_id = request_body['conversationId'] message_id = request_body['message']['messageId'] - business_messages = BusinessMessages(message, conversation_id, credentials_json) - await business_messages.handle_user_message(text=message, sender_id=self.user.email, - metadata=metadata, bot=self.bot, message_id=message_id) + business_messages = BusinessMessages(credentials_json) + await business_messages.handle_user_message(text=message, sender_id=self.user.email, metadata=metadata, + conversation_id=conversation_id, bot=self.bot, + message_id=message_id) return '' class BusinessMessages: - def __init__(self, message: Text, conversation_id: Text, credentials_json: Dict): - self.message = message - self.conversation_id = conversation_id + def __init__(self, credentials_json: Dict): self.credentials_json = credentials_json @classmethod @@ -108,7 +107,8 @@ def get_business_message_credentials(self): return credentials async def handle_user_message( - self, text: Text, sender_id: Text, message_id: Text, metadata: Optional[Dict[Text, Any]], bot: str + self, text: Text, sender_id: Text, message_id: Text, conversation_id: Text, + metadata: Optional[Dict[Text, Any]], bot: str ) -> None: user_msg = UserMessage(text=text, message_id=message_id, input_channel=self.name(), sender_id=sender_id, metadata=metadata) @@ -120,8 +120,7 @@ async def handle_user_message( logger.exception( "Exception when trying to handle webhook for business message." ) - self.message = message - await self.send_message() + await self.send_message(message=message, conversation_id=conversation_id) @staticmethod async def process_message(bot: str, user_message: UserMessage): @@ -130,14 +129,14 @@ async def process_message(bot: str, user_message: UserMessage): print(response) return response - async def send_message(self): + async def send_message(self, message: Text, conversation_id: Text): """ Posts a message to the Business Messages API, first sending a typing indicator event and sending a stop typing event after the message has been sent. Args: - message (obj): The message object payload to send to the user. + message (str): The message to send to the user. conversation_id (str): The unique id for this user and agent. """ credentials = self.get_business_message_credentials() @@ -151,7 +150,7 @@ async def send_message(self): ), eventType=BusinessMessagesEvent.EventTypeValueValuesEnum.TYPING_STARTED ), - parent='conversations/' + self.conversation_id) + parent='conversations/' + conversation_id) bm_client.BusinessmessagesV1.ConversationsEventsService( client=client).Create(request=create_request) @@ -161,11 +160,11 @@ async def send_message(self): representative=BusinessMessagesRepresentative( representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT ), - text=self.message) + text=message) create_request = BusinessmessagesConversationsMessagesCreateRequest( businessMessagesMessage=message_obj, - parent='conversations/' + self.conversation_id) + parent='conversations/' + conversation_id) bm_client.BusinessmessagesV1.ConversationsMessagesService( client=client).Create(request=create_request) @@ -179,7 +178,7 @@ async def send_message(self): ), eventType=BusinessMessagesEvent.EventTypeValueValuesEnum.TYPING_STOPPED ), - parent='conversations/' + self.conversation_id) + parent='conversations/' + conversation_id) bm_client.BusinessmessagesV1.ConversationsEventsService( client=client).Create(request=create_request) diff --git a/tests/unit_test/utility_test.py b/tests/unit_test/utility_test.py index 691c995e7..ebce18857 100644 --- a/tests/unit_test/utility_test.py +++ b/tests/unit_test/utility_test.py @@ -2128,8 +2128,8 @@ def test_load_json_file_with_incorrect_path_raise_exception(self): assert str(error.value) == "file not found" def test_get_channels(self): - expected_channels = ['msteams', 'slack', 'telegram', 'hangouts', 'messenger', 'instagram', 'whatsapp', - 'business_messages'] + expected_channels = ['msteams', 'slack', 'telegram', 'business_messages','hangouts', + 'messenger', 'instagram', 'whatsapp'] channels = Utility.get_channels() assert channels == expected_channels