minor improvements
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-05-01 22:49:31 +02:00
parent ba3845496e
commit dec12b5576
5 changed files with 100 additions and 23 deletions

View File

@@ -2,7 +2,7 @@ import datetime
import os
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
DATE_CHOICE, DATE_ENTRY, CONTENT = range(3)
ACTION_CHOICE, DATE_ENTRY, ADD_CONTENT, DELETE_ENTRY = range(4)
from .basehandler import BaseHandler
@@ -13,16 +13,20 @@ class JournalHandler(BaseHandler):
self.handler = ConversationHandler(
entry_points=[CommandHandler(entry_string, self.entry_point)],
states={
DATE_CHOICE: [
ACTION_CHOICE: [
CallbackQueryHandler(self.date_choice, pattern="today|yesterday"),
CallbackQueryHandler(self.date_custom, pattern="custom"),
CallbackQueryHandler(self.option_delete, pattern="delete")
],
DATE_ENTRY: [
MessageHandler(filters.ALL, self.date_entry),
],
CONTENT: [
ADD_CONTENT: [
MessageHandler(filters.ALL, self.content_save),
],
DELETE_ENTRY: [
MessageHandler(filters.ALL, self.delete_entry),
]
},
fallbacks=[],
)
@@ -37,14 +41,18 @@ class JournalHandler(BaseHandler):
return ConversationHandler.END
options = [
InlineKeyboardButton("Today", callback_data="today"),
InlineKeyboardButton("Yesterday", callback_data="yesterday"),
InlineKeyboardButton("Custom date", callback_data="custom"),
options = [[
InlineKeyboardButton("Today", callback_data="today"),
InlineKeyboardButton("Yesterday", callback_data="yesterday"),
InlineKeyboardButton("Custom date", callback_data="custom"),
],
[
InlineKeyboardButton("Delete", callback_data="delete")
]
]
keyboard = InlineKeyboardMarkup([options])
keyboard = InlineKeyboardMarkup(options)
await update.message.reply_text("Please choose an option for the entry:", reply_markup=keyboard)
return DATE_CHOICE
return ACTION_CHOICE
async def date_choice(self, update, context):
@@ -63,13 +71,13 @@ class JournalHandler(BaseHandler):
)
if new:
await query.edit_message_text(
text="Please enter the content for the entry"
text=f"What is your entry for {self.current_model.date.strftime('%A, %-d. %B %Y')}?"
)
else:
await query.edit_message_text(text="An entry already exists for this date")
return ConversationHandler.END
return CONTENT
return ADD_CONTENT
async def date_custom(self, update, context):
@@ -81,22 +89,34 @@ class JournalHandler(BaseHandler):
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")
return DATE_ENTRY
with self.models.db:
self.current_model, new = self.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
if context.chat_data.get("delete", True):
with self.models.db:
self.current_model = self.models.JournalEntry.get_or_none(
date = date
)
if self.current_model:
return DELETE_ENTRY
else:
await update.message.reply_text("No entry found for this date")
return ConversationHandler.END
else:
await update.message.reply_text("Please enter the content for the entry")
return CONTENT
with self.models.db:
self.current_model, new = self.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:
await update.message.reply_text(f"What is your entry for {self.current_model.date.strftime('%A, %-d. %B %Y')}?")
return ADD_CONTENT
async def content_save(self, update, context):
@@ -119,5 +139,20 @@ class JournalHandler(BaseHandler):
self.current_model.save()
await update.message.reply_text(f"Saved entry for {self.current_model.date.strftime('%A, %-d. %B %Y')}")
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")
context.chat_data["delete"] = True
return DATE_ENTRY
async def delete_entry(self, update, context):
with self.models.db:
self.current_model.delete_instance()
await update.callback_query.edit_message_text(text="Entry deleted ✅")
return ConversationHandler.END

13
bot/commands/turtle.py Normal file
View File

@@ -0,0 +1,13 @@
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from .basehandler import BaseHandler
class TurtleHandler(BaseHandler):
def __init__(self):
self.handler = MessageHandler(filters.Regex(r"Hallo|hallo"), self.entry_point)
pass
async def entry_point(self, update, context):
await super().entry_point(update, context)
update.message.reply_