journal-bot/bot/commands/journal.py
Remy Moll 49df5a4495
All checks were successful
continuous-integration/drone/push Build is passing
cleanup the model layout
2023-11-17 22:53:06 +01:00

193 lines
7.4 KiB
Python

import datetime
import os
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ParseMode
import models
ENTRY_OPTIONS, CONTENT_ENTRY = range(2)
BUTTON_COUNT = 5
from .basehandler import BaseHandler
class JournalHandler(BaseHandler):
def __init__(self, entry_string):
self.entry_string = entry_string
self.handler = ConversationHandler(
entry_points=[CommandHandler(entry_string, self.entry_point)],
states={
ENTRY_OPTIONS: [
CallbackQueryHandler(self.date_button, pattern=r"^\d{8}$"), # a serialized date
CallbackQueryHandler(self.date_custom, pattern=r"^\d{1,3}$"), # a ~ small delta, symbolizing a new range to show
CallbackQueryHandler(self.option_delete, pattern="delete"),
MessageHandler(filters.ALL, self.date_entry),
],
CONTENT_ENTRY: [
MessageHandler(filters.ALL, self.content_save),
],
},
fallbacks=[],
)
self.current_model = None
async def entry_point(self, update, context):
await super().entry_point(update, context)
if os.getenv("DOCKERIZED", "false") == "true" and os.getenv("CHAT_ID") != str(update.message.chat_id):
await update.message.reply_text("You are not authorized to use this bot")
return ConversationHandler.END
dates = [(datetime.datetime.now() - datetime.timedelta(days = i)).date() for i in range(BUTTON_COUNT + 2)][::-1]
# since there are two buttons additional buttons, we need to have two more days
names = [d.strftime("%d.%m.") for d in dates]
callbacks = [d.strftime("%d%m%Y") for d in dates]
names[-1] = "Today"
names[-2] = "Yesterday"
options = [
[
InlineKeyboardButton(names[-1], callback_data=callbacks[-1])
],
[
InlineKeyboardButton(names[-2], callback_data=callbacks[-2])
],
[
InlineKeyboardButton(n, callback_data=c) for n,c in zip(names[:-2], callbacks[:-2])
],
[
InlineKeyboardButton("<<", callback_data=BUTTON_COUNT + 2)
],
[
InlineKeyboardButton("Delete", callback_data="delete")
]
]
keyboard = InlineKeyboardMarkup(options)
await update.message.reply_text("Please choose a date \(or type it in the format _DDMMYYYY_\)", reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN_V2)
return ENTRY_OPTIONS
async def date_button(self, update, context):
query = update.callback_query
await query.answer()
date = datetime.datetime.strptime(query.data, "%d%m%Y").date()
with models.db:
self.current_model, new = models.JournalEntry.get_or_create(
date = date
)
if new:
count = models.JournalEntry.select().count()
await query.edit_message_text(
text=f"Journal entry no. {count}. What happened on {self.current_model.date_pretty}?"
)
else:
await query.edit_message_text(text="An entry already exists for this date")
return ConversationHandler.END
return CONTENT_ENTRY
async def date_custom(self, update, context):
query = update.callback_query
await query.answer()
delta = int(query.data)
dates = [(datetime.datetime.now() - datetime.timedelta(days = i + delta)).date() for i in range(BUTTON_COUNT)][::-1]
names = [d.strftime("%d.%m.") for d in dates]
callbacks = [d.strftime("%d%m%Y") for d in dates]
options = [
[
InlineKeyboardButton(">>", callback_data=delta - BUTTON_COUNT)
],
[
InlineKeyboardButton(n, callback_data=c) for n,c in zip(names, callbacks)
],
[
InlineKeyboardButton("<<", callback_data=delta + BUTTON_COUNT)
],
[
InlineKeyboardButton("Delete", callback_data="delete")
]
]
keyboard = InlineKeyboardMarkup(options)
await query.edit_message_text("Please choose a date \(or type it in the format _DDMMYYYY_\)", parse_mode=ParseMode.MARKDOWN_V2, reply_markup=keyboard)
return ENTRY_OPTIONS
async def date_entry(self, update, context):
date = update.message.text
try:
date = datetime.datetime.strptime(date, "%d%m%Y").date()
except ValueError:
await update.message.reply_text("Please enter the date in the format _DDMMYYYY_", parse_mode=ParseMode.MARKDOWN_V2)
return ENTRY_OPTIONS
if context.chat_data.get("delete", False): # if not set, delete was not chosen
with models.db:
self.current_model = models.JournalEntry.get_or_none(
date = date
)
if self.current_model:
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
else:
with models.db:
self.current_model, new = models.JournalEntry.get_or_create(
date = date
)
if not new:
await update.message.reply_text("An entry already exists for this date")
return ConversationHandler.END
else:
count = models.JournalEntry.select().count()
await update.message.reply_text(
text=f"Journal entry no. {count}. What happened on {self.current_model.date_pretty}?"
)
return CONTENT_ENTRY
async def content_save(self, update, context):
with models.db:
self.current_model.author_id = update.message.from_user.id
if update.message.text:
self.current_model.text = update.message.text
else:
if update.message.photo:
file = await update.message.effective_attachment[-1].get_file()
else:
file = await update.message.effective_attachment.get_file()
file_bytes = await file.download_as_bytearray()
file_path = file.file_path
self.current_model.save_media(file_bytes, file_path)
self.current_model.text = update.message.caption
self.current_model.save()
await update.message.reply_text(f"Saved entry ✅")
return ConversationHandler.END
async def option_delete(self, update, context):
query = update.callback_query
await query.answer()
await query.edit_message_text(text="Please enter the date in the format _DDMMYYYY_", parse_mode=ParseMode.MARKDOWN_V2)
context.chat_data["delete"] = True
return ENTRY_OPTIONS
async def delete_entry(self, update, context):
with models.db:
self.current_model.delete_instance()
context.chat_data["delete"] = False
await update.message.reply_text(text="Entry deleted ✅")