From b62c86a6d76564830677a5b85568967f5dcab464 Mon Sep 17 00:00:00 2001 From: Bhavesh Jain Date: Sun, 19 Mar 2023 13:41:03 +0530 Subject: [PATCH 1/3] Renamed the BotHandler class to AbstractBotHandler --- zulip_bots/zulip_bots/lib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 1e526d802..088b6f9f6 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -175,7 +175,7 @@ def use_storage(storage: BotStorage, keys: List[str]) -> Iterator[BotStorage]: cache.flush() -class BotHandler(Protocol): +class AbstractBotHandler(Protocol): user_id: int email: str @@ -383,7 +383,7 @@ def quit(self, message: str = "") -> None: sys.exit(message) -def extract_query_without_mention(message: Dict[str, Any], client: BotHandler) -> Optional[str]: +def extract_query_without_mention(message: Dict[str, Any], client: AbstractBotHandler) -> Optional[str]: """ If the bot is the first @mention in the message, then this function returns the stripped message with the bot's @mention removed. Otherwise, it returns None. @@ -403,7 +403,7 @@ def extract_query_without_mention(message: Dict[str, Any], client: BotHandler) - def is_private_message_but_not_group_pm( - message_dict: Dict[str, Any], current_user: BotHandler + message_dict: Dict[str, Any], current_user: AbstractBotHandler ) -> bool: """ Checks whether a message dict represents a PM from another user. @@ -427,7 +427,7 @@ def display_config_file_errors(error_msg: str, config_file: str) -> None: print(f"\nMore details here:\n\n{error_msg}\n") -def prepare_message_handler(bot: str, bot_handler: BotHandler, bot_lib_module: Any) -> Any: +def prepare_message_handler(bot: str, bot_handler: AbstractBotHandler, bot_lib_module: Any) -> Any: message_handler = bot_lib_module.handler_class() if hasattr(message_handler, "validate_config"): config_data = bot_handler.get_config_info(bot) From d8fbcd698eac242c4f7a0c8ebaf6a21be7d0826f Mon Sep 17 00:00:00 2001 From: Bhavesh Jain Date: Mon, 20 Mar 2023 17:03:42 +0530 Subject: [PATCH 2/3] Changed the dependent files to use AbstractBotHandler --- .../packaged_helloworld.py | 4 ++-- .../bots/baremetrics/baremetrics.py | 8 +++---- .../zulip_bots/bots/beeminder/beeminder.py | 6 ++--- .../zulip_bots/bots/chessbot/chessbot.py | 24 +++++++++---------- .../zulip_bots/bots/converter/converter.py | 6 ++--- zulip_bots/zulip_bots/bots/define/define.py | 4 ++-- .../zulip_bots/bots/dialogflow/dialogflow.py | 6 ++--- .../bots/dropbox_share/dropbox_share.py | 6 ++--- zulip_bots/zulip_bots/bots/encrypt/encrypt.py | 4 ++-- .../bots/file_uploader/file_uploader.py | 4 ++-- zulip_bots/zulip_bots/bots/flock/flock.py | 6 ++--- .../zulip_bots/bots/followup/followup.py | 6 ++--- zulip_bots/zulip_bots/bots/front/front.py | 18 +++++++------- zulip_bots/zulip_bots/bots/giphy/giphy.py | 8 +++---- .../bots/github_detail/github_detail.py | 6 ++--- .../bots/google_search/google_search.py | 4 ++-- .../zulip_bots/bots/helloworld/helloworld.py | 4 ++-- zulip_bots/zulip_bots/bots/help/help.py | 4 ++-- .../zulip_bots/bots/idonethis/idonethis.py | 6 ++--- .../zulip_bots/bots/incident/incident.py | 6 ++--- .../bots/incrementor/incrementor.py | 6 ++--- zulip_bots/zulip_bots/bots/jira/jira.py | 6 ++--- .../bots/link_shortener/link_shortener.py | 8 +++---- zulip_bots/zulip_bots/bots/mention/mention.py | 8 +++---- .../bots/monkeytestit/monkeytestit.py | 6 ++--- .../zulip_bots/bots/salesforce/salesforce.py | 6 ++--- .../bots/stack_overflow/stack_overflow.py | 6 ++--- zulip_bots/zulip_bots/bots/susi/susi.py | 4 ++-- zulip_bots/zulip_bots/bots/trello/trello.py | 8 +++---- .../bots/trivia_quiz/trivia_quiz.py | 12 +++++----- .../zulip_bots/bots/twitpost/twitpost.py | 6 ++--- .../zulip_bots/bots/virtual_fs/virtual_fs.py | 4 ++-- zulip_bots/zulip_bots/bots/weather/weather.py | 8 +++---- .../zulip_bots/bots/wikipedia/wikipedia.py | 6 ++--- zulip_bots/zulip_bots/bots/witai/witai.py | 6 ++--- zulip_bots/zulip_bots/bots/xkcd/xkcd.py | 4 ++-- zulip_bots/zulip_bots/bots/yoda/yoda.py | 10 ++++---- zulip_bots/zulip_bots/bots/youtube/youtube.py | 6 ++--- zulip_bots/zulip_bots/game_handler.py | 6 ++--- zulip_botserver/tests/test_server.py | 4 ++-- 40 files changed, 135 insertions(+), 135 deletions(-) diff --git a/packaged_helloworld/packaged_helloworld/packaged_helloworld.py b/packaged_helloworld/packaged_helloworld/packaged_helloworld.py index a8b9aefb8..1c262ea90 100644 --- a/packaged_helloworld/packaged_helloworld/packaged_helloworld.py +++ b/packaged_helloworld/packaged_helloworld/packaged_helloworld.py @@ -3,7 +3,7 @@ import packaged_helloworld -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler __version__ = packaged_helloworld.__version__ @@ -18,7 +18,7 @@ def usage(self) -> str: sophisticated, bots that can be installed separately. """ - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: content = "beep boop" # type: str bot_handler.send_reply(message, content) diff --git a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py index c6d303d5f..0773017ab 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py @@ -4,11 +4,11 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class BaremetricsHandler: - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("baremetrics") self.api_key = self.config_info["api_key"] @@ -38,7 +38,7 @@ def initialize(self, bot_handler: BotHandler) -> None: self.check_api_key(bot_handler) - def check_api_key(self, bot_handler: BotHandler) -> None: + def check_api_key(self, bot_handler: AbstractBotHandler) -> None: url = "https://api.baremetrics.com/v1/account" test_query_response = requests.get(url, headers=self.auth_header) test_query_data = test_query_response.json() @@ -57,7 +57,7 @@ def usage(self) -> str: Version 1.0 """ - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: content = message["content"].strip().split() if content == []: diff --git a/zulip_bots/zulip_bots/bots/beeminder/beeminder.py b/zulip_bots/zulip_bots/bots/beeminder/beeminder.py index d15c79658..8cf03754a 100644 --- a/zulip_bots/zulip_bots/bots/beeminder/beeminder.py +++ b/zulip_bots/zulip_bots/bots/beeminder/beeminder.py @@ -4,7 +4,7 @@ import requests from requests.exceptions import ConnectionError -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler help_message = """ You can add datapoints towards your beeminder goals \ @@ -86,7 +86,7 @@ class BeeminderHandler: towards their beeminder goals via zulip """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("beeminder") # Check for valid auth_token auth_token = self.config_info["auth_token"] @@ -102,7 +102,7 @@ def initialize(self, bot_handler: BotHandler) -> None: def usage(self) -> str: return "This plugin allows users to add datapoints towards their Beeminder goals" - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: response = get_beeminder_response(message["content"], self.config_info) bot_handler.send_reply(message, response) diff --git a/zulip_bots/zulip_bots/bots/chessbot/chessbot.py b/zulip_bots/zulip_bots/bots/chessbot/chessbot.py index d1409e83f..66113c5fa 100644 --- a/zulip_bots/zulip_bots/bots/chessbot/chessbot.py +++ b/zulip_bots/zulip_bots/bots/chessbot/chessbot.py @@ -5,7 +5,7 @@ import chess import chess.engine -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler START_REGEX = re.compile("start with other user$") START_COMPUTER_REGEX = re.compile("start as (?Pwhite|black) with computer") @@ -24,7 +24,7 @@ def usage(self) -> str: "Stockfish program on this computer." ) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("chess") try: @@ -36,7 +36,7 @@ def initialize(self, bot_handler: BotHandler) -> None: # runner is testing or knows they won't be using an engine. print("That Stockfish doesn't exist. Continuing.") - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: content = message["content"] if content == "": @@ -77,7 +77,7 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No elif resign_regex_match: self.resign(message, bot_handler, last_fen) - def start(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def start(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: """Starts a game with another user, with the current user as white. Replies to the bot handler. @@ -94,7 +94,7 @@ def start(self, message: Dict[str, str], bot_handler: BotHandler) -> None: bot_handler.storage.put("last_fen", new_board.fen()) def start_computer( - self, message: Dict[str, str], bot_handler: BotHandler, is_white_user: bool + self, message: Dict[str, str], bot_handler: AbstractBotHandler, is_white_user: bool ) -> None: """Starts a game with the computer. Replies to the bot handler. @@ -124,7 +124,7 @@ def start_computer( ) def validate_board( - self, message: Dict[str, str], bot_handler: BotHandler, fen: str + self, message: Dict[str, str], bot_handler: AbstractBotHandler, fen: str ) -> Optional[chess.Board]: """Validates a board based on its FEN string. Replies to the bot handler if there is an error with the board. @@ -148,7 +148,7 @@ def validate_board( def validate_move( self, message: Dict[str, str], - bot_handler: BotHandler, + bot_handler: AbstractBotHandler, last_board: chess.Board, move_san: str, is_computer: object, @@ -181,7 +181,7 @@ def validate_move( return move def check_game_over( - self, message: Dict[str, str], bot_handler: BotHandler, new_board: chess.Board + self, message: Dict[str, str], bot_handler: AbstractBotHandler, new_board: chess.Board ) -> bool: """Checks if a game is over due to - checkmate, @@ -225,7 +225,7 @@ def check_game_over( return False def move( - self, message: Dict[str, str], bot_handler: BotHandler, last_fen: str, move_san: str + self, message: Dict[str, str], bot_handler: AbstractBotHandler, last_fen: str, move_san: str ) -> None: """Makes a move for a user in a game with another user. Replies to the bot handler. @@ -257,7 +257,7 @@ def move( bot_handler.storage.put("last_fen", new_board.fen()) def move_computer( - self, message: Dict[str, str], bot_handler: BotHandler, last_fen: str, move_san: str + self, message: Dict[str, str], bot_handler: AbstractBotHandler, last_fen: str, move_san: str ) -> None: """Preforms a move for a user in a game with the computer and then makes the computer's move. Replies to the bot handler. Unlike `move`, @@ -307,7 +307,7 @@ def move_computer( bot_handler.storage.put("last_fen", new_board_after_computer_move.fen()) def move_computer_first( - self, message: Dict[str, str], bot_handler: BotHandler, last_fen: str + self, message: Dict[str, str], bot_handler: AbstractBotHandler, last_fen: str ) -> None: """Preforms a move for the computer without having the user go first in a game with the computer. Replies to the bot handler. Like @@ -346,7 +346,7 @@ def move_computer_first( # `bot_handler`'s `storage` only accepts `str` values. bot_handler.storage.put("is_with_computer", str(True)) - def resign(self, message: Dict[str, str], bot_handler: BotHandler, last_fen: str) -> None: + def resign(self, message: Dict[str, str], bot_handler: AbstractBotHandler, last_fen: str) -> None: """Resigns the game for the current player. Parameters: diff --git a/zulip_bots/zulip_bots/bots/converter/converter.py b/zulip_bots/zulip_bots/bots/converter/converter.py index d8725b059..dc33495f7 100644 --- a/zulip_bots/zulip_bots/bots/converter/converter.py +++ b/zulip_bots/zulip_bots/bots/converter/converter.py @@ -5,7 +5,7 @@ from typing import Any, Dict, List from zulip_bots.bots.converter import utils -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler def is_float(value: Any) -> bool: @@ -48,12 +48,12 @@ def usage(self) -> str: all supported units. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: bot_response = get_bot_converter_response(message, bot_handler) bot_handler.send_reply(message, bot_response) -def get_bot_converter_response(message: Dict[str, str], bot_handler: BotHandler) -> str: +def get_bot_converter_response(message: Dict[str, str], bot_handler: AbstractBotHandler) -> str: content = message["content"] words = content.lower().split() diff --git a/zulip_bots/zulip_bots/bots/define/define.py b/zulip_bots/zulip_bots/bots/define/define.py index 073afb2e1..2d9a4d9be 100644 --- a/zulip_bots/zulip_bots/bots/define/define.py +++ b/zulip_bots/zulip_bots/bots/define/define.py @@ -6,7 +6,7 @@ import html2text import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class DefineHandler: @@ -27,7 +27,7 @@ def usage(self) -> str: messages with @mention-bot. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: original_content = message["content"].strip() bot_response = self.get_bot_define_response(original_content) diff --git a/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py b/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py index 592180ba0..d686732c5 100644 --- a/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py +++ b/zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py @@ -5,7 +5,7 @@ import apiai -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler help_message = """DialogFlow bot This bot will interact with dialogflow bots. @@ -45,7 +45,7 @@ class DialogFlowHandler: DialogFlow bots to zulip """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("dialogflow") def usage(self) -> str: @@ -54,7 +54,7 @@ def usage(self) -> str: DialogFlow bots to zulip """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: result = get_bot_result(message["content"], self.config_info, message["sender_id"]) bot_handler.send_reply(message, result) diff --git a/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py b/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py index 930ac0ea4..83ee63d8a 100644 --- a/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py +++ b/zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py @@ -3,7 +3,7 @@ from dropbox import Dropbox -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler URL = "[{name}](https://www.dropbox.com/home{path})" @@ -14,7 +14,7 @@ class DropboxHandler: between zulip and your dropbox account. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("dropbox_share") self.ACCESS_TOKEN = self.config_info.get("access_token") self.client = Dropbox(self.ACCESS_TOKEN) @@ -22,7 +22,7 @@ def initialize(self, bot_handler: BotHandler) -> None: def usage(self) -> str: return get_help() - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: command = message["content"] if command == "": command = "help" diff --git a/zulip_bots/zulip_bots/bots/encrypt/encrypt.py b/zulip_bots/zulip_bots/bots/encrypt/encrypt.py index 9d08a0440..08c732966 100755 --- a/zulip_bots/zulip_bots/bots/encrypt/encrypt.py +++ b/zulip_bots/zulip_bots/bots/encrypt/encrypt.py @@ -1,6 +1,6 @@ from typing import Dict -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler def encrypt(text: str) -> str: @@ -34,7 +34,7 @@ def usage(self) -> str: Feeding encrypted messages into the bot decrypts them. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: bot_response = self.get_bot_encrypt_response(message) bot_handler.send_reply(message, bot_response) diff --git a/zulip_bots/zulip_bots/bots/file_uploader/file_uploader.py b/zulip_bots/zulip_bots/bots/file_uploader/file_uploader.py index 96908037b..d259bbfa9 100644 --- a/zulip_bots/zulip_bots/bots/file_uploader/file_uploader.py +++ b/zulip_bots/zulip_bots/bots/file_uploader/file_uploader.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Dict -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class FileUploaderHandler: @@ -13,7 +13,7 @@ def usage(self) -> str: "\n- @uploader help : Display help message" ) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: HELP_STR = ( "Use this bot with any of the following commands:" "\n* `@uploader ` : Upload a file, where `` is the path to the file" diff --git a/zulip_bots/zulip_bots/bots/flock/flock.py b/zulip_bots/zulip_bots/bots/flock/flock.py index 69485e03f..614aa1473 100644 --- a/zulip_bots/zulip_bots/bots/flock/flock.py +++ b/zulip_bots/zulip_bots/bots/flock/flock.py @@ -4,7 +4,7 @@ import requests from requests.exceptions import ConnectionError -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler USERS_LIST_URL = "https://api.flock.co/v1/roster.listContacts" SEND_MESSAGE_URL = "https://api.flock.co/v1/chat.sendMessage" @@ -96,14 +96,14 @@ class FlockHandler: flock user without having to leave Zulip. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("flock") def usage(self) -> str: return """Hello from Flock Bot. You can send messages to any Flock user right from Zulip.""" - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: response = get_flock_bot_response(message["content"], self.config_info) bot_handler.send_reply(message, response) diff --git a/zulip_bots/zulip_bots/bots/followup/followup.py b/zulip_bots/zulip_bots/bots/followup/followup.py index 47c3c54f3..39181c579 100644 --- a/zulip_bots/zulip_bots/bots/followup/followup.py +++ b/zulip_bots/zulip_bots/bots/followup/followup.py @@ -1,7 +1,7 @@ # See readme.md for instructions on running this code. from typing import Dict -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class FollowupHandler: @@ -26,11 +26,11 @@ def usage(self) -> str: called "followup" that your API user can send to. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("followup", optional=False) self.stream = self.config_info.get("stream", "followup") - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: if message["content"] == "": bot_response = ( "Please specify the message you want to send to followup stream after @mention-bot" diff --git a/zulip_bots/zulip_bots/bots/front/front.py b/zulip_bots/zulip_bots/bots/front/front.py index 2f89c91f7..8b062fa8c 100644 --- a/zulip_bots/zulip_bots/bots/front/front.py +++ b/zulip_bots/zulip_bots/bots/front/front.py @@ -3,7 +3,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class FrontHandler: @@ -24,7 +24,7 @@ def usage(self) -> str: Front Bot, `front.conf` must be set up. See `doc.md` for more details. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: config = bot_handler.get_config_info("front") api_key = config.get("api_key") if not api_key: @@ -32,14 +32,14 @@ def initialize(self, bot_handler: BotHandler) -> None: self.auth = "Bearer " + api_key - def help(self, bot_handler: BotHandler) -> str: + def help(self, bot_handler: AbstractBotHandler) -> str: response = "" for command, description in self.COMMANDS: response += f"`{command}` {description}\n" return response - def archive(self, bot_handler: BotHandler) -> str: + def archive(self, bot_handler: AbstractBotHandler) -> str: response = requests.patch( self.FRONT_API.format(self.conversation_id), headers={"Authorization": self.auth}, @@ -51,7 +51,7 @@ def archive(self, bot_handler: BotHandler) -> str: return "Conversation was archived." - def delete(self, bot_handler: BotHandler) -> str: + def delete(self, bot_handler: AbstractBotHandler) -> str: response = requests.patch( self.FRONT_API.format(self.conversation_id), headers={"Authorization": self.auth}, @@ -63,7 +63,7 @@ def delete(self, bot_handler: BotHandler) -> str: return "Conversation was deleted." - def spam(self, bot_handler: BotHandler) -> str: + def spam(self, bot_handler: AbstractBotHandler) -> str: response = requests.patch( self.FRONT_API.format(self.conversation_id), headers={"Authorization": self.auth}, @@ -75,7 +75,7 @@ def spam(self, bot_handler: BotHandler) -> str: return "Conversation was marked as spam." - def restore(self, bot_handler: BotHandler) -> str: + def restore(self, bot_handler: AbstractBotHandler) -> str: response = requests.patch( self.FRONT_API.format(self.conversation_id), headers={"Authorization": self.auth}, @@ -87,7 +87,7 @@ def restore(self, bot_handler: BotHandler) -> str: return "Conversation was restored." - def comment(self, bot_handler: BotHandler, **kwargs: Any) -> str: + def comment(self, bot_handler: AbstractBotHandler, **kwargs: Any) -> str: response = requests.post( self.FRONT_API.format(self.conversation_id) + "/comments", headers={"Authorization": self.auth}, @@ -99,7 +99,7 @@ def comment(self, bot_handler: BotHandler, **kwargs: Any) -> str: return "Comment was sent." - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: command = message["content"] result = re.search(self.CNV_ID_REGEXP, message["subject"]) diff --git a/zulip_bots/zulip_bots/bots/giphy/giphy.py b/zulip_bots/zulip_bots/bots/giphy/giphy.py index a0a5a421e..a870c540a 100644 --- a/zulip_bots/zulip_bots/bots/giphy/giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/giphy.py @@ -5,7 +5,7 @@ from requests.exceptions import ConnectionError, HTTPError from zulip_bots.custom_exceptions import ConfigValidationError -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler GIPHY_TRANSLATE_API = "http://api.giphy.com/v1/gifs/translate" GIPHY_RANDOM_API = "http://api.giphy.com/v1/gifs/random" @@ -44,10 +44,10 @@ def validate_config(config_info: Dict[str, str]) -> None: ) raise ConfigValidationError(error_message) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("giphy") - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: bot_response = get_bot_giphy_response(message, bot_handler, self.config_info) bot_handler.send_reply(message, bot_response) @@ -82,7 +82,7 @@ def get_url_gif_giphy(keyword: str, api_key: str) -> Union[int, str]: def get_bot_giphy_response( - message: Dict[str, str], bot_handler: BotHandler, config_info: Dict[str, str] + message: Dict[str, str], bot_handler: AbstractBotHandler, config_info: Dict[str, str] ) -> str: # Each exception has a specific reply should "gif_url" return a number. # The bot will post the appropriate message for the error. diff --git a/zulip_bots/zulip_bots/bots/github_detail/github_detail.py b/zulip_bots/zulip_bots/bots/github_detail/github_detail.py index 726e50be0..b24a9c30e 100644 --- a/zulip_bots/zulip_bots/bots/github_detail/github_detail.py +++ b/zulip_bots/zulip_bots/bots/github_detail/github_detail.py @@ -4,7 +4,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class GithubHandler: @@ -16,7 +16,7 @@ class GithubHandler: GITHUB_ISSUE_URL_TEMPLATE = "https://api.github.com/repos/{owner}/{repo}/issues/{id}" HANDLE_MESSAGE_REGEX = re.compile(r"(?:([\w-]+)\/)?([\w-]+)?#(\d+)") - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("github_detail", optional=True) self.owner = self.config_info.get("owner", False) self.repo = self.config_info.get("repo", False) @@ -75,7 +75,7 @@ def get_owner_and_repo(self, issue_pr: Any) -> Tuple[str, str]: repo = self.repo return (owner, repo) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: # Send help message if message["content"] == "help": bot_handler.send_reply(message, self.usage()) diff --git a/zulip_bots/zulip_bots/bots/google_search/google_search.py b/zulip_bots/zulip_bots/bots/google_search/google_search.py index dc981d5e9..c1b44eabf 100644 --- a/zulip_bots/zulip_bots/bots/google_search/google_search.py +++ b/zulip_bots/zulip_bots/bots/google_search/google_search.py @@ -5,7 +5,7 @@ import requests from bs4 import BeautifulSoup -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler def google_search(keywords: str) -> List[Dict[str, str]]: @@ -83,7 +83,7 @@ def usage(self) -> str: @mentioned-bot. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: original_content = message["content"] result = get_google_result(original_content) bot_handler.send_reply(message, result) diff --git a/zulip_bots/zulip_bots/bots/helloworld/helloworld.py b/zulip_bots/zulip_bots/bots/helloworld/helloworld.py index 79e9437fb..964b53686 100644 --- a/zulip_bots/zulip_bots/bots/helloworld/helloworld.py +++ b/zulip_bots/zulip_bots/bots/helloworld/helloworld.py @@ -2,7 +2,7 @@ from typing import Any, Dict -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class HelloWorldHandler: @@ -15,7 +15,7 @@ def usage(self) -> str: sophisticated, bots. """ - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: content = "beep boop" # type: str bot_handler.send_reply(message, content) diff --git a/zulip_bots/zulip_bots/bots/help/help.py b/zulip_bots/zulip_bots/bots/help/help.py index 1d95112f9..860f3e098 100644 --- a/zulip_bots/zulip_bots/bots/help/help.py +++ b/zulip_bots/zulip_bots/bots/help/help.py @@ -1,7 +1,7 @@ # See readme.md for instructions on running this code. from typing import Dict -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class HelpHandler: @@ -15,7 +15,7 @@ def usage(self) -> str: your Zulip instance. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: help_content = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip" bot_handler.send_reply(message, help_content) diff --git a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py index a84346f34..c9bf24a51 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py +++ b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py @@ -4,7 +4,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler API_BASE_URL = "https://beta.idonethis.com/api/v2" @@ -155,7 +155,7 @@ def create_entry(message: str) -> str: class IDoneThisHandler: - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: global api_key, default_team self.config_info = bot_handler.get_config_info("idonethis") if "api_key" in self.config_info: @@ -215,7 +215,7 @@ def usage(self) -> str: + default_team_message ) - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: bot_handler.send_reply(message, self.get_response(message)) def get_response(self, message: Dict[str, Any]) -> str: diff --git a/zulip_bots/zulip_bots/bots/incident/incident.py b/zulip_bots/zulip_bots/bots/incident/incident.py index f4f891969..42268794f 100644 --- a/zulip_bots/zulip_bots/bots/incident/incident.py +++ b/zulip_bots/zulip_bots/bots/incident/incident.py @@ -2,7 +2,7 @@ import re from typing import Any, Dict, Tuple -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler QUESTION = "How should we handle this?" @@ -28,7 +28,7 @@ def usage(self) -> str: glue code here should be pretty portable. """ - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: query = message["content"] if query.startswith("new "): start_new_incident(query, message, bot_handler) @@ -46,7 +46,7 @@ def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> No bot_handler.send_reply(message, bot_response) -def start_new_incident(query: str, message: Dict[str, Any], bot_handler: BotHandler) -> None: +def start_new_incident(query: str, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: # Here is where we would enter the incident in some sort of backend # system. We just simulate everything by having an incident id that # we generate here. diff --git a/zulip_bots/zulip_bots/bots/incrementor/incrementor.py b/zulip_bots/zulip_bots/bots/incrementor/incrementor.py index ea42a08ba..7d7858132 100644 --- a/zulip_bots/zulip_bots/bots/incrementor/incrementor.py +++ b/zulip_bots/zulip_bots/bots/incrementor/incrementor.py @@ -2,7 +2,7 @@ from typing import Dict -from zulip_bots.lib import BotHandler, use_storage +from zulip_bots.lib import AbstractBotHandler, use_storage class IncrementorHandler: @@ -19,13 +19,13 @@ def usage(self) -> str: is @-mentioned, this number will be incremented in the same message. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: storage = bot_handler.storage if not storage.contains("number") or not storage.contains("message_id"): storage.put("number", 0) storage.put("message_id", None) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: with use_storage(bot_handler.storage, ["number"]) as storage: num = storage.get("number") diff --git a/zulip_bots/zulip_bots/bots/jira/jira.py b/zulip_bots/zulip_bots/bots/jira/jira.py index 7c3c81cf3..937baf47f 100644 --- a/zulip_bots/zulip_bots/bots/jira/jira.py +++ b/zulip_bots/zulip_bots/bots/jira/jira.py @@ -4,7 +4,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler GET_REGEX = re.compile('get "(?P.+)"$') CREATE_REGEX = re.compile( @@ -153,7 +153,7 @@ def usage(self) -> str: Jira Bot, `jira.conf` must be set up. See `doc.md` for more details. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: config = bot_handler.get_config_info("jira") username = config.get("username") @@ -205,7 +205,7 @@ def jql_search(self, jql_query: str) -> str: return response - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: content = message.get("content") response = "" diff --git a/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py b/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py index 39707df16..3bc96bd6b 100644 --- a/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py +++ b/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py @@ -3,7 +3,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class LinkShortenerHandler: @@ -18,11 +18,11 @@ def usage(self) -> str: "`key` must be set in `link_shortener.conf`." ) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("link_shortener") self.check_api_key(bot_handler) - def check_api_key(self, bot_handler: BotHandler) -> None: + def check_api_key(self, bot_handler: AbstractBotHandler) -> None: test_request_data = self.call_link_shorten_service("www.youtube.com/watch") # type: Any try: if self.is_invalid_token_error(test_request_data): @@ -38,7 +38,7 @@ def is_invalid_token_error(self, response_json: Any) -> bool: and response_json["status_txt"] == "INVALID_ARG_ACCESS_TOKEN" ) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: REGEX_STR = ( r"(" r"(?:http|https):\/\/" # This allows for the HTTP or HTTPS diff --git a/zulip_bots/zulip_bots/bots/mention/mention.py b/zulip_bots/zulip_bots/bots/mention/mention.py index 253dcc5ce..62f710d66 100644 --- a/zulip_bots/zulip_bots/bots/mention/mention.py +++ b/zulip_bots/zulip_bots/bots/mention/mention.py @@ -4,18 +4,18 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class MentionHandler: - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("mention") self.access_token = self.config_info["access_token"] self.account_id = "" self.check_access_token(bot_handler) - def check_access_token(self, bot_handler: BotHandler) -> None: + def check_access_token(self, bot_handler: AbstractBotHandler) -> None: test_query_header = { "Authorization": "Bearer " + self.access_token, "Accept-Version": "1.15", @@ -43,7 +43,7 @@ def usage(self) -> str: Version 1.00 """ - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: message["content"] = message["content"].strip() if message["content"].lower() == "help": diff --git a/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py b/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py index 0aab18846..20771c110 100644 --- a/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py +++ b/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py @@ -2,7 +2,7 @@ from typing import Dict from zulip_bots.bots.monkeytestit.lib import parse -from zulip_bots.lib import BotHandler, NoBotConfigException +from zulip_bots.lib import AbstractBotHandler, NoBotConfigException class MonkeyTestitBot: @@ -17,7 +17,7 @@ def usage(self): "Check doc.md for more options and setup instructions." ) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: try: self.config = bot_handler.get_config_info("monkeytestit") except NoBotConfigException: @@ -47,7 +47,7 @@ def initialize(self, bot_handler: BotHandler) -> None: " your api_key value and try again." ) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: content = message["content"] response = parse.execute(content, self.api_key) diff --git a/zulip_bots/zulip_bots/bots/salesforce/salesforce.py b/zulip_bots/zulip_bots/bots/salesforce/salesforce.py index 87fad94db..d151d9236 100644 --- a/zulip_bots/zulip_bots/bots/salesforce/salesforce.py +++ b/zulip_bots/zulip_bots/bots/salesforce/salesforce.py @@ -7,7 +7,7 @@ import simple_salesforce from zulip_bots.bots.salesforce.utils import commands, default_query, link_query, object_types -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler base_help_text = """Salesforce bot This bot can do simple salesforce query requests @@ -161,7 +161,7 @@ def get_salesforce_response(self, content: str) -> str: return "Usage: {} [arguments]".format(command["template"]) return get_help_text() - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("salesforce") try: self.sf = simple_salesforce.Salesforce( @@ -172,7 +172,7 @@ def initialize(self, bot_handler: BotHandler) -> None: except simple_salesforce.exceptions.SalesforceAuthenticationFailed as err: bot_handler.quit(f"Failed to log in to Salesforce. {err.code} {err.message}") - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: try: bot_response = self.get_salesforce_response(message["content"]) bot_handler.send_reply(message, bot_response) diff --git a/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py b/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py index 6315001b8..70aeb77fb 100644 --- a/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py +++ b/zulip_bots/zulip_bots/bots/stack_overflow/stack_overflow.py @@ -3,7 +3,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler # See readme.md for instructions on running this code. @@ -31,12 +31,12 @@ def usage(self) -> str: should preface query with "@mention-bot". @mention-bot """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: bot_response = self.get_bot_stackoverflow_response(message, bot_handler) bot_handler.send_reply(message, bot_response) def get_bot_stackoverflow_response( - self, message: Dict[str, str], bot_handler: BotHandler + self, message: Dict[str, str], bot_handler: AbstractBotHandler ) -> Optional[str]: """This function returns the URLs of the requested topic.""" diff --git a/zulip_bots/zulip_bots/bots/susi/susi.py b/zulip_bots/zulip_bots/bots/susi/susi.py index ab36122c1..7c984303d 100644 --- a/zulip_bots/zulip_bots/bots/susi/susi.py +++ b/zulip_bots/zulip_bots/bots/susi/susi.py @@ -2,7 +2,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class SusiHandler: @@ -38,7 +38,7 @@ def usage(self) -> str: ``` """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: msg = message["content"] if msg == "help" or msg == "": bot_handler.send_reply(message, self.usage()) diff --git a/zulip_bots/zulip_bots/bots/trello/trello.py b/zulip_bots/zulip_bots/bots/trello/trello.py index effb6e44f..3dcee300e 100644 --- a/zulip_bots/zulip_bots/bots/trello/trello.py +++ b/zulip_bots/zulip_bots/bots/trello/trello.py @@ -2,7 +2,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler supported_commands = [ ("help", "Get the bot usage information."), @@ -18,7 +18,7 @@ class TrelloHandler: - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("trello") self.api_key = self.config_info["api_key"] self.access_token = self.config_info["access_token"] @@ -28,7 +28,7 @@ def initialize(self, bot_handler: BotHandler) -> None: self.check_access_token(bot_handler) - def check_access_token(self, bot_handler: BotHandler) -> None: + def check_access_token(self, bot_handler: AbstractBotHandler) -> None: test_query_response = requests.get( f"https://api.trello.com/1/members/{self.user_name}/", params=self.auth_params ) @@ -43,7 +43,7 @@ def usage(self) -> str: Use `list-commands` to get information about the supported commands. """ - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: content = message["content"].strip().split() if content == []: diff --git a/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py b/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py index ac444f16b..2601667ec 100644 --- a/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py +++ b/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py @@ -6,7 +6,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class NotAvailableException(Exception): @@ -23,7 +23,7 @@ def usage(self) -> str: This plugin will give users a trivia question from the open trivia database at opentdb.com.""" - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: query = message["content"] if query == "new": try: @@ -59,11 +59,11 @@ def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> No bot_handler.send_reply(message, bot_response) -def get_quiz_from_id(quiz_id: str, bot_handler: BotHandler) -> str: +def get_quiz_from_id(quiz_id: str, bot_handler: AbstractBotHandler) -> str: return bot_handler.storage.get(quiz_id) -def start_new_quiz(message: Dict[str, Any], bot_handler: BotHandler) -> None: +def start_new_quiz(message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: quiz = get_trivia_quiz() quiz_id = generate_quiz_id(bot_handler.storage) bot_response = format_quiz_for_markdown(quiz_id, quiz) @@ -211,7 +211,7 @@ def format_quiz_for_markdown(quiz_id: str, quiz: Dict[str, Any]) -> str: return content -def update_quiz(quiz: Dict[str, Any], quiz_id: str, bot_handler: BotHandler) -> None: +def update_quiz(quiz: Dict[str, Any], quiz_id: str, bot_handler: AbstractBotHandler) -> None: bot_handler.storage.put(quiz_id, json.dumps(quiz)) @@ -227,7 +227,7 @@ def build_response(is_correct: bool, num_answers: int) -> str: def handle_answer( - quiz: Dict[str, Any], option: str, quiz_id: str, bot_handler: BotHandler, sender_name: str + quiz: Dict[str, Any], option: str, quiz_id: str, bot_handler: AbstractBotHandler, sender_name: str ) -> Tuple[bool, str]: answer = quiz["answers"][quiz["correct_letter"]] is_new_answer = option not in quiz["answered_options"] diff --git a/zulip_bots/zulip_bots/bots/twitpost/twitpost.py b/zulip_bots/zulip_bots/bots/twitpost/twitpost.py index ad6b25846..2a77241b3 100644 --- a/zulip_bots/zulip_bots/bots/twitpost/twitpost.py +++ b/zulip_bots/zulip_bots/bots/twitpost/twitpost.py @@ -2,7 +2,7 @@ import tweepy -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class TwitpostBot: @@ -21,7 +21,7 @@ def usage(self) -> str: " * @twitpost tweet hey batman\n" ) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("twitter") auth = tweepy.OAuthHandler( self.config_info["consumer_key"], self.config_info["consumer_secret"] @@ -31,7 +31,7 @@ def initialize(self, bot_handler: BotHandler) -> None: ) self.api = tweepy.API(auth, parser=tweepy.parsers.JSONParser()) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: content = message["content"] if content.strip() == "": diff --git a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py b/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py index a514e3101..5d577fcda 100644 --- a/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py +++ b/zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py @@ -4,7 +4,7 @@ import re from typing import Any, Dict, List, Set, Tuple, Union -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class VirtualFsHandler: @@ -16,7 +16,7 @@ class VirtualFsHandler: def usage(self) -> str: return get_help() - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: command = message["content"] if command == "": command = "help" diff --git a/zulip_bots/zulip_bots/bots/weather/weather.py b/zulip_bots/zulip_bots/bots/weather/weather.py index 46540fac7..f197a750e 100644 --- a/zulip_bots/zulip_bots/bots/weather/weather.py +++ b/zulip_bots/zulip_bots/bots/weather/weather.py @@ -3,18 +3,18 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler api_url = "http://api.openweathermap.org/data/2.5/weather" class WeatherHandler: - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.api_key = bot_handler.get_config_info("weather")["key"] self.response_pattern = "Weather in {}, {}:\n{:.2f} F / {:.2f} C\n{}" self.check_api_key(bot_handler) - def check_api_key(self, bot_handler: BotHandler) -> None: + def check_api_key(self, bot_handler: AbstractBotHandler) -> None: api_params = dict(q="nyc", APPID=self.api_key) test_response = requests.get(api_url, params=api_params) try: @@ -29,7 +29,7 @@ def usage(self) -> str: This plugin will give info about weather in a specified city """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: help_content = """ This bot returns weather info for specified city. You specify city in the following format: diff --git a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py index ff707fbf2..44def7ed8 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py @@ -3,7 +3,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler # See readme.md for instructions on running this code. @@ -33,11 +33,11 @@ def usage(self) -> str: should preface searches with "@mention-bot". @mention-bot """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: bot_response = self.get_bot_wiki_response(message, bot_handler) bot_handler.send_reply(message, bot_response) - def get_bot_wiki_response(self, message: Dict[str, str], bot_handler: BotHandler) -> str: + def get_bot_wiki_response(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> str: """This function returns the URLs of the requested topic.""" help_text = "Please enter your search term after {}" diff --git a/zulip_bots/zulip_bots/bots/witai/witai.py b/zulip_bots/zulip_bots/bots/witai/witai.py index 46de58236..29faf603a 100644 --- a/zulip_bots/zulip_bots/bots/witai/witai.py +++ b/zulip_bots/zulip_bots/bots/witai/witai.py @@ -6,7 +6,7 @@ import wit -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class WitaiHandler: @@ -16,7 +16,7 @@ def usage(self) -> str: Wit.ai bot, `witai.conf` must be set up. See `doc.md` for more details. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: config = bot_handler.get_config_info("witai") token = config.get("token") @@ -41,7 +41,7 @@ def initialize(self, bot_handler: BotHandler) -> None: self.client = wit.Wit(token) - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: if message["content"] == "" or message["content"] == "help": bot_handler.send_reply(message, self.help_message) return diff --git a/zulip_bots/zulip_bots/bots/xkcd/xkcd.py b/zulip_bots/zulip_bots/bots/xkcd/xkcd.py index ee1b607ce..37f2d5346 100644 --- a/zulip_bots/zulip_bots/bots/xkcd/xkcd.py +++ b/zulip_bots/zulip_bots/bots/xkcd/xkcd.py @@ -4,7 +4,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler XKCD_TEMPLATE_URL = "https://xkcd.com/%s/info.0.json" LATEST_XKCD_URL = "https://xkcd.com/info.0.json" @@ -36,7 +36,7 @@ def usage(self) -> str: ``, e.g `@mention-bot 1234`. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: quoted_name = bot_handler.identity().mention xkcd_bot_response = get_xkcd_bot_response(message, quoted_name) bot_handler.send_reply(message, xkcd_bot_response) diff --git a/zulip_bots/zulip_bots/bots/yoda/yoda.py b/zulip_bots/zulip_bots/bots/yoda/yoda.py index 4e1cfc5d5..0bf2d2f17 100644 --- a/zulip_bots/zulip_bots/bots/yoda/yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/yoda.py @@ -5,7 +5,7 @@ import requests -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler HELP_MESSAGE = """ This bot allows users to translate a sentence into @@ -36,7 +36,7 @@ class YodaSpeakHandler: It looks for messages starting with '@mention-bot'. """ - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.api_key = bot_handler.get_config_info("yoda")["api_key"] def usage(self) -> str: @@ -53,7 +53,7 @@ def usage(self) -> str: @mention-bot You will learn how to speak like me someday. """ - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: self.handle_input(message, bot_handler) def send_to_yoda_api(self, sentence: str) -> str: @@ -89,7 +89,7 @@ def format_input(self, original_content: str) -> str: sentence = message_content.replace(" ", "+") return sentence - def handle_input(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_input(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: original_content = message["content"] if self.is_help(original_content) or (original_content == ""): @@ -116,7 +116,7 @@ def handle_input(self, message: Dict[str, str], bot_handler: BotHandler) -> None bot_handler.send_reply(message, reply_message) def send_message( - self, bot_handler: BotHandler, message: str, stream: str, subject: str + self, bot_handler: AbstractBotHandler, message: str, stream: str, subject: str ) -> None: # function for sending a message bot_handler.send_message(dict(type="stream", to=stream, subject=subject, content=message)) diff --git a/zulip_bots/zulip_bots/bots/youtube/youtube.py b/zulip_bots/zulip_bots/bots/youtube/youtube.py index 7e58300df..4044a69ae 100644 --- a/zulip_bots/zulip_bots/bots/youtube/youtube.py +++ b/zulip_bots/zulip_bots/bots/youtube/youtube.py @@ -4,7 +4,7 @@ import requests from requests.exceptions import ConnectionError, HTTPError -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler commands_list = ("list", "top", "help") @@ -28,7 +28,7 @@ def usage(self) -> str: " * @mention-bot list funny dogs" ) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.config_info = bot_handler.get_config_info("youtube") # Check if API key is valid. If it is not valid, don't run the bot. try: @@ -43,7 +43,7 @@ def initialize(self, bot_handler: BotHandler) -> None: except ConnectionError: logging.warning("Bad connection") - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: if message["content"] == "" or message["content"] == "help": bot_handler.send_reply(message, self.help_content) diff --git a/zulip_bots/zulip_bots/game_handler.py b/zulip_bots/zulip_bots/game_handler.py index 437749611..72a6954bb 100644 --- a/zulip_bots/zulip_bots/game_handler.py +++ b/zulip_bots/zulip_bots/game_handler.py @@ -5,7 +5,7 @@ from copy import deepcopy from typing import Any, Dict, List, Tuple -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler class BadMoveException(Exception): @@ -206,13 +206,13 @@ def usage(self) -> str: """ ) - def initialize(self, bot_handler: BotHandler) -> None: + def initialize(self, bot_handler: AbstractBotHandler) -> None: self.bot_handler = bot_handler self.get_user_cache() self.email = self.bot_handler.email self.full_name = self.bot_handler.full_name - def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, Any], bot_handler: AbstractBotHandler) -> None: try: self.bot_handler = bot_handler content = message["content"].strip() diff --git a/zulip_botserver/tests/test_server.py b/zulip_botserver/tests/test_server.py index 3d94db579..2a9d738bd 100644 --- a/zulip_botserver/tests/test_server.py +++ b/zulip_botserver/tests/test_server.py @@ -9,7 +9,7 @@ from unittest import mock from zulip_bots.finder import metadata -from zulip_bots.lib import BotHandler +from zulip_bots.lib import AbstractBotHandler from zulip_botserver import server from zulip_botserver.input_parameters import parse_args @@ -18,7 +18,7 @@ class BotServerTests(BotServerTestCase): class MockMessageHandler: - def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> None: + def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: assert message == {"key": "test message"} class MockLibModule: From 2bdb1027ce4dca971e26c40090f09d6712c968ac Mon Sep 17 00:00:00 2001 From: Bhavesh Jain Date: Mon, 20 Mar 2023 17:40:53 +0530 Subject: [PATCH 3/3] Resolved error in zephyr_mirror.py as a function is missing return type --- zulip/integrations/zephyr/zephyr_mirror.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zulip/integrations/zephyr/zephyr_mirror.py b/zulip/integrations/zephyr/zephyr_mirror.py index 2bb0b9910..d164bb623 100755 --- a/zulip/integrations/zephyr/zephyr_mirror.py +++ b/zulip/integrations/zephyr/zephyr_mirror.py @@ -43,7 +43,7 @@ async def run_shard(shard: str) -> int: process = await asyncio.create_subprocess_exec(*args, f"--shard={shard}") return await process.wait() - async def run_shards(): + async def run_shards() -> None: for coro in asyncio.as_completed(map(run_shard, shards)): await coro print("A mirroring shard died!")