init: Initial commit
This commit is contained in:
48
bot.py
48
bot.py
@@ -1,11 +1,12 @@
|
||||
from typing import Tuple
|
||||
|
||||
from botbuilder.core import ActivityHandler, TurnContext
|
||||
from botbuilder.schema import ChannelAccount, Mention
|
||||
from botbuilder.schema import ChannelAccount, Mention, Activity, ActivityTypes
|
||||
|
||||
from config import DefaultConfig
|
||||
|
||||
from commands.webhook import WebhookManager
|
||||
from commands.webhook import WebhookCommand
|
||||
from commands.test import TestCommand
|
||||
from modules.database import DatabaseManager
|
||||
|
||||
import re
|
||||
@@ -16,7 +17,7 @@ class WebhookBot(ActivityHandler):
|
||||
def __init__(self, config: DefaultConfig, database: DatabaseManager) -> None:
|
||||
super().__init__()
|
||||
self.command_prefix = DefaultConfig.COMMAND_PREFIX
|
||||
self.webhook_manager = WebhookManager(database)
|
||||
self.webhook_manager = WebhookCommand(database)
|
||||
self.database = database
|
||||
|
||||
def is_command(self, text: str) -> tuple[bool, str | None]:
|
||||
@@ -44,9 +45,7 @@ class WebhookBot(ActivityHandler):
|
||||
# Check entities for mentions
|
||||
if activity.entities is None:
|
||||
return False
|
||||
|
||||
print(f"[Bot] Checking mentions, bot_id: {bot_id}, entities: {len(activity.entities)}")
|
||||
|
||||
|
||||
for entity in activity.entities:
|
||||
if entity.type == "mention":
|
||||
try:
|
||||
@@ -54,7 +53,6 @@ class WebhookBot(ActivityHandler):
|
||||
if hasattr(entity, 'mentioned') and entity.mentioned:
|
||||
mentioned_id = getattr(entity.mentioned, 'id', None)
|
||||
if mentioned_id == bot_id:
|
||||
print(f"[Bot] Bot mentioned (via mentioned property)")
|
||||
return True
|
||||
|
||||
# Check properties dict (alternative format)
|
||||
@@ -63,14 +61,10 @@ class WebhookBot(ActivityHandler):
|
||||
if isinstance(props, dict):
|
||||
mentioned = props.get('mentioned', {})
|
||||
if isinstance(mentioned, dict):
|
||||
if mentioned.get('id') == bot_id:
|
||||
print(f"[Bot] Bot mentioned (via properties dict)")
|
||||
if mentioned.get('id') == bot_id: # type: ignore
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"[Bot] Error checking mention entity: {e}")
|
||||
continue
|
||||
|
||||
print(f"[Bot] Bot not mentioned")
|
||||
return False
|
||||
|
||||
def is_channel_conversation(self, turn_context: TurnContext) -> bool:
|
||||
@@ -86,14 +80,22 @@ class WebhookBot(ActivityHandler):
|
||||
# Remove all between <at> and </at>
|
||||
return re.sub(r'<at>.*?</at>', '', text)
|
||||
|
||||
def send_message(self, channel_id: str, message: str) -> None:
|
||||
channel_account = ChannelAccount(id=channel_id)
|
||||
activity = Activity(
|
||||
type=ActivityTypes.message,
|
||||
text=message,
|
||||
channel_id=channel_id,
|
||||
from_property=channel_account
|
||||
)
|
||||
|
||||
|
||||
async def on_command(self, command: str, turn_context: TurnContext) -> None:
|
||||
"""Handle the command."""
|
||||
await turn_context.send_activity(f"Command {command} received.")
|
||||
return
|
||||
|
||||
async def on_members_added_activity(
|
||||
self, members_added: list[ChannelAccount], turn_context: TurnContext
|
||||
) -> None:
|
||||
async def on_members_added_activity(self, members_added: list[ChannelAccount], turn_context: TurnContext) -> None:
|
||||
"""Handle the members added activity."""
|
||||
if turn_context.activity.recipient is None:
|
||||
return
|
||||
@@ -112,13 +114,6 @@ class WebhookBot(ActivityHandler):
|
||||
async def on_message_activity(self, turn_context: TurnContext) -> None:
|
||||
"""Handle the message activity - works for both 1:1 and channel posts."""
|
||||
activity = turn_context.activity
|
||||
print(f"[Bot] on_message_activity called")
|
||||
print(f"[Bot] Activity type: {activity.type}")
|
||||
print(f"[Bot] Channel ID: {activity.channel_id}")
|
||||
print(f"[Bot] Activity text: {activity.text}")
|
||||
print(f"[Bot] Conversation type: {activity.conversation.conversation_type if activity.conversation else 'None'}")
|
||||
print(f"[Bot] From: {activity.from_property.name if activity.from_property else 'None'}")
|
||||
print(f"[Bot] Entities: {activity.entities}")
|
||||
|
||||
# Check if this is a channel conversation
|
||||
is_channel = self.is_channel_conversation(turn_context)
|
||||
@@ -142,6 +137,15 @@ class WebhookBot(ActivityHandler):
|
||||
|
||||
if is_command:
|
||||
match command:
|
||||
case "test":
|
||||
# Get the text behind the command /test <message>
|
||||
# Remove the command from the text
|
||||
message = text.replace(f"/test ", "").strip()
|
||||
if message == "":
|
||||
await turn_context.send_activity(f"Please provide a message for the test command.")
|
||||
return
|
||||
await TestCommand(self.database).handle_test(turn_context, message)
|
||||
return
|
||||
case "webhooks":
|
||||
is_sub_command, sub_command = self.has_sub_command(text)
|
||||
if is_sub_command:
|
||||
|
||||
Reference in New Issue
Block a user