From 9a82d66897c13691acb85f3109ae5f73212e04eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lia=20Sch=C3=B6newei=C3=9F?= Date: Sun, 14 May 2023 22:05:24 +0200 Subject: [PATCH] Basic memory function implemented --- bot/commands/journal.py | 18 +++----- bot/commands/memory.py | 96 +++++++++++++++++++++++++++++++++++++++++ bot/main.py | 5 ++- bot/models.py | 8 ++++ 4 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 bot/commands/memory.py diff --git a/bot/commands/journal.py b/bot/commands/journal.py index 008f0d9..6ef06f3 100644 --- a/bot/commands/journal.py +++ b/bot/commands/journal.py @@ -2,7 +2,7 @@ import datetime import os from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup -ACTION_CHOICE, DATE_ENTRY, ADD_CONTENT, DELETE_ENTRY = range(4) +ACTION_CHOICE, DATE_ENTRY, ADD_CONTENT = range(3) from .basehandler import BaseHandler @@ -23,10 +23,7 @@ class JournalHandler(BaseHandler): ], ADD_CONTENT: [ MessageHandler(filters.ALL, self.content_save), - ], - DELETE_ENTRY: [ - MessageHandler(filters.ALL, self.delete_entry), - ] + ] }, fallbacks=[], ) @@ -71,7 +68,7 @@ class JournalHandler(BaseHandler): ) if new: await query.edit_message_text( - text=f"What is your entry for {self.current_model.date.strftime('%A, %-d. %B %Y')}?" + text=f"What is your entry for {self.current_model.date_pretty}?" ) else: await query.edit_message_text(text="An entry already exists for this date") @@ -102,11 +99,11 @@ class JournalHandler(BaseHandler): date = date ) if self.current_model: - return DELETE_ENTRY + await self.delete_entry(update, context) else: await update.message.reply_text("No entry found for this date") context.chat_data["delete"] = False - return ConversationHandler.END + return ConversationHandler.END else: with self.models.db: self.current_model, new = self.models.JournalEntry.get_or_create( @@ -116,7 +113,7 @@ class JournalHandler(BaseHandler): await update.message.reply_text("An entry already exists for this date") return ConversationHandler.END else: - await update.message.reply_text(f"What is your entry for {self.current_model.date.strftime('%A, %-d. %B %Y')}?") + await update.message.reply_text(f"What is your entry for {self.current_model.date_pretty}?") return ADD_CONTENT @@ -156,5 +153,4 @@ class JournalHandler(BaseHandler): with self.models.db: self.current_model.delete_instance() context.chat_data["delete"] = False - await update.callback_query.edit_message_text(text="Entry deleted ✅") - return ConversationHandler.END + await update.message.reply_text(text="Entry deleted ✅") \ No newline at end of file diff --git a/bot/commands/memory.py b/bot/commands/memory.py new file mode 100644 index 0000000..8e7033d --- /dev/null +++ b/bot/commands/memory.py @@ -0,0 +1,96 @@ +import datetime +from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler, CallbackContext +from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, InputMediaPhoto +# ACTION_CHOICE, DATE_ENTRY, ADD_CONTENT = range(3) +MEMORY_CHOICE = range(1) + + +from .basehandler import BaseHandler + +class MemoryHandler(BaseHandler): + def __init__(self, entry_string, models): + self.models = models + self.entry_string = entry_string + self.handler = ConversationHandler( + entry_points=[CommandHandler(entry_string, self.entry_point, )], + states={ + MEMORY_CHOICE: [ + CallbackQueryHandler(self.choose_memory), + ] + + }, + fallbacks=[], + ) + + self.current_model = None + + + async def entry_point(self, update: Update, context: CallbackContext): + await super().entry_point(update, context) + search_string = " ".join(context.args) + matching_models = self.models.JournalEntry.select().where( + self.models.JournalEntry.text.contains( + search_string + ) + ).order_by(self.models.JournalEntry.date) + + # matches_string = "" + # for i,m in enumerate(matching_models): + # matches_string += "\n"+ f"{i+1}: " + m.date_pretty + + options = [[InlineKeyboardButton(m.date_pretty, callback_data=i)] for i,m in enumerate(matching_models)] + + keyboard = InlineKeyboardMarkup(options) + + await update.message.reply_text( + f"Which moment would you like to remember?", reply_markup=keyboard + ) + + context.chat_data["kept_matches"] = list(matching_models) + + return MEMORY_CHOICE + + # for m in matching_models: + # # print(m.date) + # if m.media_path: + # await update.message.reply_photo( + # photo = m.media_path, + # caption = + # f"On {m.date}, " + # f"{m.author} wrote: \n" + # f"{m.text}" + # ) + # else: + # await update.message.reply_text( + # f"On {m.date}, " + # f"{m.author} wrote: \n" + # f"{m.text}" + # ) + + # return ConversationHandler.END + + async def choose_memory(self, update: Update, context: CallbackContext): + query = update.callback_query + ind = int(query.data) + await query.answer() + + matching_models = context.chat_data["kept_matches"] + chosen_match = matching_models[ind] + + if chosen_match.media_path: + # context.bot.sendPhoto() + await update.effective_message.reply_photo( + photo = chosen_match.media_path, + caption= + f"On {chosen_match.date_pretty}, " + f"{chosen_match.author} wrote: \n" + f"{chosen_match.text}" + ) + else: + await query.edit_message_text( + f"On {chosen_match.date_pretty}, " + f"{chosen_match.author} wrote: \n" + f"{chosen_match.text}" + ) + + return ConversationHandler.END \ No newline at end of file diff --git a/bot/main.py b/bot/main.py index da3c574..de38a35 100644 --- a/bot/main.py +++ b/bot/main.py @@ -3,9 +3,8 @@ from telegram.ext import Application import logging import models -from commands import journal, status +from commands import journal, status, turtle, memory from commands.list import list -from commands import turtle from cronjob import chat_photo logging.basicConfig( @@ -30,6 +29,8 @@ def main() -> None: 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) + # application.add_handler(CommandHandler("help", help_command)) # on non command i.e message - echo the message on Telegram # application.add_handler(InlineQueryHandler(inline_query)) diff --git a/bot/models.py b/bot/models.py index 84b4eb6..7f8b341 100644 --- a/bot/models.py +++ b/bot/models.py @@ -1,6 +1,7 @@ from peewee import * from pathlib import Path import os +import datetime ID_MAPPINGS = { "Lia": 5603036217, @@ -52,6 +53,13 @@ class JournalEntry(BaseModel): self.save() + @property + def date_pretty(self) -> str: + try: + return self.date.strftime('%A, %-d. %B %Y') + except ValueError: #fck windows + return self.date.strftime('%a, %d. %b %Y') + def set_db(db_path): db.initialize(SqliteDatabase(db_path))