init: Initial commit
This commit is contained in:
43
bot.py
43
bot.py
@@ -8,6 +8,7 @@ from config import DefaultConfig
|
||||
from commands.webhook import WebhookCommand
|
||||
from commands.test import TestCommand
|
||||
from modules.database import DatabaseManager
|
||||
from modules.command import Command
|
||||
|
||||
import re
|
||||
|
||||
@@ -20,11 +21,11 @@ class WebhookBot(ActivityHandler):
|
||||
self.webhook_manager = WebhookCommand(database)
|
||||
self.database = database
|
||||
|
||||
def is_command(self, text: str) -> tuple[bool, str | None]:
|
||||
def is_command(self, text: str) -> tuple[bool, str]:
|
||||
"""Check if the text is a command."""
|
||||
if text.startswith(self.command_prefix) and len(text) > len(self.command_prefix):
|
||||
return True, text[len(self.command_prefix):].split(" ")[0]
|
||||
return False, None
|
||||
return False, ""
|
||||
|
||||
def has_sub_command(self, text: str) -> tuple[bool, str | None]:
|
||||
"""Check if the text has a sub command."""
|
||||
@@ -117,10 +118,8 @@ class WebhookBot(ActivityHandler):
|
||||
|
||||
# Check if this is a channel conversation
|
||||
is_channel = self.is_channel_conversation(turn_context)
|
||||
print(f"[Bot] Is channel conversation: {is_channel}")
|
||||
|
||||
if not is_channel:
|
||||
print(f"[Bot] Not a channel conversation, ignoring message")
|
||||
return
|
||||
|
||||
# Get text from activity - can be None for channel posts with only attachments
|
||||
@@ -131,39 +130,13 @@ class WebhookBot(ActivityHandler):
|
||||
|
||||
# Strip whitespace and check if it's a command
|
||||
text = text.strip()
|
||||
print(f"[Bot] Processed text (after removing mentions): '{text}'")
|
||||
message = text.split(" ")[1:] if len(text.split(" ")) > 1 else None
|
||||
message = " ".join(message) if message is not None else None
|
||||
is_command, command = self.is_command(text)
|
||||
print(f"[Bot] Is command: {is_command}, Command: {command}")
|
||||
|
||||
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:
|
||||
match sub_command:
|
||||
case "list":
|
||||
await self.webhook_manager.handle_list_webhooks(turn_context)
|
||||
return
|
||||
case _:
|
||||
await turn_context.send_activity(f"Sub command {sub_command} not found.")
|
||||
return
|
||||
else:
|
||||
# Default to list if no sub-command specified
|
||||
await self.webhook_manager.handle_list_webhooks(turn_context)
|
||||
return
|
||||
case _:
|
||||
await turn_context.send_activity(f"Command {command} not found.")
|
||||
return
|
||||
cmd = Command(turn_context, self.database)
|
||||
await cmd.handle_command(command, message)
|
||||
else:
|
||||
print(f"[Bot] Not a command, echoing message")
|
||||
await turn_context.send_activity(f"Message: {text}")
|
||||
return
|
||||
return
|
||||
Reference in New Issue
Block a user