fixed stuck in memory function. implemented random memory

This commit is contained in:
Lia Schöneweiß
2023-05-24 15:02:17 +02:00
committed by Remy Moll
parent 49339ebcb9
commit 0860196a53
5 changed files with 135 additions and 29 deletions

View File

@@ -30,7 +30,6 @@ class MemoryHandler(BaseHandler):
search_string = " ".join(context.args)
if search_string == '~photo':
matching_models = self.models.JournalEntry.select().where(self.models.JournalEntry.media_path != "").order_by(self.models.JournalEntry.date)
else: # searching for text
@@ -40,7 +39,12 @@ class MemoryHandler(BaseHandler):
)
).order_by(self.models.JournalEntry.date)
# exit if no memory matches the string
if len(matching_models) == 0:
await update.message.reply_text(f"There is no matching memory yet.")
return ConversationHandler.END
options = [[InlineKeyboardButton(m.date_pretty, callback_data=i)] for i,m in enumerate(matching_models)]
keyboard = InlineKeyboardMarkup(options)

View File

@@ -0,0 +1,59 @@
from datetime import time, timedelta, timezone, datetime, date
import os
from peewee import fn
class RandomMemoryJob():
def __init__(self, models, bot, job_queue):
self.models = models
self.bot = bot
# set the message sending time; include UTC shift +2
sending_time = time(hour=12, minute=00, second=0, tzinfo=timezone(timedelta(hours=2)))
# context.job_queue.run_daily(self.callback_memory, sending_time, chat_id=chat_id)
# job_queue.run_repeating(self.callback_memory, interval=10, first=sending_time)
job_queue.run_once(self.callback_memory, timedelta(seconds=2))
async def callback_memory(self, context):
# last_seen of memory must be older than 10 days in past or None
possible_entries = self.models.JournalEntry.select().where(
(datetime.today().date().year - self.models.JournalEntry.last_shown.year >= 0) | (self.models.JournalEntry.last_shown == None)).where(
(datetime.today().date().month - self.models.JournalEntry.last_shown.month >= 0) | (self.models.JournalEntry.last_shown == None)).where(
(datetime.today().date().day - self.models.JournalEntry.last_shown.day >= 0) | (self.models.JournalEntry.last_shown == None)).order_by(fn.Random())
# returns if all entries have been seen recently
if len(possible_entries) == 0:
print("Come back later for another memory.")
return
# update the last_shown of the chosen entry
chosen_entry = possible_entries.get()
# chosen_entry = self.models.JournalEntry.select().get()
# chosen_entry.last_shown = date(year=2023, month=5, day=3)
chosen_entry.last_shown = datetime.today().date()
chosen_entry.save()
chat_id = os.getenv("CHAT_ID")
if chosen_entry.media_path:
await self.bot.send_photo(
chat_id = chat_id,
photo = chosen_entry.media_path,
caption =
f"On {chosen_entry.date_pretty}, "
f"{chosen_entry.author} wrote: \n"
f"{chosen_entry.text}"
)
else:
await self.bot.send_message(
chat_id = chat_id,
text =
f"On {chosen_entry.date_pretty}, "
f"{chosen_entry.author} wrote: \n"
f"{chosen_entry.text}"
)

View File

@@ -5,7 +5,7 @@ import logging
import models
from commands import journal, status, turtle, memory
from commands.list import list
from cronjob import chat_photo
from cronjob import chat_photo, random_memory
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
@@ -24,13 +24,14 @@ def main() -> None:
models.set_db(db_path)
application = Application.builder().token(token).build()
application.add_handler(journal.JournalHandler("journal", models).handler)
application.add_handler(list.ListHandler("list", models).handler)
application.add_handler(status.StatusHandler("status", models).handler)
application.add_handler(turtle.TurtleHandler().handler)
application.add_handler(memory.MemoryHandler("memory", models).handler)
random_memory.RandomMemoryJob(models, application.bot, application.job_queue)
# application.add_handler(CommandHandler("help", help_command))
# on non command i.e message - echo the message on Telegram
# application.add_handler(InlineQueryHandler(inline_query))