Basic memory function implemented
This commit is contained in:
		
							
								
								
									
										96
									
								
								bot/commands/memory.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										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
 | 
			
		||||
		Reference in New Issue
	
	Block a user