diff --git a/Pipfile b/Pipfile index 1bfed8a..791c853 100644 --- a/Pipfile +++ b/Pipfile @@ -6,7 +6,6 @@ name = "pypi" [packages] python-telegram-bot = "*" peewee = "*" -requests = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 3265430..f9930f8 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "21390354000e1df138123000bc142ceaf899dbc2b971375f4f4fd7f6116a64f8" + "sha256": "b71137ebde2ce3e6bcde400dad601555ccee09d26530c8e928d76d49aae43fee" }, "pipfile-spec": 6, "requires": { @@ -32,87 +32,6 @@ "markers": "python_version >= '3.6'", "version": "==2022.12.7" }, - "charset-normalizer": { - "hashes": [ - "sha256:04afa6387e2b282cf78ff3dbce20f0cc071c12dc8f685bd40960cc68644cfea6", - "sha256:04eefcee095f58eaabe6dc3cc2262f3bcd776d2c67005880894f447b3f2cb9c1", - "sha256:0be65ccf618c1e7ac9b849c315cc2e8a8751d9cfdaa43027d4f6624bd587ab7e", - "sha256:0c95f12b74681e9ae127728f7e5409cbbef9cd914d5896ef238cc779b8152373", - "sha256:0ca564606d2caafb0abe6d1b5311c2649e8071eb241b2d64e75a0d0065107e62", - "sha256:10c93628d7497c81686e8e5e557aafa78f230cd9e77dd0c40032ef90c18f2230", - "sha256:11d117e6c63e8f495412d37e7dc2e2fff09c34b2d09dbe2bee3c6229577818be", - "sha256:11d3bcb7be35e7b1bba2c23beedac81ee893ac9871d0ba79effc7fc01167db6c", - "sha256:12a2b561af122e3d94cdb97fe6fb2bb2b82cef0cdca131646fdb940a1eda04f0", - "sha256:12d1a39aa6b8c6f6248bb54550efcc1c38ce0d8096a146638fd4738e42284448", - "sha256:1435ae15108b1cb6fffbcea2af3d468683b7afed0169ad718451f8db5d1aff6f", - "sha256:1c60b9c202d00052183c9be85e5eaf18a4ada0a47d188a83c8f5c5b23252f649", - "sha256:1e8fcdd8f672a1c4fc8d0bd3a2b576b152d2a349782d1eb0f6b8e52e9954731d", - "sha256:20064ead0717cf9a73a6d1e779b23d149b53daf971169289ed2ed43a71e8d3b0", - "sha256:21fa558996782fc226b529fdd2ed7866c2c6ec91cee82735c98a197fae39f706", - "sha256:22908891a380d50738e1f978667536f6c6b526a2064156203d418f4856d6e86a", - "sha256:3160a0fd9754aab7d47f95a6b63ab355388d890163eb03b2d2b87ab0a30cfa59", - "sha256:322102cdf1ab682ecc7d9b1c5eed4ec59657a65e1c146a0da342b78f4112db23", - "sha256:34e0a2f9c370eb95597aae63bf85eb5e96826d81e3dcf88b8886012906f509b5", - "sha256:3573d376454d956553c356df45bb824262c397c6e26ce43e8203c4c540ee0acb", - "sha256:3747443b6a904001473370d7810aa19c3a180ccd52a7157aacc264a5ac79265e", - "sha256:38e812a197bf8e71a59fe55b757a84c1f946d0ac114acafaafaf21667a7e169e", - "sha256:3a06f32c9634a8705f4ca9946d667609f52cf130d5548881401f1eb2c39b1e2c", - "sha256:3a5fc78f9e3f501a1614a98f7c54d3969f3ad9bba8ba3d9b438c3bc5d047dd28", - "sha256:3d9098b479e78c85080c98e1e35ff40b4a31d8953102bb0fd7d1b6f8a2111a3d", - "sha256:3dc5b6a8ecfdc5748a7e429782598e4f17ef378e3e272eeb1340ea57c9109f41", - "sha256:4155b51ae05ed47199dc5b2a4e62abccb274cee6b01da5b895099b61b1982974", - "sha256:49919f8400b5e49e961f320c735388ee686a62327e773fa5b3ce6721f7e785ce", - "sha256:53d0a3fa5f8af98a1e261de6a3943ca631c526635eb5817a87a59d9a57ebf48f", - "sha256:5f008525e02908b20e04707a4f704cd286d94718f48bb33edddc7d7b584dddc1", - "sha256:628c985afb2c7d27a4800bfb609e03985aaecb42f955049957814e0491d4006d", - "sha256:65ed923f84a6844de5fd29726b888e58c62820e0769b76565480e1fdc3d062f8", - "sha256:6734e606355834f13445b6adc38b53c0fd45f1a56a9ba06c2058f86893ae8017", - "sha256:6baf0baf0d5d265fa7944feb9f7451cc316bfe30e8df1a61b1bb08577c554f31", - "sha256:6f4f4668e1831850ebcc2fd0b1cd11721947b6dc7c00bf1c6bd3c929ae14f2c7", - "sha256:6f5c2e7bc8a4bf7c426599765b1bd33217ec84023033672c1e9a8b35eaeaaaf8", - "sha256:6f6c7a8a57e9405cad7485f4c9d3172ae486cfef1344b5ddd8e5239582d7355e", - "sha256:7381c66e0561c5757ffe616af869b916c8b4e42b367ab29fedc98481d1e74e14", - "sha256:73dc03a6a7e30b7edc5b01b601e53e7fc924b04e1835e8e407c12c037e81adbd", - "sha256:74db0052d985cf37fa111828d0dd230776ac99c740e1a758ad99094be4f1803d", - "sha256:75f2568b4189dda1c567339b48cba4ac7384accb9c2a7ed655cd86b04055c795", - "sha256:78cacd03e79d009d95635e7d6ff12c21eb89b894c354bd2b2ed0b4763373693b", - "sha256:80d1543d58bd3d6c271b66abf454d437a438dff01c3e62fdbcd68f2a11310d4b", - "sha256:830d2948a5ec37c386d3170c483063798d7879037492540f10a475e3fd6f244b", - "sha256:891cf9b48776b5c61c700b55a598621fdb7b1e301a550365571e9624f270c203", - "sha256:8f25e17ab3039b05f762b0a55ae0b3632b2e073d9c8fc88e89aca31a6198e88f", - "sha256:9a3267620866c9d17b959a84dd0bd2d45719b817245e49371ead79ed4f710d19", - "sha256:a04f86f41a8916fe45ac5024ec477f41f886b3c435da2d4e3d2709b22ab02af1", - "sha256:aaf53a6cebad0eae578f062c7d462155eada9c172bd8c4d250b8c1d8eb7f916a", - "sha256:abc1185d79f47c0a7aaf7e2412a0eb2c03b724581139193d2d82b3ad8cbb00ac", - "sha256:ac0aa6cd53ab9a31d397f8303f92c42f534693528fafbdb997c82bae6e477ad9", - "sha256:ac3775e3311661d4adace3697a52ac0bab17edd166087d493b52d4f4f553f9f0", - "sha256:b06f0d3bf045158d2fb8837c5785fe9ff9b8c93358be64461a1089f5da983137", - "sha256:b116502087ce8a6b7a5f1814568ccbd0e9f6cfd99948aa59b0e241dc57cf739f", - "sha256:b82fab78e0b1329e183a65260581de4375f619167478dddab510c6c6fb04d9b6", - "sha256:bd7163182133c0c7701b25e604cf1611c0d87712e56e88e7ee5d72deab3e76b5", - "sha256:c36bcbc0d5174a80d6cccf43a0ecaca44e81d25be4b7f90f0ed7bcfbb5a00909", - "sha256:c3af8e0f07399d3176b179f2e2634c3ce9c1301379a6b8c9c9aeecd481da494f", - "sha256:c84132a54c750fda57729d1e2599bb598f5fa0344085dbde5003ba429a4798c0", - "sha256:cb7b2ab0188829593b9de646545175547a70d9a6e2b63bf2cd87a0a391599324", - "sha256:cca4def576f47a09a943666b8f829606bcb17e2bc2d5911a46c8f8da45f56755", - "sha256:cf6511efa4801b9b38dc5546d7547d5b5c6ef4b081c60b23e4d941d0eba9cbeb", - "sha256:d16fd5252f883eb074ca55cb622bc0bee49b979ae4e8639fff6ca3ff44f9f854", - "sha256:d2686f91611f9e17f4548dbf050e75b079bbc2a82be565832bc8ea9047b61c8c", - "sha256:d7fc3fca01da18fbabe4625d64bb612b533533ed10045a2ac3dd194bfa656b60", - "sha256:dd5653e67b149503c68c4018bf07e42eeed6b4e956b24c00ccdf93ac79cdff84", - "sha256:de5695a6f1d8340b12a5d6d4484290ee74d61e467c39ff03b39e30df62cf83a0", - "sha256:e0ac8959c929593fee38da1c2b64ee9778733cdf03c482c9ff1d508b6b593b2b", - "sha256:e1b25e3ad6c909f398df8921780d6a3d120d8c09466720226fc621605b6f92b1", - "sha256:e633940f28c1e913615fd624fcdd72fdba807bf53ea6925d6a588e84e1151531", - "sha256:e89df2958e5159b811af9ff0f92614dabf4ff617c03a4c1c6ff53bf1c399e0e1", - "sha256:ea9f9c6034ea2d93d9147818f17c2a0860d41b71c38b9ce4d55f21b6f9165a11", - "sha256:f645caaf0008bacf349875a974220f1f1da349c5dbe7c4ec93048cdc785a3326", - "sha256:f8303414c7b03f794347ad062c0516cee0e15f7a612abd0ce1e25caf6ceb47df", - "sha256:fca62a8301b605b954ad2e9c3666f9d97f63872aa4efcae5492baca2056b74ab" - ], - "markers": "python_full_version >= '3.7.0'", - "version": "==3.1.0" - }, "h11": { "hashes": [ "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d", @@ -147,10 +66,10 @@ }, "peewee": { "hashes": [ - "sha256:03ba808b96bb3366fc7e949d669804d79059e01d472f0dfaddc2da5fa1614ee7" + "sha256:10769981198c7311f84a0ca8db892fa213303a8eb1305deb795a71e7bd606a91" ], "index": "pypi", - "version": "==3.16.1" + "version": "==3.16.2" }, "python-telegram-bot": { "hashes": [ @@ -160,14 +79,6 @@ "index": "pypi", "version": "==20.2" }, - "requests": { - "hashes": [ - "sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa", - "sha256:98b1b2782e3c6c4904938b84c0eb932721069dfdb9134313beff7c83c2df24bf" - ], - "index": "pypi", - "version": "==2.28.2" - }, "rfc3986": { "extras": [ "idna2008" @@ -185,14 +96,6 @@ ], "markers": "python_version >= '3.7'", "version": "==1.3.0" - }, - "urllib3": { - "hashes": [ - "sha256:8a388717b9476f934a21484e8c8e61875ab60644d29b9b39e11e4b9dc1c6b305", - "sha256:aa751d169e23c7479ce47a0cb0da579e3ede798f994f5816a74e4f4500dcea42" - ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'", - "version": "==1.26.15" } }, "develop": {} diff --git a/bot/commands/journal.py b/bot/commands/journal.py index f32c491..164c39b 100644 --- a/bot/commands/journal.py +++ b/bot/commands/journal.py @@ -51,9 +51,10 @@ class JournalHandler(BaseHandler): await 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, new = self.models.JournalEntry.get_or_create( - date = date - ) + with self.models.db: + self.current_model, new = self.models.JournalEntry.get_or_create( + date = date + ) if not new: await query.edit_message_text(text="An entry already exists for this date") return ConversationHandler.END @@ -69,9 +70,10 @@ class JournalHandler(BaseHandler): date = update.message.text try: date = datetime.datetime.strptime(date, "%d%m%Y").date() - self.current_model, new = self.models.JournalEntry.get_or_create( - date = date - ) + 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 diff --git a/bot/commands/list/list.py b/bot/commands/list/list.py index cc351ed..00f1c87 100644 --- a/bot/commands/list/list.py +++ b/bot/commands/list/list.py @@ -3,7 +3,7 @@ from pathlib import Path from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup -from .models import ListModel, set_db +from .models import ListModel, set_db, db MEDIA_DIR = Path(os.getenv("MEDIA_DIR")) DB_DIR = MEDIA_DIR / "lists_db" @@ -19,7 +19,7 @@ class ListHandler(BaseHandler): """Create and edit lists""" def __init__(self, entry_string, models): - self.models = models + self.journal_models = models # not needed here self.entry_string = entry_string self.handler = ConversationHandler( entry_points=[CommandHandler(entry_string, self.entry_point)], @@ -47,7 +47,8 @@ class ListHandler(BaseHandler): async def entry_point(self, update, context) -> None: await super().entry_point(update, context) set_db(DB_DIR / f"{update.message.chat_id}.db") - lists = ListModel.select() + with db: + lists = ListModel.select() keyboard = [[InlineKeyboardButton(k.name, callback_data=f"list-{k.name}")] for k in lists] + \ [[InlineKeyboardButton("New list", callback_data="new")]] @@ -103,7 +104,8 @@ class ListHandler(BaseHandler): async def new_listname(self, update, context) -> None: name = update.message.text try: - ListModel.create(name = name) + with db: + ListModel.create(name = name) keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("To the menu!", callback_data="overview")]] reply_markup = InlineKeyboardMarkup(keyboard) self.current_name = name @@ -124,7 +126,8 @@ class ListHandler(BaseHandler): async def list_remove(self, update, context) -> None: query = update.callback_query await query.answer() - list_object = ListModel.get(name = self.current_name) + with db: + list_object = ListModel.get(name = self.current_name) keyboard = [[InlineKeyboardButton(k, callback_data=i)] for i,k in enumerate(list_object.content_list)] reply_markup = InlineKeyboardMarkup(keyboard) @@ -136,7 +139,8 @@ class ListHandler(BaseHandler): async def list_clear(self, update, context) -> None: query = update.callback_query await query.answer() - ListModel.get(name = self.current_name).content_list = [] + with db: + ListModel.get(name = self.current_name).content_list = [] keyboard = [[InlineKeyboardButton("Add an item", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]] reply_markup = InlineKeyboardMarkup(keyboard) await query.edit_message_text("List " + self.current_name + " cleared", reply_markup=reply_markup) @@ -146,7 +150,8 @@ class ListHandler(BaseHandler): async def list_delete(self, update, context) -> None: query = update.callback_query await query.answer() - ListModel.get(name = self.current_name).delete_instance() + with db: + ListModel.get(name = self.current_name).delete_instance() await query.edit_message_text("List " + self.current_name + " deleted") return ConversationHandler.END @@ -154,7 +159,8 @@ class ListHandler(BaseHandler): async def list_print(self, update, context) -> None: query = update.callback_query await query.answer() - it = ListModel.get(name = self.current_name).content_list + with db: + it = ListModel.get(name = self.current_name).content_list if it: content = "·" + "\n· ".join(it) else: @@ -168,7 +174,8 @@ class ListHandler(BaseHandler): async def list_add_item(self, update, context) -> None: item = update.message.text - ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item] + with db: + ListModel.get(name = self.current_name).content_list = ListModel.get(name = self.current_name).content_list + [item] # TODO test me! keyboard = [[InlineKeyboardButton("Add some more", callback_data="add"), InlineKeyboardButton("Back to the menu", callback_data="overview")]] reply_markup = InlineKeyboardMarkup(keyboard) @@ -181,10 +188,11 @@ class ListHandler(BaseHandler): ind = int(query.data) await query.answer() - list_object = ListModel.get(name = self.current_name) - old = list_object.content_list - name = old.pop(ind) - list_object.content_list = old + with db: + list_object = ListModel.get(name = self.current_name) + old = list_object.content_list + name = old.pop(ind) + list_object.content_list = old keyboard = [[InlineKeyboardButton("Remove another", callback_data="remove"), InlineKeyboardButton("Back to the menu", callback_data="overview")]] reply_markup = InlineKeyboardMarkup(keyboard) diff --git a/bot/commands/list/models.py b/bot/commands/list/models.py index 5b81d9a..f672f47 100644 --- a/bot/commands/list/models.py +++ b/bot/commands/list/models.py @@ -19,7 +19,8 @@ class ListModel(BaseModel): @content_list.setter def content_list(self, list_content): self.content = json.dumps(list_content) - self.save() + with db: + self.save() diff --git a/bot/commands/status.py b/bot/commands/status.py index cf64da3..8657446 100644 --- a/bot/commands/status.py +++ b/bot/commands/status.py @@ -1,8 +1,9 @@ import datetime -import requests +import httpx import socket -from telegram.ext import ConversationHandler, CommandHandler, CallbackQueryHandler, ParseMode +from telegram.ext import ConversationHandler, CommandHandler, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup +from telegram.constants import ParseMode @@ -27,7 +28,7 @@ class StatusHandler(BaseHandler): async def entry_point(self, update, context) -> None: - super().entry_point(update, context) + await super().entry_point(update, context) keyboard = [ [ InlineKeyboardButton("And the log?", callback_data="full"), @@ -36,10 +37,10 @@ class StatusHandler(BaseHandler): reply_markup = InlineKeyboardMarkup(keyboard) delta = str(datetime.datetime.now() - self.start_time) - message = "BeebBop, this is Norbit)\n" + message = "BeebBop, this is Norbit\n" try: - ip = requests.get('https://api.ipify.org').text + ip = httpx.get('https://api.ipify.org').text with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.connect(('8.8.8.8', 80)) (addr, port) = s.getsockname() @@ -49,15 +50,14 @@ class StatusHandler(BaseHandler): local_ips = "not fetchable" message += "Status: Running 🟢\n" - message += "Uptime: `" + delta[:delta.rfind(".")] + "`\n" - message += "IP (public): `" + ip + "`\n" - message += "IP (private): `" + str(local_ips) + "`\n" + message += f"Uptime: `{delta[:delta.rfind('.')]}`\n" + message += f"IP \(public\): `{ip}`\n" + message += f"IP \(private\): `{local_ips}`\n" - if update.message: - await update.message.reply_text(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) + await update.message.reply_text(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN_V2) else: - await update._effective_chat.send_message(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) + await update._effective_chat.send_message(message, reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN_V2) return FIRST @@ -65,10 +65,7 @@ class StatusHandler(BaseHandler): async def send_log(self, update, context) -> None: query = update.callback_query wanted = query.data.replace("status-","") - query.answer() - # with open("persistence/server.log") as l: - # query.message.reply_document(l) - await update.message.reply_text("Not implemented yet.") - super().log_activity(read = False, execute = False, send = True) + await query.answer() + await query.edit_message_text("Here you go: https://portainer.kluster.moll.re/#!/1/kubernetes/applications/journal/journal-botte") return ConversationHandler.END diff --git a/bot/main.py b/bot/main.py index c447f0c..7edbee8 100644 --- a/bot/main.py +++ b/bot/main.py @@ -2,12 +2,14 @@ import os from telegram.ext import Application import logging -from commands import journal #, status +from commands import journal, status from commands.list import list import models logging.basicConfig( - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + # level=logging.INFO + level=logging.DEBUG ) logger = logging.getLogger(__name__) @@ -24,7 +26,7 @@ def main() -> None: application.add_handler(journal.JournalHandler("journal", models).handler) application.add_handler(list.ListHandler("list", models).handler) - # application.add_handler(status.StatusHandlerl("status", models).handler) + application.add_handler(status.StatusHandler("status", 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)) diff --git a/bot/models.py b/bot/models.py index 579111b..b62534c 100644 --- a/bot/models.py +++ b/bot/models.py @@ -25,6 +25,7 @@ class JournalEntry(BaseModel): author = TextField(null=True) text = TextField(null=True) media_path = TextField(null=True) + last_shown = DateField(null=True) @property diff --git a/deployment/deployment.yaml b/deployment/deployment.yaml index 9f1ee63..fdc254e 100644 --- a/deployment/deployment.yaml +++ b/deployment/deployment.yaml @@ -19,6 +19,7 @@ spec: containers: - name: journal image: mollre/journal-bot:latest + imagePullPolicy: Always envFrom: - secretRef: name: journal-secret-env