Skip to content

Commit

Permalink
bots: Rename BotHandler to AbstractBotHandler.
Browse files Browse the repository at this point in the history
Fixes #690.
  • Loading branch information
Niloth-p authored and timabbott committed Nov 21, 2024
1 parent fb73220 commit 1d37ed2
Show file tree
Hide file tree
Showing 42 changed files with 155 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Any, Dict

import packaged_helloworld
from zulip_bots.lib import BotHandler
from zulip_bots.lib import AbstractBotHandler

__version__ = packaged_helloworld.__version__

Expand All @@ -17,7 +17,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"
bot_handler.send_reply(message, content)

Expand Down
8 changes: 4 additions & 4 deletions zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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()
Expand All @@ -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 == []:
Expand Down
6 changes: 3 additions & 3 deletions zulip_bots/zulip_bots/bots/beeminder/beeminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -80,7 +80,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"]
Expand All @@ -96,7 +96,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)

Expand Down
26 changes: 14 additions & 12 deletions zulip_bots/zulip_bots/bots/chessbot/chessbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?P<user_color>white|black) with computer")
Expand All @@ -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:
Expand All @@ -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 == "":
Expand Down Expand Up @@ -76,7 +76,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.
Expand All @@ -93,7 +93,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.
Expand Down Expand Up @@ -123,7 +123,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.
Expand All @@ -147,7 +147,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,
Expand Down Expand Up @@ -180,7 +180,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,
Expand Down Expand Up @@ -224,7 +224,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.
Expand Down Expand Up @@ -256,7 +256,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`,
Expand Down Expand Up @@ -306,7 +306,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
Expand Down Expand Up @@ -345,7 +345,9 @@ 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:
Expand Down
6 changes: 3 additions & 3 deletions zulip_bots/zulip_bots/bots/converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -49,12 +49,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()
Expand Down
4 changes: 2 additions & 2 deletions zulip_bots/zulip_bots/bots/define/define.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import html2text
import requests

from zulip_bots.lib import BotHandler
from zulip_bots.lib import AbstractBotHandler


class DefineHandler:
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -47,7 +47,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:
Expand All @@ -56,7 +56,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)

Expand Down
6 changes: 3 additions & 3 deletions zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"

Expand All @@ -14,15 +14,15 @@ 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)

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"
Expand Down
4 changes: 2 additions & 2 deletions zulip_bots/zulip_bots/bots/encrypt/encrypt.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions zulip_bots/zulip_bots/bots/file_uploader/file_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 <local_file_path>` : Upload a file, where `<local_file_path>` is the path to the file"
Expand Down
6 changes: 3 additions & 3 deletions zulip_bots/zulip_bots/bots/flock/flock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -97,14 +97,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)

Expand Down
6 changes: 3 additions & 3 deletions zulip_bots/zulip_bots/bots/followup/followup.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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"
Expand Down
Loading

0 comments on commit 1d37ed2

Please sign in to comment.