Basic memory function implemented

This commit is contained in:
Lia Schöneweiß 2023-05-14 22:05:24 +02:00
parent 793dda3ce6
commit 9a82d66897
4 changed files with 114 additions and 13 deletions

@ -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 ✅")

96
bot/commands/memory.py Normal file

@ -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

@ -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))

@ -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))