From c489e9ab46a3a6723669bd483ce59495b3dcc58b Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 28 Mar 2022 19:39:37 +0200 Subject: [PATCH] added keywords from db (not working currently) --- telegram_bot/api_handler.py | 6 +++--- telegram_bot/bot.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py index 45c5de4..93d5c2f 100644 --- a/telegram_bot/api_handler.py +++ b/telegram_bot/api_handler.py @@ -34,7 +34,7 @@ class API_Handler: self.token = None def reauthorize(self, email, password): - """reauthorizes the user + """set new credentials Args: email (string): email of the user @@ -207,7 +207,7 @@ if __name__ == "__main__": print("This is a module for the telegram bot. It is not intended to be run directly.") handler = API_Handler("https://aktienbot.flokaiser.com/api", "bot@example.com", "bot") - - keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is mine (Linus) + print(handler.token) + keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) print(keywords) sys.exit(1) \ No newline at end of file diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index e3e4f28..c62083f 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -28,12 +28,17 @@ import shares.share_fetcher as share_fetcher from telebot import types from dotenv import load_dotenv +from api_handler import API_Handler + load_dotenv() bot_version = "0.2.1" user_list = [] +#create api handler +api_handler = API_Handler("https://aktienbot.flokaiser.com", os.getenv("BOT_EMAIL"), os.getenv("BOT_PASSWORD")) + class User: # Currently saving users in this class to test functionality -> later database def __init__(self, p_user_id, p_user_name, p_chat_id): @@ -222,11 +227,11 @@ def send_news(message): :rtype: none """ - keyword = "bitcoin" user_id = int(message.from_user.id) - #Get Information for user with this id + keywords = api_handler.get_keywords(user_id) + keyword = keywords[0] - articles = news.get_top_news_by_keyword(keyword) #tbd: get keyword from db + articles = news.get_top_news_by_keyword(keywords[0]) try: formatted_article = news.format_article(articles["articles"][0]) except IndexError: @@ -235,6 +240,27 @@ def send_news(message): bot.send_message(chat_id=user_id, text=f"_keyword: {keyword}_\n\n" + formatted_article, parse_mode="MARKDOWN") +@bot.message_handler(commands=['addkeyword']) +def add_keyword(message): + """ Add keyword to user + :type message: message object bot + :param message: message that was reacted to, in this case always '/addkeyword' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + bot.send_message(chat_id=user_id, text='Type keyword to add:') + bot.register_next_step_handler(message, store_keyword) + +def store_keyword(message): + user_id = int(message.from_user.id) + keyword = str(message.text) + api_handler.add_keyword(user_id, keyword) + bot.send_message(chat_id=user_id, text=f'Keyword {keyword} added.') + + @bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement def echo_all(message):