journal-bot/bot/commands/journal.py
2023-04-19 23:38:09 +02:00

76 lines
2.8 KiB
Python

import datetime
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler, CallbackContext
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
DATE_CHOICE, DATE_ENTRY, CONTENT = range(3)
class JournalHandler:
def __init__(self, entry_string, models):
self.models = models
self.handler = ConversationHandler(
entry_points=[CommandHandler(entry_string, self.start)],
states={
DATE_CHOICE: [
CallbackQueryHandler(self.date_choice),
],
DATE_ENTRY: [
MessageHandler(filters.TEXT, self.date_entry),
],
CONTENT: [
MessageHandler(filters.TEXT, self.content_text),
MessageHandler(filters.ATTACHMENT, self.content_media),
],
},
fallbacks=[],
)
self.current_model = None
async def start(self, update, context):
"""Send a message when the command /start is issued."""
options = [
InlineKeyboardButton("Today", callback_data="today"),
InlineKeyboardButton("Yesterday", callback_data="yesterday"),
InlineKeyboardButton("Custom date", callback_data="custom"),
]
keyboard = InlineKeyboardMarkup([options])
await update.message.reply_text("Please choose an option for the entry:", reply_markup=keyboard)
return DATE_CHOICE
async def date_choice(self, update, context):
query = update.callback_query
query.answer()
if query.data == "today" or query.data == "yesterday":
date = datetime.datetime.now().date() if query.data == "today" else datetime.datetime.now().date() - datetime.timedelta(days=1)
self.current_model = self.models.JournalEntry(
date = date
)
return CONTENT
else:
await query.edit_message_text(text="Please enter the date in the format DDMMYYYY")
return DATE_ENTRY
async def date_entry(self, update, context):
# create an inline keyboard with the option today and yesterday and custom
# date
date = update.message.text
try:
date = datetime.datetime.strptime(date, "%d%m%Y").date()
self.current_model = self.models.JournalEntry(
date = date
)
except ValueError:
await update.message.reply_text("Please enter the date in the format DDMMYYYY")
return DATE_ENTRY
await update.message.reply_text("Please enter the content for the entry")
return CONTENT
async def content_text(self, update, context):
return
async def content_media(self, update, context):
return