mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
embedded bots: Log warning when bot quit()s.
External bots may call bot_handler.quit() when they wish to terminate, e.g. due to a misconfiguration. Currently, embedded bots ignore calls to quit(), even though they signal a problem. This commit does the first step in handling quit() calls by logging a warning.
This commit is contained in:
@@ -57,6 +57,9 @@ class StateHandler:
|
|||||||
def contains(self, key: Text) -> bool:
|
def contains(self, key: Text) -> bool:
|
||||||
return is_key_in_bot_storage(self.user_profile, key)
|
return is_key_in_bot_storage(self.user_profile, key)
|
||||||
|
|
||||||
|
class EmbeddedBotQuitException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class EmbeddedBotHandler:
|
class EmbeddedBotHandler:
|
||||||
def __init__(self, user_profile: UserProfile) -> None:
|
def __init__(self, user_profile: UserProfile) -> None:
|
||||||
# Only expose a subset of our UserProfile's functionality
|
# Only expose a subset of our UserProfile's functionality
|
||||||
@@ -113,3 +116,6 @@ class EmbeddedBotHandler:
|
|||||||
if optional:
|
if optional:
|
||||||
return dict()
|
return dict()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def quit(self, message: str= "") -> None:
|
||||||
|
raise EmbeddedBotQuitException(message)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
from mock import patch
|
||||||
from typing import Any, Dict, Tuple, Text, Optional
|
from typing import Any, Dict, Tuple, Text, Optional
|
||||||
|
|
||||||
|
from zerver.lib.bot_lib import EmbeddedBotQuitException
|
||||||
from zerver.lib.test_classes import ZulipTestCase
|
from zerver.lib.test_classes import ZulipTestCase
|
||||||
from zerver.models import UserProfile, Recipient, get_display_recipient
|
from zerver.models import UserProfile, Recipient, get_display_recipient
|
||||||
|
|
||||||
@@ -47,6 +49,15 @@ class TestEmbeddedBotMessaging(ZulipTestCase):
|
|||||||
last_message = self.get_last_message()
|
last_message = self.get_last_message()
|
||||||
self.assertEqual(last_message.content, "foo")
|
self.assertEqual(last_message.content, "foo")
|
||||||
|
|
||||||
|
def test_embedded_bot_quit_exception(self) -> None:
|
||||||
|
with patch('zulip_bots.bots.helloworld.helloworld.HelloWorldHandler.handle_message',
|
||||||
|
side_effect=EmbeddedBotQuitException("I'm quitting!")):
|
||||||
|
with patch('logging.warning') as mock_logging:
|
||||||
|
self.send_stream_message(self.user_profile.email, "Denmark",
|
||||||
|
content="@**{}** foo".format(self.bot_profile.full_name),
|
||||||
|
topic_name="bar")
|
||||||
|
mock_logging.assert_called_once_with("I'm quitting!")
|
||||||
|
|
||||||
class TestEmbeddedBotFailures(ZulipTestCase):
|
class TestEmbeddedBotFailures(ZulipTestCase):
|
||||||
@mock.patch("logging.error")
|
@mock.patch("logging.error")
|
||||||
def test_invalid_embedded_bot_service(self, logging_error_mock: mock.Mock) -> None:
|
def test_invalid_embedded_bot_service(self, logging_error_mock: mock.Mock) -> None:
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ from zerver.lib.outgoing_webhook import do_rest_call, get_outgoing_webhook_servi
|
|||||||
from zerver.models import get_bot_services
|
from zerver.models import get_bot_services
|
||||||
from zulip import Client
|
from zulip import Client
|
||||||
from zulip_bots.lib import extract_query_without_mention
|
from zulip_bots.lib import extract_query_without_mention
|
||||||
from zerver.lib.bot_lib import EmbeddedBotHandler, get_bot_handler
|
from zerver.lib.bot_lib import EmbeddedBotHandler, get_bot_handler, EmbeddedBotQuitException
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@@ -529,19 +529,22 @@ class EmbeddedBotWorker(QueueProcessingWorker):
|
|||||||
logging.error("Error: User %s has bot with invalid embedded bot service %s" % (
|
logging.error("Error: User %s has bot with invalid embedded bot service %s" % (
|
||||||
user_profile_id, service.name))
|
user_profile_id, service.name))
|
||||||
continue
|
continue
|
||||||
if hasattr(bot_handler, 'initialize'):
|
try:
|
||||||
bot_handler.initialize(self.get_bot_api_client(user_profile))
|
if hasattr(bot_handler, 'initialize'):
|
||||||
if event['trigger'] == 'mention':
|
bot_handler.initialize(self.get_bot_api_client(user_profile))
|
||||||
message['content'] = extract_query_without_mention(
|
if event['trigger'] == 'mention':
|
||||||
|
message['content'] = extract_query_without_mention(
|
||||||
|
message=message,
|
||||||
|
client=self.get_bot_api_client(user_profile),
|
||||||
|
)
|
||||||
|
if message['content'] is None:
|
||||||
|
return
|
||||||
|
bot_handler.handle_message(
|
||||||
message=message,
|
message=message,
|
||||||
client=self.get_bot_api_client(user_profile),
|
bot_handler=self.get_bot_api_client(user_profile)
|
||||||
)
|
)
|
||||||
if message['content'] is None:
|
except EmbeddedBotQuitException as e:
|
||||||
return
|
logging.warning(str(e))
|
||||||
bot_handler.handle_message(
|
|
||||||
message=message,
|
|
||||||
bot_handler=self.get_bot_api_client(user_profile)
|
|
||||||
)
|
|
||||||
|
|
||||||
@assign_queue('deferred_work')
|
@assign_queue('deferred_work')
|
||||||
class DeferredWorker(QueueProcessingWorker):
|
class DeferredWorker(QueueProcessingWorker):
|
||||||
|
|||||||
Reference in New Issue
Block a user