initial framework
This commit is contained in:
76
bot/commands/journal.py
Normal file
76
bot/commands/journal.py
Normal file
@@ -0,0 +1,76 @@
|
||||
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
|
35
bot/main.py
Normal file
35
bot/main.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import os
|
||||
from telegram.ext import Application
|
||||
import logging
|
||||
|
||||
from commands import journal
|
||||
import models
|
||||
|
||||
logging.basicConfig(
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Run the bot."""
|
||||
|
||||
token = os.getenv("BOT_TOKEN")
|
||||
db_path = os.getenv("DB_PATH")
|
||||
models.set_db(db_path)
|
||||
application = Application.builder().token(token).build()
|
||||
|
||||
|
||||
application.add_handler(journal.JournalHandler("journal", models).handler)
|
||||
# application.add_handler(CommandHandler("help", help_command))
|
||||
# on non command i.e message - echo the message on Telegram
|
||||
# application.add_handler(InlineQueryHandler(inline_query))
|
||||
|
||||
# Run the bot until the user presses Ctrl-C
|
||||
application.run_polling()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
35
bot/models.py
Normal file
35
bot/models.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from peewee import *
|
||||
from pathlib import Path
|
||||
|
||||
db = DatabaseProxy()
|
||||
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = db
|
||||
db_table = 'journal'
|
||||
|
||||
|
||||
# model for a single journal entry
|
||||
class JournalEntry(BaseModel):
|
||||
# the date of the entry
|
||||
date = DateField()
|
||||
# the text of the entry
|
||||
text = TextField()
|
||||
media_path = TextField()
|
||||
author = TextField()
|
||||
|
||||
|
||||
@property
|
||||
def media(self):
|
||||
return Path(self.media_path).open('rb')
|
||||
|
||||
@media.setter
|
||||
def media(self, media):
|
||||
self.media_path = Path(media).absolute()
|
||||
self.save()
|
||||
|
||||
|
||||
def set_db(db_path):
|
||||
db.initialize(SqliteDatabase(db_path))
|
||||
db.connect()
|
||||
db.create_tables([JournalEntry], safe=True)
|
Reference in New Issue
Block a user