Skip to content

Commit

Permalink
changed users to mail ids, added mail_template to integration fields (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
hasinaxp and spandan_mondal authored Dec 19, 2024
1 parent e31e904 commit 45a5000
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
12 changes: 6 additions & 6 deletions kairon/chat/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
):
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions kairon/shared/channels/mail/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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}"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion metadata/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ channels:
- from_emails
- ignore_from_emails
- seen_status

- mail_template
actions:
pipedrive:
required_fields:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_test/chat/chat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand All @@ -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"}
Expand Down

0 comments on commit 45a5000

Please sign in to comment.