init: Initial commit

This commit is contained in:
Björn Benouarets
2026-01-08 10:10:12 +01:00
parent 7055272793
commit a1eca7baef
17 changed files with 392 additions and 111 deletions

42
commands/test.py Normal file
View File

@@ -0,0 +1,42 @@
from modules.database import DatabaseManager
from botbuilder.core import TurnContext, CardFactory, MessageFactory
from botbuilder.schema import Attachment, Activity, ActivityTypes, ConversationParameters
from aiohttp.web import Response, json_response
import json
from modules.template import TemplateEngine
class TestCommand:
def __init__(self, database: DatabaseManager):
self.database = database
async def handle_test(self, turn_context: TurnContext, message: str) -> None:
try:
activity = turn_context.activity
# conversation_reference = TurnContext.get_conversation_reference(turn_context.activity)
# Check if the channel is a Microsoft Teams channel (msteams) or emulator (for testing)
channel_id_str = activity.channel_id
channel_data = activity.channel_data
# channel_data is a dictionary, access it as such
teams_id = channel_data.get('teamsTeamId') if channel_data and isinstance(channel_data, dict) else None
microsoft_channel_id_str = channel_data.get('teamsChannelId') if channel_data and isinstance(channel_data, dict) else None
if channel_id_str == "emulator":
teams_id = "00000000-0000-0000-0000-000000000000"
microsoft_channel_id_str = "00000000-0000-0000-0000-000000000000"
# Load template and prepare data
template = TemplateEngine("card")
# Create and send the Adaptive Card
card_attachment = CardFactory.adaptive_card(template.generate({"card_type": "Test", "card_title": "Test", "card_content": message}))
# Send the attachment as a reply to the post in the channel
result = await turn_context.send_activity(MessageFactory.attachment(card_attachment))
except Exception as e:
import traceback
traceback.print_exc()
raise

View File

@@ -1,62 +1,61 @@
from modules.database import DatabaseManager
from botbuilder.core import TurnContext, CardFactory, MessageFactory
from botbuilder.schema import Attachment, Activity, ActivityTypes
from botbuilder.schema import Attachment, Activity, ActivityTypes, ConversationParameters
from aiohttp.web import Response, json_response
import json
from typing import cast, Any
class WebhookManager:
from modules.template import TemplateEngine
class WebhookCommand:
def __init__(self, database: DatabaseManager):
self.database = database
def __load_template(self, template_name: str) -> dict:
with open(f"templates/{template_name}.json", "r", encoding="utf-8") as f:
return json.loads(f.read())
def _replace_placeholders(self, obj, data: dict):
"""Recursively replace placeholders in dict/list/string values."""
if isinstance(obj, dict):
return {k: self._replace_placeholders(v, data) for k, v in obj.items()}
elif isinstance(obj, list):
return [self._replace_placeholders(item, data) for item in obj]
elif isinstance(obj, str):
return obj.format(**data)
else:
return obj
def _create_webhooks_card(self, template: dict, data: dict) -> Attachment:
# Replace placeholders in dict structure
filled_template = self._replace_placeholders(template, data)
print(f"Filled template: {filled_template}")
# Type cast: filled_template is guaranteed to be a dict since template is a dict
return CardFactory.adaptive_card(
cast(dict[str, Any], filled_template)
)
def create_webhooks_card(self, template: TemplateEngine, data: dict) -> Attachment:
return CardFactory.adaptive_card(template.generate(data))
async def handle_list_webhooks(self, turn_context: TurnContext) -> None:
try:
activity = turn_context.activity
channel_id = activity.channel_id
# conversation_reference = TurnContext.get_conversation_reference(turn_context.activity)
# Check if the channel is a Microsoft Teams channel (msteams) or emulator (for testing)
if channel_id not in ["msteams", "emulator"]:
await turn_context.send_activity(f"Dieser Befehl ist nur in Microsoft Teams verfügbar.")
return
webhooks = self.database.get_webhooks()
channel_id_str = activity.channel_id
channel_data = activity.channel_data
# channel_data is a dictionary, access it as such
teams_id = channel_data.get('teamsTeamId') if channel_data and isinstance(channel_data, dict) else None
microsoft_channel_id_str = channel_data.get('teamsChannelId') if channel_data and isinstance(channel_data, dict) else None
if channel_id_str == "emulator":
teams_id = "00000000-0000-0000-0000-000000000000"
microsoft_channel_id_str = "00000000-0000-0000-0000-000000000000"
# Get the actual channel UUID from the database
channel_uuid = None
webhook_count = 0
if microsoft_channel_id_str:
try:
from uuid import UUID
# Try to get channel by Microsoft Teams channel ID
microsoft_channel_id = UUID(microsoft_channel_id_str) if isinstance(microsoft_channel_id_str, str) else microsoft_channel_id_str
channel_obj = self.database.get_channel_by_microsoft_channel_id(str(microsoft_channel_id))
if channel_obj:
channel_uuid = str(channel_obj.id)
webhook_count = self.database.count_webhooks_by_channel_id(channel_uuid)
except (ValueError, TypeError) as e:
webhook_count = 0
# Load template and prepare data
template = self.__load_template("webhook-overview-card" if webhooks else "webhook-overview-no-webhooks-card")
data = {"webhook_count": len(webhooks)}
template = TemplateEngine("webhook-overview-card" if webhook_count > 0 else "webhook-overview-no-webhooks-card")
# Create and send the Adaptive Card
card_attachment = self._create_webhooks_card(template, data)
# Send the attachment directly - send_activity will create the Activity with proper fields
card_attachment = self.create_webhooks_card(template, {"webhook_count": webhook_count})
# Send the attachment as a reply to the post in the channel
result = await turn_context.send_activity(MessageFactory.attachment(card_attachment))
print(f"Activity sent successfully, result: {result}")
except Exception as e:
print(f"Error in handle_list_webhooks: {e}")
import traceback
traceback.print_exc()
raise