This commit is contained in:
parent
ba3845496e
commit
dec12b5576
@ -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
13
bot/commands/turtle.py
Normal 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_
|
22
bot/cronjob/chat_photo.py
Normal file
22
bot/cronjob/chat_photo.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from telegram.ext import ExtBot
|
||||
import random
|
||||
|
||||
MEDIA_DIR = Path(os.getenv("MEDIA_DIR"))
|
||||
CHAT_ID = os.getenv("CHAT_ID")
|
||||
|
||||
|
||||
async def set_random(bot: ExtBot) -> None:
|
||||
"""Set a random chat photo."""
|
||||
if os.getenv("DOCKERIZED", "false") == "false":
|
||||
# only change image on prod
|
||||
return
|
||||
|
||||
photos = list(MEDIA_DIR.glob("*.jpg")) + list(MEDIA_DIR.glob("*.png")) + list(MEDIA_DIR.glob("*.jpeg"))
|
||||
|
||||
if len(photos) == 0:
|
||||
return
|
||||
|
||||
photo = random.choice(photos)
|
||||
await bot.set_chat_photo(CHAT_ID, photo)
|
@ -2,14 +2,15 @@ import os
|
||||
from telegram.ext import Application
|
||||
import logging
|
||||
|
||||
import models
|
||||
from commands import journal, status
|
||||
from commands.list import list
|
||||
import models
|
||||
|
||||
from cronjob import chat_photo
|
||||
logging.basicConfig(
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
level=logging.INFO
|
||||
)
|
||||
import asyncio
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -30,6 +31,10 @@ def main() -> None:
|
||||
# on non command i.e message - echo the message on Telegram
|
||||
# application.add_handler(InlineQueryHandler(inline_query))
|
||||
|
||||
|
||||
# on every start set a new chat photo
|
||||
# loop = asyncio.get_event_loop()
|
||||
asyncio.ensure_future(chat_photo.set_random(application.bot))
|
||||
# Run the bot until the user presses Ctrl-C
|
||||
application.run_polling()
|
||||
|
||||
|
@ -26,6 +26,8 @@ spec:
|
||||
env:
|
||||
- name: MEDIA_DIR
|
||||
value: /journal/media
|
||||
- name: tz
|
||||
value: Europe/Berlin
|
||||
volumeMounts:
|
||||
- name: journal-nfs
|
||||
mountPath: /journal
|
||||
|
Loading…
x
Reference in New Issue
Block a user