Skip to content

Commit

Permalink
fixed test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh committed Nov 30, 2023
1 parent 8ec426f commit d1cdc1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
4 changes: 2 additions & 2 deletions tests/unit_test/utility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d1cdc1e

Please sign in to comment.