diff --git a/__pycache__/notion_client.cpython-39.pyc b/__pycache__/notion_client.cpython-39.pyc new file mode 100644 index 0000000..3d12d5d Binary files /dev/null and b/__pycache__/notion_client.cpython-39.pyc differ diff --git a/notion_client.py b/notion_client.py index 0a6930d..b539900 100644 --- a/notion_client.py +++ b/notion_client.py @@ -1,25 +1,35 @@ import configparser from notion.client import NotionClient import pandas as pd +from pandas.core.indexing import convert_from_missing_indexer_tuple -def get_dicts_of_all_items(cv) -> 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