2024-06-09 19:58:05 +02:00
|
|
|
import simplematrixbotlib as botlib
|
|
|
|
import requests
|
|
|
|
import datetime
|
2024-06-09 21:22:58 +02:00
|
|
|
import asyncio
|
2024-06-09 19:58:05 +02:00
|
|
|
import pytz
|
2024-06-12 22:27:41 +02:00
|
|
|
from typing import Dict
|
2024-06-09 19:58:05 +02:00
|
|
|
import os
|
2024-06-17 21:32:59 +02:00
|
|
|
import logging
|
2024-06-09 19:58:05 +02:00
|
|
|
from dotenv import load_dotenv
|
2024-06-16 20:57:18 +02:00
|
|
|
from .models.user import User
|
|
|
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.sql import select
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
2024-06-09 19:58:05 +02:00
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2024-06-12 21:34:08 +02:00
|
|
|
creds = botlib.Creds(
|
|
|
|
os.getenv("BOT_HOMESERVER"),
|
|
|
|
os.getenv("BOT_USERNAME"),
|
2024-06-14 21:25:46 +02:00
|
|
|
os.getenv("BOT_PASSWORD"),
|
|
|
|
session_stored_file="data/session.txt"
|
2024-06-12 21:34:08 +02:00
|
|
|
)
|
|
|
|
|
2024-06-09 19:58:05 +02:00
|
|
|
bot = botlib.Bot(creds)
|
|
|
|
PREFIX = '!'
|
|
|
|
utc = pytz.UTC
|
|
|
|
|
2024-06-16 20:57:18 +02:00
|
|
|
engine = create_engine(os.getenv("DB_URI"))
|
|
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
session = Session()
|
|
|
|
|
2024-06-17 21:32:59 +02:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
2024-06-09 19:58:05 +02:00
|
|
|
|
2024-06-17 21:32:59 +02:00
|
|
|
|
|
|
|
@bot.listener.on_startup
|
|
|
|
async def initial_schedule(room_id):
|
|
|
|
logging.info("starting praying times bot")
|
|
|
|
users = session.query(User).filter(User.location != "", User.reminder_time_in_minutes is not None).all()
|
2024-06-16 22:45:24 +02:00
|
|
|
for user in users:
|
2024-06-17 21:32:59 +02:00
|
|
|
logging.info("scheduling initial reminder for {}".format(user.username))
|
|
|
|
# reset last reminder schedule time
|
|
|
|
user.current_reminder_date = None
|
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
2024-06-16 22:45:24 +02:00
|
|
|
schedule_reminder(user.username)
|
|
|
|
|
|
|
|
|
2024-06-09 19:58:05 +02:00
|
|
|
@bot.listener.on_message_event
|
2024-06-12 22:27:41 +02:00
|
|
|
async def echo(room, message) -> None:
|
2024-06-09 19:58:05 +02:00
|
|
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
|
|
|
|
|
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("echo"):
|
|
|
|
|
|
|
|
await bot.api.send_text_message(
|
|
|
|
room.room_id, " ".join(arg for arg in match.args())
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.listener.on_message_event
|
2024-06-12 22:27:41 +02:00
|
|
|
async def times(room, message) -> None:
|
2024-06-09 19:58:05 +02:00
|
|
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
|
|
|
username = str(message).split(': ')[0]
|
|
|
|
response = ""
|
|
|
|
|
2024-06-16 20:57:18 +02:00
|
|
|
user = session.query(User).filter(User.username == username, User.location != "").one_or_none()
|
|
|
|
|
2024-06-09 19:58:05 +02:00
|
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("times"):
|
2024-06-16 20:57:18 +02:00
|
|
|
if not user:
|
2024-06-09 19:58:05 +02:00
|
|
|
response = "Please set your location first using *!set-location City, Country*"
|
|
|
|
else:
|
|
|
|
times = get_praying_times(datetime.datetime.today(), username)
|
|
|
|
|
2024-06-16 20:57:18 +02:00
|
|
|
response = "Today's praying times for {}:\n\n".format(user.location)
|
2024-06-09 19:58:05 +02:00
|
|
|
response += "\n".join("{}: {}".format(key, value) for key, value in times.items())
|
|
|
|
|
|
|
|
await bot.api.send_markdown_message(room.room_id, response)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.listener.on_message_event
|
2024-06-12 22:27:41 +02:00
|
|
|
async def usage(room, message) -> None:
|
2024-06-09 19:58:05 +02:00
|
|
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
|
|
|
|
2024-06-09 21:22:58 +02:00
|
|
|
response = """usage:
|
|
|
|
- **!set-location** <City, Country>
|
|
|
|
- **!times**
|
|
|
|
- **!set-reminder** <X> where X is the number of minutes you want to receive the reminder before praying
|
2024-06-09 19:58:05 +02:00
|
|
|
"""
|
|
|
|
|
2024-06-17 21:32:59 +02:00
|
|
|
if match.is_not_from_this_bot() and not match.prefix() and room.room_id:
|
2024-06-09 19:58:05 +02:00
|
|
|
await bot.api.send_markdown_message(room.room_id, response)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.listener.on_message_event
|
2024-06-12 22:27:41 +02:00
|
|
|
async def set_location(room, message) -> None:
|
2024-06-09 19:58:05 +02:00
|
|
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
|
|
|
|
|
|
|
response = ""
|
|
|
|
|
|
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("set-location"):
|
|
|
|
location = " ".join(arg for arg in match.args())
|
|
|
|
username = str(message).split(': ')[0]
|
2024-06-16 20:57:18 +02:00
|
|
|
|
|
|
|
user = session.query(User).filter_by(username=username).one_or_none()
|
|
|
|
if not user:
|
|
|
|
# create new user
|
|
|
|
user = User(username=username, location=location)
|
|
|
|
session.add(user)
|
|
|
|
user.location = location
|
|
|
|
session.commit()
|
|
|
|
|
2024-06-09 19:58:05 +02:00
|
|
|
response = "Your location was set to: {}".format(location)
|
|
|
|
await bot.api.send_markdown_message(room.room_id, response)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.listener.on_message_event
|
2024-06-12 22:27:41 +02:00
|
|
|
async def set_reminder(room, message) -> None:
|
2024-06-09 19:58:05 +02:00
|
|
|
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
|
|
|
|
|
|
|
response = ""
|
|
|
|
|
|
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("set-reminder"):
|
|
|
|
minutes = int(match.args()[0])
|
|
|
|
username = str(message).split(': ')[0]
|
2024-06-16 20:57:18 +02:00
|
|
|
user = session.query(User).filter(User.username == username, User.location != "").one_or_none()
|
2024-06-09 19:58:05 +02:00
|
|
|
|
2024-06-16 20:57:18 +02:00
|
|
|
if not user:
|
2024-06-09 19:58:05 +02:00
|
|
|
response = "You did not set your location yet."
|
|
|
|
await bot.api.send_markdown_message(room.room_id, response)
|
|
|
|
else:
|
2024-06-17 07:37:14 +02:00
|
|
|
user.reminder_time_in_minutes = minutes
|
2024-06-16 20:57:18 +02:00
|
|
|
user.room_id = room.room_id
|
2024-06-17 21:32:59 +02:00
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
2024-06-09 19:58:05 +02:00
|
|
|
response = "Your reminder was set to {} minutes before praying.".format(minutes)
|
|
|
|
schedule_reminder(username)
|
|
|
|
await bot.api.send_markdown_message(room.room_id, response)
|
|
|
|
|
|
|
|
|
2024-06-12 22:27:41 +02:00
|
|
|
def get_praying_times(date: datetime.date, username) -> Dict[str, str]:
|
2024-06-09 19:58:05 +02:00
|
|
|
day = date.day
|
|
|
|
month = date.month
|
|
|
|
year = date.year
|
2024-06-16 20:57:18 +02:00
|
|
|
user = session.query(User).filter_by(username=username).one_or_none()
|
|
|
|
times_api_url = 'http://api.aladhan.com/v1/timingsByAddress/{}-{}-{}?address={}&method=7&iso8601=true'.format(day, month, year, user.location)
|
2024-06-09 19:58:05 +02:00
|
|
|
times = requests.get(times_api_url)
|
|
|
|
times = times.json()['data']['timings']
|
|
|
|
return times
|
|
|
|
|
|
|
|
|
2024-06-12 22:27:41 +02:00
|
|
|
def schedule_reminder(username) -> None:
|
2024-06-17 21:32:59 +02:00
|
|
|
logging.info("scheduling reminder for {}".format(username))
|
2024-06-09 21:22:58 +02:00
|
|
|
now = datetime.datetime.now(datetime.UTC)
|
2024-06-16 20:57:18 +02:00
|
|
|
user = session.query(User).filter_by(username=username).one_or_none()
|
2024-06-16 22:45:24 +02:00
|
|
|
user.current_reminder_date = datetime.date.today()
|
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
|
|
|
times = get_praying_times(datetime.date.today(), username)
|
|
|
|
for prayer, time in times.items():
|
|
|
|
praying_time = datetime.datetime.fromisoformat(time)
|
|
|
|
if praying_time > now:
|
|
|
|
# schedule praying times but skip the ones which already have been passed today
|
|
|
|
seconds = int((praying_time - now).total_seconds())
|
|
|
|
message = "{} is at {}".format(prayer, praying_time.strftime("%H:%M"))
|
2024-06-17 21:32:59 +02:00
|
|
|
logging.info("scheduling reminder for {} with message: {}".format(username, message))
|
2024-06-17 07:37:14 +02:00
|
|
|
asyncio.ensure_future(remind(username, message, seconds - user.reminder_time_in_minutes * 60))
|
2024-06-16 22:45:24 +02:00
|
|
|
# save date of last schedule for user
|
2024-06-09 19:58:05 +02:00
|
|
|
|
|
|
|
|
2024-06-12 22:27:41 +02:00
|
|
|
async def remind(username, message, seconds) -> None:
|
2024-06-09 21:22:58 +02:00
|
|
|
await asyncio.sleep(seconds)
|
2024-06-16 20:57:18 +02:00
|
|
|
user = session.query(User).filter_by(username=username).one_or_none()
|
2024-06-16 22:45:24 +02:00
|
|
|
# check if a new reminder has to be scheduled
|
|
|
|
if user.current_reminder_date < datetime.date.today():
|
|
|
|
schedule_reminder(user.username)
|
2024-06-16 20:57:18 +02:00
|
|
|
await bot.api.send_markdown_message(user.room_id, message)
|
2024-06-09 19:58:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
bot.run()
|