42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
from typing import Tuple
|
|
|
|
from botbuilder.core import ActivityHandler, TurnContext
|
|
from botbuilder.schema import ChannelAccount
|
|
|
|
from config import DefaultConfig
|
|
|
|
class WebhookBot(ActivityHandler):
|
|
"""Webhook bot for Microsoft Teams."""
|
|
|
|
def __init__(self, config: DefaultConfig) -> None:
|
|
super().__init__()
|
|
self.command_prefix = DefaultConfig.COMMAND_PREFIX
|
|
|
|
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):])
|
|
return (False, text)
|
|
|
|
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:
|
|
"""Handle the members added activity."""
|
|
if turn_context.activity.recipient is None:
|
|
return
|
|
for member in members_added:
|
|
if member.id != turn_context.activity.recipient.id:
|
|
await turn_context.send_activity("Hello and welcome!")
|
|
return
|
|
|
|
async def on_message_activity(self, turn_context: TurnContext) -> None:
|
|
"""Handle the message activity."""
|
|
is_command, command = self.is_command(turn_context.activity.text)
|
|
if is_command:
|
|
await self.on_command(command, turn_context)
|
|
return |