✨ Added periodic reminders and included alembic migration in container
This commit is contained in:
parent
86a5dd19b5
commit
1f1ae5077e
7 changed files with 61 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
||||||
.env
|
.env
|
||||||
bin
|
bin
|
||||||
lib
|
lib
|
||||||
|
include
|
||||||
|
|
|
@ -10,4 +10,4 @@ RUN pip3 install -r requirements.txt
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
CMD ["python", "-m", "matrix_bot_praying_times"]
|
ENTRYPOINT ["/bot/entrypoint.sh"]
|
||||||
|
|
|
@ -6,6 +6,8 @@ from sqlalchemy import pool
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
|
|
30
alembic/versions/c2bcd940c9e8_add_current_reminder_date.py
Normal file
30
alembic/versions/c2bcd940c9e8_add_current_reminder_date.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
"""Add current reminder date
|
||||||
|
|
||||||
|
Revision ID: c2bcd940c9e8
|
||||||
|
Revises: 12669d9a145b
|
||||||
|
Create Date: 2024-06-16 22:21:27.188128
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'c2bcd940c9e8'
|
||||||
|
down_revision: Union[str, None] = '12669d9a145b'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('user', sa.Column('current_reminder_date', sa.Date(), nullable=True))
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_column('user', 'current_reminder_date')
|
||||||
|
# ### end Alembic commands ###
|
3
entrypoint.sh
Executable file
3
entrypoint.sh
Executable file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
alembic upgrade head
|
||||||
|
python -m matrix_bot_praying_times
|
|
@ -30,6 +30,13 @@ Session = sessionmaker(bind=engine)
|
||||||
session = Session()
|
session = Session()
|
||||||
|
|
||||||
|
|
||||||
|
@bot.listener.on_reaction_event
|
||||||
|
def initial_schedule() -> None:
|
||||||
|
users = session.query(User).filter(User.location != "").all()
|
||||||
|
for user in users:
|
||||||
|
schedule_reminder(user.username)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@bot.listener.on_message_event
|
@bot.listener.on_message_event
|
||||||
async def echo(room, message) -> None:
|
async def echo(room, message) -> None:
|
||||||
|
@ -134,20 +141,26 @@ def get_praying_times(date: datetime.date, username) -> Dict[str, str]:
|
||||||
def schedule_reminder(username) -> None:
|
def schedule_reminder(username) -> None:
|
||||||
now = datetime.datetime.now(datetime.UTC)
|
now = datetime.datetime.now(datetime.UTC)
|
||||||
user = session.query(User).filter_by(username=username).one_or_none()
|
user = session.query(User).filter_by(username=username).one_or_none()
|
||||||
# as a workaround until it's finished schedule the next 7 days
|
user.current_reminder_date = datetime.date.today()
|
||||||
for i in range(7):
|
session.add(user)
|
||||||
times = get_praying_times(datetime.date.today() + datetime.timedelta(days=i), username)
|
session.commit()
|
||||||
for prayer, time in times.items():
|
times = get_praying_times(datetime.date.today(), username)
|
||||||
praying_time = datetime.datetime.fromisoformat(time)
|
for prayer, time in times.items():
|
||||||
if praying_time > now:
|
praying_time = datetime.datetime.fromisoformat(time)
|
||||||
seconds = int((praying_time - now).total_seconds())
|
if praying_time > now:
|
||||||
message = "{} is at {}".format(prayer, praying_time.strftime("%H:%M"))
|
# schedule praying times but skip the ones which already have been passed today
|
||||||
asyncio.ensure_future(remind(username, message, seconds - user.reminder_time_in_minues * 60))
|
seconds = int((praying_time - now).total_seconds())
|
||||||
|
message = "{} is at {}".format(prayer, praying_time.strftime("%H:%M"))
|
||||||
|
asyncio.ensure_future(remind(username, message, seconds - user.reminder_time_in_minues * 60))
|
||||||
|
# save date of last schedule for user
|
||||||
|
|
||||||
|
|
||||||
async def remind(username, message, seconds) -> None:
|
async def remind(username, message, seconds) -> None:
|
||||||
await asyncio.sleep(seconds)
|
await asyncio.sleep(seconds)
|
||||||
user = session.query(User).filter_by(username=username).one_or_none()
|
user = session.query(User).filter_by(username=username).one_or_none()
|
||||||
|
# check if a new reminder has to be scheduled
|
||||||
|
if user.current_reminder_date < datetime.date.today():
|
||||||
|
schedule_reminder(user.username)
|
||||||
await bot.api.send_markdown_message(user.room_id, message)
|
await bot.api.send_markdown_message(user.room_id, message)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from sqlalchemy import Column, String, Integer
|
from sqlalchemy import Column, String, Integer, Date
|
||||||
|
|
||||||
from ..db import Base
|
from ..db import Base
|
||||||
|
|
||||||
|
@ -11,3 +11,4 @@ class User(Base):
|
||||||
location = Column(String)
|
location = Column(String)
|
||||||
room_id = Column(String)
|
room_id = Column(String)
|
||||||
reminder_time_in_minutes = Column(Integer)
|
reminder_time_in_minutes = Column(Integer)
|
||||||
|
current_reminder_date = Column(Date)
|
||||||
|
|
Loading…
Reference in a new issue