-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
changes - #1635
Closed
Closed
changes - #1635
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,55 +3,11 @@ | |
from kairon import Utility | ||
from kairon.events.definitions.base import EventsBase | ||
from kairon.exceptions import AppException | ||
from kairon.shared.channels.mail.constants import MailConstants | ||
from kairon.shared.channels.mail.processor import MailProcessor | ||
from kairon.shared.constants import EventClass | ||
|
||
|
||
class MailProcessEvent(EventsBase): | ||
""" | ||
Event to start mail channel scheduler if not already running. | ||
""" | ||
|
||
def __init__(self, bot: Text, user: Text, **kwargs): | ||
""" | ||
Initialise event. | ||
""" | ||
self.bot = bot | ||
self.user = user | ||
|
||
def validate(self): | ||
""" | ||
validate mail channel exists | ||
""" | ||
return MailProcessor.validate_smtp_connection(self.bot) | ||
|
||
|
||
def enqueue(self, **kwargs): | ||
""" | ||
Send event to event server. | ||
""" | ||
try: | ||
mails: list = kwargs.get('mails', []) | ||
payload = {'bot': self.bot, 'user': self.user, 'mails': mails} | ||
Utility.request_event_server(EventClass.mail_channel_process_mails, payload) | ||
except Exception as e: | ||
logger.error(str(e)) | ||
raise AppException(e) | ||
|
||
def execute(self, **kwargs): | ||
""" | ||
Execute the event. | ||
""" | ||
try: | ||
mails = kwargs.get('mails') | ||
if mails: | ||
MailProcessor.process_message_task(self.bot, mails) | ||
except Exception as e: | ||
logger.error(str(e)) | ||
raise AppException(e) | ||
|
||
|
||
|
||
class MailReadEvent(EventsBase): | ||
""" | ||
Event to read mails from mail channel and create events for each mail tp process them via bot | ||
|
@@ -66,16 +22,17 @@ def __init__(self, bot: Text, user: Text, **kwargs): | |
|
||
def validate(self): | ||
""" | ||
validate mail channel exists | ||
validate mail channel exists and works properly | ||
""" | ||
return MailProcessor.validate_imap_connection(self.bot) | ||
return MailProcessor.validate_imap_connection(self.bot) and MailProcessor.validate_imap_connection(self.bot) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to smtp and imap validation |
||
|
||
def enqueue(self, **kwargs): | ||
""" | ||
Send event to event server. | ||
""" | ||
try: | ||
payload = {'bot': self.bot, 'user': self.user} | ||
self.validate() | ||
Utility.request_event_server(EventClass.mail_channel_read_mails, payload) | ||
except Exception as e: | ||
logger.error(str(e)) | ||
|
@@ -87,12 +44,10 @@ def execute(self, **kwargs): | |
""" | ||
try: | ||
vals = MailProcessor.read_mails(self.bot) | ||
print(vals) | ||
emails, _, _ = vals | ||
for email in emails: | ||
ev = MailProcessEvent(self.bot, self.user) | ||
ev.validate() | ||
ev.enqueue(mails=[email]) | ||
|
||
emails, _ = vals | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra veriable vals (line) |
||
batch_size = MailConstants.PROCESS_MESSAGE_BATCH_SIZE | ||
for i in range(0, len(emails), batch_size): | ||
batch = emails[i:i + batch_size] | ||
MailProcessor.process_message_task(self.bot, batch) | ||
except Exception as e: | ||
raise AppException(f"Failed to schedule mail reading for bot {self.bot}. Error: {str(e)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,8 +172,7 @@ async def test_send_mail(self, mock_get_channel_config, mock_smtp): | |
mail_response_log = MailResponseLog(bot=pytest.mail_test_bot, | ||
sender_id="[email protected]", | ||
user="mail_channel_test_user_acc", | ||
subject="Test Subject", | ||
body="Test Body", | ||
uid=123 | ||
) | ||
mail_response_log.save() | ||
mail_response_log.save() | ||
|
@@ -210,8 +209,7 @@ async def test_send_mail_exception(self, mock_get_channel_config, mock_smtp): | |
mail_response_log = MailResponseLog(bot=pytest.mail_test_bot, | ||
sender_id="[email protected]", | ||
user="mail_channel_test_user_acc", | ||
subject="Test Subject", | ||
body="Test Body", | ||
uid=123 | ||
) | ||
mail_response_log.save() | ||
|
||
|
@@ -253,8 +251,7 @@ def test_process_mail(self, mock_get_channel_config): | |
mail_response_log = MailResponseLog(bot=pytest.mail_test_bot, | ||
sender_id="[email protected]", | ||
user="mail_channel_test_user_acc", | ||
subject="Test Subject", | ||
body="Test Body", | ||
uid=123 | ||
) | ||
mail_response_log.save() | ||
|
||
|
@@ -311,15 +308,14 @@ async def test_read_mails(self, mock_get_channel_config, | |
mock_mailbox_instance.login.return_value = mock_mailbox_instance | ||
mock_mailbox_instance.fetch.return_value = [mock_mail_message] | ||
|
||
mails, user, time_shift = MailProcessor.read_mails(bot_id) | ||
mails, user = MailProcessor.read_mails(bot_id) | ||
print(mails) | ||
assert len(mails) == 1 | ||
assert mails[0]["subject"] == "Test Subject" | ||
assert mails[0]["mail_id"] == "[email protected]" | ||
assert mails[0]["date"] == "2023-10-10" | ||
assert mails[0]["body"] == "Test Body" | ||
assert user == 'mail_channel_test_user_acc' | ||
assert time_shift == 1200 | ||
|
||
|
||
|
||
|
@@ -349,10 +345,9 @@ async def test_read_mails_no_messages(self, mock_get_channel_config, | |
mock_mailbox_instance.login.return_value = mock_mailbox_instance | ||
mock_mailbox_instance.fetch.return_value = [] | ||
|
||
mails, user, time_shift = MailProcessor.read_mails(bot_id) | ||
mails, user = MailProcessor.read_mails(bot_id) | ||
assert len(mails) == 0 | ||
assert user == 'mail_channel_test_user_acc' | ||
assert time_shift == 1200 | ||
|
||
mock_logout_imap.assert_called_once() | ||
|
||
|
@@ -369,8 +364,7 @@ async def test_process_messages(self, mock_process_messages_via_bot, mock_send_m | |
mail_response_log = MailResponseLog(bot=pytest.mail_test_bot, | ||
sender_id="[email protected]", | ||
user="mail_channel_test_user_acc", | ||
subject="Test Subject", | ||
body="Test Body", | ||
uid=123 | ||
) | ||
mail_response_log.save() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix duplicate validation call
The validate method contains a duplicate call to
validate_imap_connection
:📝 Committable suggestion