import logging
from typing import Optional
from zenml.alerter import BaseAlerter, BaseAlerterStepParameters
class MyAlerter(BaseAlerter):
"""My alerter class."""
def post(
self, message: str, params: Optional[BaseAlerterStepParameters]
) -> bool:
"""Post a message to a chat service."""
try:
# Implement your chat service posting logic here
# e.g., send HTTP request to chat API
logging.info(f"Posting message: {message}")
return True
except Exception as e:
logging.error(f"Failed to post message: {e}")
return False
def ask(
self, question: str, params: Optional[BaseAlerterStepParameters]
) -> bool:
"""Post a message to a chat service and wait for approval."""
try:
# First, post the question
if not self.post(question, params):
return False
# Define default approval/disapproval options
approve_options = ["approve", "yes", "ok", "LGTM"]
disapprove_options = ["reject", "no", "cancel", "stop"]
# Check if custom options are provided in params
if params and hasattr(params, 'approve_msg_options'):
approve_options = params.approve_msg_options
if params and hasattr(params, 'disapprove_msg_options'):
disapprove_options = params.disapprove_msg_options
# Wait for response (implement your chat service polling logic)
# This is a simplified example - you'd implement actual polling
response = self._wait_for_user_response()
if response.lower() in [opt.lower() for opt in approve_options]:
return True
elif response.lower() in [opt.lower() for opt in disapprove_options]:
return False
else:
# Invalid response or timeout
return False
except Exception as e:
print(f"Failed to get approval: {e}")
return False
def _wait_for_user_response(self) -> str:
"""Wait for user response - implement based on your chat service."""
# This is where you'd implement the actual waiting logic
# e.g., polling your chat service API for new messages
return "approve" # Placeholder