BIN
__pycache__/notion_client.cpython-39.pyc
Normal file
BIN
__pycache__/notion_client.cpython-39.pyc
Normal file
Binary file not shown.
@@ -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()]
|
||||
|
||||
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
|
||||
|
||||
def main():
|
||||
class CustomNotionClient():
|
||||
def __init__(self) -> None:
|
||||
config = configparser.ConfigParser()
|
||||
config.read('credentials.ini')
|
||||
|
||||
client = NotionClient(token_v2=config.get("notion", "token"))
|
||||
self.client = NotionClient(token_v2=config.get("notion", "token"))
|
||||
self.cv = self.client.get_collection_view(config.get("notion", "database_url"))
|
||||
|
||||
# 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_dicts_of_all_items(self) -> list:
|
||||
return [row.get_all_properties() for row in self.cv.collection.get_rows()]
|
||||
|
||||
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
|
||||
|
||||
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()
|
||||
__main()
|
||||
@@ -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
|
||||
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)
|
||||
logger = logging.getLogger(__name__)
|
||||
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")
|
||||
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()
|
||||
if __name__ == '__main__':
|
||||
food_bot = FoodBot()
|
||||
Reference in New Issue
Block a user