52 lines
2.5 KiB
Python
52 lines
2.5 KiB
Python
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 WebhookCommand:
|
|
def __init__(self, database: DatabaseManager):
|
|
self.database = database
|
|
|
|
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_str = activity.channel_id
|
|
channel_data = activity.channel_data
|
|
|
|
microsoft_teams_id_str = 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":
|
|
microsoft_teams_id_str = "00000000-0000-0000-0000-000000000000"
|
|
microsoft_channel_id_str = "00000000-0000-0000-0000-000000000000"
|
|
|
|
channel_uuid = None
|
|
webhook_count = 0
|
|
|
|
if microsoft_channel_id_str:
|
|
try:
|
|
from uuid import UUID
|
|
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
|
|
|
|
template = TemplateEngine("webhook-overview-card" if webhook_count > 0 else "webhook-overview-no-webhooks-card")
|
|
card_attachment = self.create_webhooks_card(template, {"webhook_count": webhook_count})
|
|
await turn_context.send_activity(MessageFactory.attachment(card_attachment))
|
|
return
|
|
except Exception as e:
|
|
import traceback
|
|
traceback.print_exc()
|
|
raise
|