Files
calendar_bot/my_project_name/main.py
2024-06-18 11:53:06 +02:00

189 lines
6.4 KiB
Python

#!/usr/bin/env python3
import asyncio
import logging
import threading
import sys
from time import sleep
import datetime
import requests
from aiohttp import ClientConnectionError, ServerDisconnectedError
from nio import (
AsyncClient,
AsyncClientConfig,
InviteMemberEvent,
LocalProtocolError,
LoginError,
MegolmEvent,
RoomMessageText,
UnknownEvent,
)
from my_project_name.chat_functions import react_to_event, send_text_to_room
from my_project_name.caldav_handler import CaldavHandler
from my_project_name.callbacks import Callbacks
from my_project_name.config import Config
from my_project_name.storage import Storage
logger = logging.getLogger(__name__)
from datetime import datetime, time
def weekly_task_reminder():
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 = "# Weekly Task Reminder\n"
for task in data:
message += "- {}: **{}**\n".format(task["Name"], task["Value"])
message += "\n*Write* ```!c tasks``` *to get details about the tasks*"
return message
def is_time_between(begin_time, end_time, check_time=None):
# If check time is not given, default to current UTC time
check_time = check_time or datetime.utcnow().time()
if begin_time < end_time:
return check_time >= begin_time and check_time <= end_time
else: # crosses midnight
return check_time >= begin_time or check_time <= end_time
async def foo(client):
ShouldSendReminder = True
while True:
print(datetime.today().weekday())
await client.sync()
if is_time_between(time(9,00), time(10,10)):
print("Ist Time to check")
if ShouldSendReminder:
handler = CaldavHandler()
messages = handler.send_reminders()
for message in messages:
await send_text_to_room(client, "!aRFGSGKGeaBxiEfDMC:matrix.org", message)
print("Check if its tuesday")
if datetime.today().weekday() == 1: #check if its tuesday
await send_text_to_room(client, "!aRFGSGKGeaBxiEfDMC:matrix.org", weekly_task_reminder())
ShouldSendReminder = False
else:
ShouldSendReminder = True
sleep(20)
async def main():
"""The first function that is run when starting the bot"""
# Read user-configured options from a config file.
# A different config file path can be specified as the first command line argument
if len(sys.argv) > 1:
config_path = sys.argv[1]
else:
config_path = "config.yaml"
# Read the parsed config file and create a Config object
config = Config(config_path)
# Configure the database
store = Storage(config.database)
# Configuration options for the AsyncClient
client_config = AsyncClientConfig(
max_limit_exceeded=0,
max_timeouts=0,
store_sync_tokens=True,
encryption_enabled=True,
)
# Initialize the matrix client
client = AsyncClient(
config.homeserver_url,
config.user_id,
device_id=config.device_id,
store_path=config.store_path,
config=client_config,
)
if config.user_token:
client.access_token = config.user_token
client.user_id = config.user_id
# Set up event callbacks
callbacks = Callbacks(client, store, config)
client.add_event_callback(callbacks.message, (RoomMessageText,))
client.add_event_callback(callbacks.invite, (InviteMemberEvent,))
client.add_event_callback(callbacks.decryption_failure, (MegolmEvent,))
client.add_event_callback(callbacks.unknown, (UnknownEvent,))
# Keep trying to reconnect on failure (with some time in-between)
while True:
try:
if config.user_token:
# Use token to log in
client.load_store()
# Sync encryption keys with the server
if client.should_upload_keys:
await client.keys_upload()
else:
# Try to login with the configured username/password
try:
login_response = await client.login(
password=config.user_password,
device_name=config.device_name,
)
# Check if login failed
if type(login_response) == LoginError:
logger.error("Failed to login: %s", login_response.message)
return False
except LocalProtocolError as e:
# There's an edge case here where the user hasn't installed the correct C
# dependencies. In that case, a LocalProtocolError is raised on login.
logger.fatal(
"Failed to login. Have you installed the correct dependencies? "
"https://github.com/poljar/matrix-nio#installation "
"Error: %s",
e,
)
return False
# Login succeeded!
#logger.info(f"Logged in as {config.user_id}")
await client.join("!aRFGSGKGeaBxiEfDMC:matrix.org")
print("joined room")
sleep(5)
await client.sync(full_state=True)
print(client.rooms)
#await client.room_send(
# # Watch out! If you join an old room you'll see lots of old messages
# room_id="!zKwFlsxXpmVhBMdOBa:matrix.org",
# message_type="m.room.message",
# content={"msgtype": "m.text", "body": "Hello world!"},
# )
#t = threading.Thread(target = foo, args =(client, ))
#t.start()
await foo(client)
await client.sync_forever(timeout=30000, full_state=True)
except (ClientConnectionError, ServerDisconnectedError):
logger.warning("Unable to connect to homeserver, retrying in 15s...")
# Sleep so we don't bombard the server with login requests
sleep(15)
finally:
# Make sure to close the client connection on disconnect
await client.close()
# Run the main function in an asyncio event loop
asyncio.get_event_loop().run_until_complete(main())