From 63c64f389d2d245ed0edc8f82fa15aae484eb51c Mon Sep 17 00:00:00 2001 From: cato447 Date: Sat, 4 Sep 2021 03:38:42 +0200 Subject: [PATCH] Added basic output functionality Closes #1 --- __pycache__/notion_client.cpython-39.pyc | Bin 0 -> 2516 bytes notion_client.py | 38 +++++++++------ telegram_bot.py | 56 ++++++++++------------- 3 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 __pycache__/notion_client.cpython-39.pyc diff --git a/__pycache__/notion_client.cpython-39.pyc b/__pycache__/notion_client.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3d12d5d583f7be2f69d0e647e2cbcf937802b8be GIT binary patch literal 2516 zcmb7G&u`l{6ecNIR$M3D(k%V4tqpeACKPdopujFcmd-_X8G-`EE&>KZ73oZlEV(4@ zZiaJ8t~>0!4Tz8VFZOrvy3@|P^|bdW$4wk(MFoiF5532a-}m05ljY?Gf#>M2?c|M5 z$lo}){CF_fgI7O?Mi4=B65}t+X^+K>R&!qL!+vOp|}2K?5= zEzv}z4%@O_Z4b05dzX* z;Z2zEf2Cu>Yh%)_odwhR+JsGTwk`-z6C8|gr%DQ-BTI7K%8D!w3P+Nynes>$5sgFF z;lu5?DH4;sOtj~8%4&h=KBnDWV!ueaKzkX!BPV81NI(*e+z zps#OBvsHoT)n={!FE->LXl6LCgEt*QLug1FY>kdSzD|AX;_EBKqX?+GBYqt^sP`!W zVo4XMpO9zd1S8EmxMGHfhj0g?Vdphy4#DKdrqhc|$ZzbG|Drl2u)K=ABzeyJs_a8d zW>TLs%UWMqPnEA77gh5CII@kMJkusEd;MKx>0fxs=$nP-LBAqPjpk*?ZIBt+)2*Gn zOhNKp=v;V;Jk@m0-^yb{GzS3soT_D5oC6Zy<9zW3EAX*K1_&KJeh**^8Fa*d3BEAm z#{?;#&|^B40TS=NlVA+J9Wq8K{Qr^SAvkhUtfLfy42W72r0~bII~C=eVh+}{NtB70 zx&sE*H(5{49bbI}E4ZpKDn?Hiob#f@%u>gcyq^>zStvn#D|wkGgARN>=tZ7oeGxA` zgRI+ENl)6x{Q3?z2>`fJFuR01chGzeynS#pwhzHlW33@{g|5@l=JkDBH(OuRaF#q9 z;Bis%fR^4F)UL_9wEG+!IPD%DQv*_W=?N%!X*6)^CaiLMod7J}dAH8EQ#Hg{r5R_>9{lQ+*3CY5Ib-9Lh0P+l~6kfhir5?aU zMAZiTsQcJp*{K>=kk%0^M1+B>MDs**Z4_LlXm(Krq8k8Sw~CgqY$GR}+Xm-7;1)RM zw#oUcL6TQ13WL=8SiTgdbOjCrM16tHTZFEMFcq?eVXg5GNk~`fD_&R&A$nk#!aIAA ziCNTs0s})%$T4yO>-}Rt4eLc82P|8&ZCCHn=GFCYgIR$+cFNS!lKYayf&>eX{jV!?C_rk^iX&v jcUtFGVNJaRV@8w>cPi3%F)TC|^N= list: - return [row.get_all_properties() for row in cv.collection.get_rows()] +class CustomNotionClient(): + def __init__(self) -> None: + config = configparser.ConfigParser() + config.read('credentials.ini') -def convert_database_to_pandas(cv): - dataframe = pd.DataFrame(get_dicts_of_all_items(cv)) - dataframe['ablaufdatum'] = [None if time is None else time.start for time in dataframe['ablaufdatum']] - return dataframe + self.client = NotionClient(token_v2=config.get("notion", "token")) + self.cv = self.client.get_collection_view(config.get("notion", "database_url")) -def main(): - config = configparser.ConfigParser() - config.read('credentials.ini') + def __get_dicts_of_all_items(self) -> list: + return [row.get_all_properties() for row in self.cv.collection.get_rows()] - client = NotionClient(token_v2=config.get("notion", "token")) + def convert_database_to_pandas(self): + dataframe = pd.DataFrame(self.__get_dicts_of_all_items()) + dataframe['ablaufdatum'] = [None if time is None else time.start for time in dataframe['ablaufdatum']] + return dataframe - # Access a database using the URL of the database page or the inline block - cv = client.get_collection_view(config.get("notion", "database_url")) - dataframe = convert_database_to_pandas(cv) + def get_names_of_available_products(self): + names = [f"- {row.name}" for row in self.cv.collection.get_rows() if int(row.anzahl) > 0] + return '\n'.join(names) + + def get_contents_as_string(self): + self.convert_database_to_pandas() + +def __main(): + notion_client = NotionClient() + dataframe = notion_client.convert_database_to_pandas() dataframe.to_csv("test.csv") if __name__ == '__main__': - main() \ No newline at end of file + __main() \ No newline at end of file diff --git a/telegram_bot.py b/telegram_bot.py index d3dd238..f778274 100644 --- a/telegram_bot.py +++ b/telegram_bot.py @@ -1,43 +1,35 @@ -from telegram import update +import configparser +import logging +import re + from telegram.ext import Updater from telegram.ext import CommandHandler from telegram.ext import MessageHandler, Filters -import configparser -import logging -logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - level=logging.INFO) -logger = logging.getLogger(__name__) +from notion_client import CustomNotionClient +class FoodBot(): + def __init__(self) -> None: + logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + self.logger = logging.getLogger(__name__) -config = configparser.ConfigParser() -config.read('credentials.ini') -updater = Updater(token=config.get("telegram", "token"), use_context=True) -dispatcher = updater.dispatcher -logger.info("Started bot") + config = configparser.ConfigParser() + config.read('credentials.ini') + self.updater = Updater(token=config.get("telegram", "token"), use_context=True) + self.dispatcher = self.updater.dispatcher -def start(update, context): - logger.info(f"Recieved /start message") - context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!") + self.notion_client = CustomNotionClient() -start_handler = CommandHandler('start', start) -dispatcher.add_handler(start_handler) + food_handler = MessageHandler(Filters.regex(re.compile('^food$|^essen$', re.IGNORECASE)), self.get_food) + self.dispatcher.add_handler(food_handler) -def echo(update, context): - logger.info(f"Recieved message: {update.message.text}") - context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text) + self.updater.start_polling() -echo_handler = MessageHandler(Filters.text & (~Filters.command), echo) -dispatcher.add_handler(echo_handler) + self.logger.info("Started bot") -def get_food(update, context): - logger.info("Getting food") - if len(context.args) > 0: - text_caps = ' '.join(context.args).upper() - context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps) - else: - context.bot.send_message(chat_id=update.effective_chat.id, text="No text passed!") + def get_food(self, update, context): + self.logger.info("Getting food") + context.bot.send_message(chat_id=update.effective_chat.id, text=self.notion_client.get_names_of_available_products()) -food_handler = CommandHandler('food', get_food) -dispatcher.add_handler(food_handler) - -updater.start_polling() \ No newline at end of file +if __name__ == '__main__': + food_bot = FoodBot() \ No newline at end of file