From 45a5000e5fe923e5d2da0290d7b671d0ffdb56ea Mon Sep 17 00:00:00 2001 From: Spandan Mondal Date: Thu, 19 Dec 2024 14:17:09 +0530 Subject: [PATCH] changed users to mail ids, added mail_template to integration fields (#1673) * changed users to mail ids, added mail_template to integration fields for mail channel * changed users to mail ids, added mail_template to integration fields for mail channel --------- Co-authored-by: spandan_mondal --- kairon/chat/utils.py | 12 ++++++------ kairon/shared/channels/mail/processor.py | 8 ++++++-- metadata/integrations.yml | 2 +- tests/unit_test/chat/chat_test.py | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/kairon/chat/utils.py b/kairon/chat/utils.py index c0e909355..cf105dd56 100644 --- a/kairon/chat/utils.py +++ b/kairon/chat/utils.py @@ -1,6 +1,6 @@ import datetime import os -from typing import Text, Dict +from typing import Text, Dict, List from loguru import logger from pymongo.collection import Collection @@ -45,10 +45,10 @@ async def chat( @staticmethod async def process_messages_via_bot( - messages: [str], + messages: List[str], account: int, bot: str, - user: str, + users: List[str], is_integration_user: bool = False, metadata: Dict = None, ): @@ -57,7 +57,7 @@ async def process_messages_via_bot( messages: List of messages to process account: Account ID bot: Bot ID - user: User ID + users: List of User IDs is_integration_user: Flag indicating if user is integration user metadata: Additional metadata @@ -67,8 +67,8 @@ async def process_messages_via_bot( responses = [] uncached_model = AgentProcessor.get_agent_without_cache(bot, False) metadata = ChatUtils.get_metadata(account, bot, is_integration_user, metadata) - for message in messages: - msg = UserMessage(message, sender_id=user, metadata=metadata) + for index, message, in enumerate(messages): + msg = UserMessage(message, sender_id=users[index], metadata=metadata) chat_response = await uncached_model.handle_message(msg) responses.append(chat_response) return responses diff --git a/kairon/shared/channels/mail/processor.py b/kairon/shared/channels/mail/processor.py index 3554ceb8a..b7003232a 100644 --- a/kairon/shared/channels/mail/processor.py +++ b/kairon/shared/channels/mail/processor.py @@ -26,7 +26,9 @@ def __init__(self, bot): self.bot = bot self.config = ChatDataProcessor.get_channel_config(ChannelTypes.MAIL, bot, False)['config'] self.intent = self.config.get('intent') - self.mail_template = self.config.get('mail_template', MailConstants.DEFAULT_TEMPLATE) + self.mail_template = self.config.get('mail_template') + if not self.mail_template or len(self.mail_template) == 0: + self.mail_template = MailConstants.DEFAULT_TEMPLATE self.bot_settings = BotSettings.objects(bot=self.bot).get() self.state = MailProcessor.get_mail_channel_state_data(bot) bot_info = Bot.objects.get(id=bot) @@ -177,6 +179,7 @@ async def process_messages(bot: str, batch: [dict]): from kairon.chat.utils import ChatUtils mp = MailProcessor(bot) user_messages: [str] = [] + users: [str] = [] responses = [] for mail in batch: try: @@ -189,6 +192,7 @@ async def process_messages(bot: str, batch: [dict]): entities_str = json.dumps(entities) user_msg = f'/{mp.intent}{entities_str}' user_messages.append(user_msg) + users.append(mail['mail_id']) subject = mail.get('subject', 'Reply') if not subject.startswith('Re:'): subject = f"Re: {subject}" @@ -205,7 +209,7 @@ async def process_messages(bot: str, batch: [dict]): chat_responses = await ChatUtils.process_messages_via_bot(user_messages, mp.account, bot, - mp.bot_settings.user, + users, False, { 'channel': ChannelTypes.MAIL.value diff --git a/metadata/integrations.yml b/metadata/integrations.yml index 5b8b5755d..a8904d994 100644 --- a/metadata/integrations.yml +++ b/metadata/integrations.yml @@ -90,7 +90,7 @@ channels: - from_emails - ignore_from_emails - seen_status - + - mail_template actions: pipedrive: required_fields: diff --git a/tests/unit_test/chat/chat_test.py b/tests/unit_test/chat/chat_test.py index 94451e754..9add9b72a 100644 --- a/tests/unit_test/chat/chat_test.py +++ b/tests/unit_test/chat/chat_test.py @@ -946,7 +946,7 @@ async def test_process_messages_via_bot(mock_get_metadata, mock_get_agent_withou messages = ["/greet", "/bye"] account = 1 bot = "test_bot" - user = "test_user" + users = ["test_user", "test_user2"] is_integration_user = False metadata = {"key": "value"} @@ -956,7 +956,7 @@ async def test_process_messages_via_bot(mock_get_metadata, mock_get_agent_withou mock_model.handle_message = AsyncMock(side_effect=[{"text": "Hello"}, {"text": "Goodbye"}]) from kairon.chat.utils import ChatUtils - responses = await ChatUtils.process_messages_via_bot(messages, account, bot, user, is_integration_user, metadata) + responses = await ChatUtils.process_messages_via_bot(messages, account, bot, users, is_integration_user, metadata) assert len(responses) == 2 assert responses[0] == {"text": "Hello"}