Files
calendar_bot/my_project_name/bot_commands.py
2024-06-17 22:15:47 +02:00

143 lines
4.6 KiB
Python

from nio import AsyncClient, MatrixRoom, RoomMessageText
from my_project_name.chat_functions import react_to_event, send_text_to_room
from my_project_name.config import Config
from my_project_name.storage import Storage
from my_project_name.caldav_handler import CaldavHandler
import json
import requests
def task_help():
api_url = "https://tasklist.malobeo.org/api/next"
response = requests.get(api_url)
if response.status_code != 200:
return "Error requesting " + api_url + " Status Code: " + str(response.status_code)
data = response.json()
message = "# Tasks for this week\n"
for task in data:
message += "- **{}** (*{}*): {}\n".format(task["Name"], task["Value"], task["Description"])
return message
class Command:
def __init__(
self,
client: AsyncClient,
store: Storage,
config: Config,
command: str,
room: MatrixRoom,
event: RoomMessageText,
):
"""A command made by a user.
Args:
client: The client to communicate to matrix with.
store: Bot storage.
config: Bot configuration parameters.
command: The command and arguments.
room: The room the command was sent in.
event: The event describing the command.
"""
self.client = client
self.store = store
self.config = config
self.command = command
self.room = room
self.event = event
self.args = self.command.split()[1:]
async def process(self):
"""Process the command"""
#if self.command.startswith("echo"):
# await self._echo()
if self.command.startswith("react"):
await self._react()
elif self.command.startswith("help"):
await self._show_help()
elif self.command.startswith("today"):
await self._show_today()
elif self.command.startswith("week"):
await self._show_week()
elif self.command.startswith("month"):
await self._show_month()
elif self.command.startswith("tasks"):
await self._show_tasks()
#else:
# await self._unknown_command()
async def _show_today(self):
handler = CaldavHandler()
response = handler.print_today()
if len(response) == 0:
response = "today is nothing planned yet. riot or read theory"
await send_text_to_room(self.client, self.room.room_id, response)
async def _show_week(self):
handler = CaldavHandler()
response = handler.print_week()
await send_text_to_room(self.client, self.room.room_id, response)
async def _show_month(self):
handler = CaldavHandler()
response = handler.print_month()
await send_text_to_room(self.client, self.room.room_id, response, markdown_convert=True)
async def _show_tasks(self):
response = task_help()
await send_text_to_room(self.client, self.room.room_id, response, markdown_convert=True)
async def _echo(self):
"""Echo back the command's arguments"""
response = " ".join(self.args)
await send_text_to_room(self.client, self.room.room_id, response)
async def _react(self):
"""Make the bot react to the command message"""
# React with a start emoji
reaction = ""
await react_to_event(
self.client, self.room.room_id, self.event.event_id, reaction
)
# React with some generic text
reaction = "(A)"
await react_to_event(
self.client, self.room.room_id, self.event.event_id, reaction
)
async def _show_help(self):
"""Show the help text"""
if not self.args:
text = (
"Hello, I am kallauser MC's (A)wesome calendar bot <3! Use `help commands` to view "
"available commands.\n"
"Use `help rules` to view the rules"
)
await send_text_to_room(self.client, self.room.room_id, text)
return
topic = self.args[0]
if topic == "rules":
text = "be nice to each other."
elif topic == "commands":
text = "Available commands: today, week, month, tasks"
else:
text = "I dont know what you are talking about.."
await send_text_to_room(self.client, self.room.room_id, text)
async def _unknown_command(self):
await send_text_to_room(
self.client,
self.room.room_id,
f"Unknown command '{self.command}'. Try the 'help' command for more information.",
)