-
Notifications
You must be signed in to change notification settings - Fork 82
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
Mail channel filter and stop #1650
Changes from all commits
6c99982
4c999fc
34821e5
96c9311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,9 @@ | ||||||||||||||||||||||||||
from urllib.parse import urljoin | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
from kairon import Utility | ||||||||||||||||||||||||||
from kairon.exceptions import AppException | ||||||||||||||||||||||||||
from kairon.shared.channels.mail.processor import MailProcessor | ||||||||||||||||||||||||||
from kairon.shared.chat.data_objects import Channels | ||||||||||||||||||||||||||
from kairon.shared.constants import ChannelTypes | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
class MailScheduler: | ||||||||||||||||||||||||||
|
@@ -28,6 +28,23 @@ def request_epoch(bot: str): | |||||||||||||||||||||||||
if not resp['success']: | ||||||||||||||||||||||||||
raise AppException("Failed to request email channel epoch") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@staticmethod | ||||||||||||||||||||||||||
def request_stop(bot: str): | ||||||||||||||||||||||||||
event_server_url = Utility.get_event_server_url() | ||||||||||||||||||||||||||
if Utility.is_exist(Channels, raise_error=False, bot=bot, connector_type=ChannelTypes.MAIL.value): | ||||||||||||||||||||||||||
resp = Utility.execute_http_request( | ||||||||||||||||||||||||||
"GET", | ||||||||||||||||||||||||||
urljoin( | ||||||||||||||||||||||||||
event_server_url, | ||||||||||||||||||||||||||
f"/api/mail/stop/{bot}", | ||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||
Comment on lines
+35
to
+40
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. Use appropriate HTTP method for stopping the email channel Using Apply this diff to change the HTTP method: - resp = Utility.execute_http_request(
- "GET",
+ resp = Utility.execute_http_request(
+ "POST",
urljoin(
event_server_url,
f"/api/mail/stop/{bot}",
),
err_msg="Failed to request epoch",
) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
err_msg="Failed to request epoch", | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
if not resp['success']: | ||||||||||||||||||||||||||
raise AppException("Failed to stop email channel reading") | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
raise AppException("Mail channel does not exist") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
from unittest.mock import patch, MagicMock | ||
|
||
import pytest | ||
from imap_tools import MailMessage | ||
from imap_tools import MailMessage, AND | ||
|
||
from mongoengine import connect, disconnect | ||
from uuid6 import uuid7 | ||
|
@@ -276,6 +276,53 @@ def test_process_mail(self, mock_get_channel_config): | |
assert result == "Hello John Doe, How can I help you today?" | ||
|
||
|
||
|
||
@patch("kairon.shared.chat.processor.ChatDataProcessor.get_channel_config") | ||
@pytest.mark.asyncio | ||
async def test_generate_criteria(self, mock_get_channel_config): | ||
bot_id = pytest.mail_test_bot | ||
mock_get_channel_config.return_value = { | ||
'config': { | ||
'email_account': "[email protected]", | ||
'email_password': "password", | ||
'imap_server': "imap.testuser.com", | ||
} | ||
} | ||
|
||
mp = MailProcessor(bot=bot_id) | ||
mp.state.last_email_uid = 123 | ||
#seen | ||
criteria = mp.generate_criteria(read_status="seen") | ||
print(criteria) | ||
assert criteria == '((SEEN) (UID 124:*))' | ||
|
||
#unseen | ||
criteria = mp.generate_criteria(read_status="unseen") | ||
assert criteria == '((UNSEEN) (UID 124:*))' | ||
|
||
#default | ||
criteria = mp.generate_criteria() | ||
assert criteria == '((UID 124:*))' | ||
|
||
#subjects | ||
criteria = mp.generate_criteria(subjects=["Test Subject", "another test subject"]) | ||
assert criteria == '((OR SUBJECT "Test Subject" SUBJECT "another test subject") (UID 124:*))' | ||
|
||
#from | ||
criteria = mp.generate_criteria(from_addresses=["info", "[email protected]", "[email protected]"]) | ||
assert criteria == '((OR OR FROM "[email protected]" FROM "[email protected]" FROM "info") (UID 124:*))' | ||
|
||
#mix | ||
criteria = mp.generate_criteria(read_status="unseen", | ||
subjects=["Test Subject", "another test subject", "happy"], | ||
ignore_subjects=['cat'], | ||
ignore_from=["info", "nomreply"], | ||
from_addresses=["@digite.com", "@nimblework.com"]) | ||
|
||
assert criteria == '((UNSEEN) (OR OR SUBJECT "Test Subject" SUBJECT "another test subject" SUBJECT "happy") NOT ((SUBJECT "cat")) (OR FROM "@digite.com" FROM "@nimblework.com") NOT ((FROM "info")) NOT ((FROM "nomreply")) (UID 124:*))' | ||
|
||
|
||
|
||
@patch("kairon.shared.channels.mail.processor.MailProcessor.logout_imap") | ||
@patch("kairon.shared.channels.mail.processor.MailProcessor.process_message_task") | ||
@patch("kairon.shared.channels.mail.processor.MailBox") | ||
|
@@ -538,4 +585,7 @@ def test_get_log_exception(self): | |
BotSettings.objects(user="mail_channel_test_user_acc").delete() | ||
Bot.objects(user="mail_channel_test_user_acc").delete() | ||
Account.objects(user="mail_channel_test_user_acc").delete() | ||
Channels.objects(connector_type=ChannelTypes.MAIL.value).delete() | ||
Channels.objects(connector_type=ChannelTypes.MAIL.value).delete() | ||
|
||
|
||
|
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.
🛠️ Refactor suggestion
Use exception chaining to preserve the original traceback
In the
except
block, raise the new exception usingfrom e
to maintain the original traceback, aiding in debugging.Apply this diff to chain exceptions:
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.8.2)
74-74: Within an
except
clause, raise exceptions withraise ... from err
orraise ... from None
to distinguish them from errors in exception handling(B904)