init: Initial commit
This commit is contained in:
20
modules/command.py
Normal file
20
modules/command.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from botbuilder.core import TurnContext
|
||||
|
||||
from commands.test import TestCommand
|
||||
from commands.webhook import WebhookCommand
|
||||
from commands.help import HelpCommand
|
||||
from modules.database import DatabaseManager
|
||||
|
||||
class Command:
|
||||
def __init__(self, turn_context: TurnContext, database: DatabaseManager) -> None:
|
||||
self.turn_context = turn_context
|
||||
self.database = database
|
||||
|
||||
async def handle_command(self, command: str, message: str | None = None) -> None:
|
||||
match command:
|
||||
case "test":
|
||||
await TestCommand(self.database).handle_test(self.turn_context, command, message)
|
||||
case "webhooks":
|
||||
await WebhookCommand(self.database).handle_list_webhooks(self.turn_context)
|
||||
case _:
|
||||
await HelpCommand(self.turn_context, self.database).handle_help()
|
||||
@@ -59,24 +59,19 @@ class DatabaseManager:
|
||||
def get_webhooks(self) -> list[Webhook]:
|
||||
return self.__session.query(Webhook).all()
|
||||
|
||||
def count_webhooks(self) -> int:
|
||||
return len(self.get_webhooks())
|
||||
|
||||
def get_webhook_by_id(self, id: str) -> Webhook:
|
||||
webhook = self.__session.query(Webhook).filter(Webhook.id == id).first()
|
||||
if webhook is None:
|
||||
raise ValueError(f"Webhook with id {id} not found")
|
||||
return webhook
|
||||
|
||||
def get_services(self) -> list[Service]:
|
||||
return self.__session.query(Service).all()
|
||||
|
||||
def get_service_by_id(self, id: str) -> Service:
|
||||
service = self.__session.query(Service).filter(Service.id == id).first()
|
||||
if service is None:
|
||||
raise ValueError(f"Service with id {id} not found")
|
||||
return service
|
||||
|
||||
def count_webhooks_by_channel_id(self, channel_id: str | UUID) -> int | None:
|
||||
try:
|
||||
channel_uuid = UUID(channel_id) if isinstance(channel_id, str) else channel_id
|
||||
return len(self.__session.query(Webhook).filter(Webhook.channel_id == channel_uuid).all())
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
def get_channels(self) -> list[Channel]:
|
||||
return self.__session.query(Channel).all()
|
||||
|
||||
@@ -86,22 +81,17 @@ class DatabaseManager:
|
||||
raise ValueError(f"Channel with id {id} not found")
|
||||
return channel
|
||||
|
||||
def get_channel_by_microsoft_channel_id(self, microsoft_channel_id: str) -> Channel | None:
|
||||
"""Get channel by Microsoft Teams channel ID. Returns None if not found."""
|
||||
try:
|
||||
# Convert string to UUID if needed
|
||||
channel_uuid = UUID(microsoft_channel_id) if isinstance(microsoft_channel_id, str) else microsoft_channel_id
|
||||
channel = self.__session.query(Channel).filter(Channel.microsoft_channel_id == channel_uuid).first()
|
||||
return channel
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
def get_channel_by_microsoft_channel_id(self, microsoft_channel_id: str) -> Channel:
|
||||
channel = self.__session.query(Channel).filter(Channel.microsoft_channel_id == microsoft_channel_id).first()
|
||||
if channel is None:
|
||||
raise ValueError(f"Channel with microsoft channel id {microsoft_channel_id} not found")
|
||||
return channel
|
||||
|
||||
def count_webhooks_by_channel_id(self, channel_id: str | UUID) -> int:
|
||||
"""Count webhooks by channel ID. Accepts UUID string or UUID object."""
|
||||
try:
|
||||
# Convert string to UUID if needed
|
||||
channel_uuid = UUID(channel_id) if isinstance(channel_id, str) else channel_id
|
||||
return len(self.__session.query(Webhook).filter(Webhook.channel_id == channel_uuid).all())
|
||||
except (ValueError, TypeError):
|
||||
# If channel_id is not a valid UUID, return 0
|
||||
return 0
|
||||
def get_services(self) -> list[Service]:
|
||||
return self.__session.query(Service).all()
|
||||
|
||||
def get_service_by_id(self, id: str) -> Service:
|
||||
service = self.__session.query(Service).filter(Service.id == id).first()
|
||||
if service is None:
|
||||
raise ValueError(f"Service with id {id} not found")
|
||||
return service
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from botbuilder.schema import Attachment
|
||||
class TemplateEngine:
|
||||
def __init__(self, template_name: str, template_path: str = "templates/") -> None:
|
||||
self.__name = template_name
|
||||
@@ -23,7 +24,22 @@ class TemplateEngine:
|
||||
return obj.format(**data)
|
||||
else:
|
||||
return obj
|
||||
|
||||
def generate(self, data: dict) -> dict[str, Any]:
|
||||
|
||||
def __generate_card(self, data: dict) -> dict[str, Any]:
|
||||
template = self.__load_template()
|
||||
return self.__replace_placeholders(template, data)
|
||||
return self.__replace_placeholders(template, data)
|
||||
|
||||
def generate(self, data: dict) -> dict[str, Any]:
|
||||
return self.__generate_card(data)
|
||||
|
||||
def attachment(self, data: dict) -> Attachment:
|
||||
return Attachment(
|
||||
content_type="application/vnd.microsoft.card.adaptive",
|
||||
content=self.generate(data)
|
||||
)
|
||||
|
||||
def json_dict(self, data: dict) -> dict:
|
||||
return self.generate(data)
|
||||
|
||||
def json(self, data: dict) -> str:
|
||||
return json.dumps(self.generate(data))
|
||||
Reference in New Issue
Block a user