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/commands/turtle.py b/bot/commands/turtle.py index a9b74dd..c71cc4a 100644 --- a/bot/commands/turtle.py +++ b/bot/commands/turtle.py @@ -1,13 +1,66 @@ +import os +from pathlib import Path from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup +from telegram import Update +import re +import random +import time from .basehandler import BaseHandler + +MEDIA_DIR = Path(os.getenv("MEDIA_DIR")) +TURTLE_VIDEO_LOCATION = MEDIA_DIR / "turtle_reactions" + +TURTLE_VIDEOS = list(TURTLE_VIDEO_LOCATION.glob("*.mp4")) + class TurtleHandler(BaseHandler): def __init__(self): - self.handler = MessageHandler(filters.Regex(r"Hallo|hallo"), self.entry_point) + self.entry_string = "Variation of hallo" + self.handler = MessageHandler(filters.Regex(r"[hH]([aA]+|[eE]+)[lL]{2,}[oOöÖ]+(le)?|(chen)") | # react to hello strings + filters.Regex(b"\xF0\x9F\x90\xA2".decode("utf8")) | # react to turtle emoji + filters.Regex(r"[sS](childkröte)|[tT](urtle)"), # react to turtle string + self.entry_point) pass - async def entry_point(self, update, context): + async def entry_point(self, update: Update, context): await super().entry_point(update, context) - update.message.reply_ \ No newline at end of file + msgtxt = update.message.text + turtle_emoji = b"\xF0\x9F\x90\xA2".decode("utf8") + + if "hallo" in msgtxt: # react to hallo + vid = TURTLE_VIDEOS[0] + answertxt = "Hallo!" + elif re.search("[eE][lL]{2,}[oO]", msgtxt): # react to hello + vid = TURTLE_VIDEOS[2] + answertxt = "Hello!" + elif re.search("([aA]{4,}|[lL]{4,}|[oO]{4,}|[öÖ]{4,})", msgtxt): # react to stretched hello + vid = TURTLE_VIDEOS[5] + answertxt = "That's a lot of letters!" + elif re.search(turtle_emoji, msgtxt): # react to turtle emoji + vid=TURTLE_VIDEOS[0] # TODO: choose video for smiley reaction + answertxt="Turtle detected! Self-destruction mode activated..." + elif re.search("[sS](childkröte)|[tT](urtle)", msgtxt): # react to turtle string + vid=None + answertxt=turtle_emoji + else: + vid = random.choice(TURTLE_VIDEOS[1:2]+TURTLE_VIDEOS[3:5]+TURTLE_VIDEOS[6:]) + answertxt = "" + + if vid!=None: + if re.search(turtle_emoji, msgtxt): + await update.message.reply_text(text=answertxt) + time.sleep(1) + await update.message.reply_text(text="3") + time.sleep(1) + await update.message.reply_text(text="2") + time.sleep(1) + await update.message.reply_text(text="1") + time.sleep(1) + await update.message.reply_video(video=vid, caption="Kaboom!") + else: + await update.message.reply_video(video=vid, caption=answertxt) + else: + await update.message.reply_text(text=answertxt) + \ No newline at end of file diff --git a/bot/main.py b/bot/main.py index 0d62340..de38a35 100644 --- a/bot/main.py +++ b/bot/main.py @@ -3,9 +3,10 @@ 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 cronjob import chat_photo + logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO @@ -27,6 +28,9 @@ def main() -> None: 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) + # 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))