From 96e96b4612922f155278fc205a67a23f4546ae0f Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:59:00 +0200 Subject: [PATCH] updated functions, added fail handling and messages --- telegram_bot/bot.py | 88 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 15 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index be23eb4..c50ad61 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -14,6 +14,7 @@ __license__ = "None" # API Documentation https://core.telegram.org/bots/api # Code examples https://github.com/eternnoir/pyTelegramBotAPI#getting-started +from ast import keyword import os import telebot @@ -45,8 +46,8 @@ class User: # Currently saving users in this class to test functionality -> late def __init__(self, p_user_id, p_user_name, p_chat_id): """ Initialize a new user - :type self: - :param self: for class + :type self: User + :param self: object of the class :type p_user_id: int :param p_user_id: telegram user id @@ -115,7 +116,7 @@ def send_welcome(message): :rtype: none """ - bot.reply_to(message, "/id or /auth for authentication. /update to get updates on your shares. /users to see all users. /news to get current use for your keywords. /share to get price of specific share. For further details see aktienbot.flokaiser.com") + bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/users see all users.\n/news get top article for each keyword.\n/allnews get all news (last 7 days)\n/keywords get all your keywords\n/addkeyword add a keyword\n/removekeyword remove a keyword\n/share get price of specific share\n\n_For further details see https://gruppe1.testsites.info _", parse_mode='MARKDOWN') @bot.message_handler(commands=['users']) @@ -152,7 +153,7 @@ def send_id(message): :rtype: none """ - answer = 'Your ID/Authentication Code is: [' + str(message.from_user.id) + ']. Enter this code in the settings on aktienbot.flokaiser.com to get updates on your shares.' + answer = 'Your ID/Authentication Code is: [' + str(message.from_user.id) + ']. Enter this code in the settings on https://gruppe1.testsites.info to get updates on your shares.' bot.reply_to(message, answer) @@ -205,7 +206,7 @@ def send_update(message): my_update_message = f'Symbol: {my_share_symbol}\nPrice: {my_share_course}\nBought for: {my_share_buy_price}\n\ -Amount owned: {my_share_amount}\nWin/Lose: {(my_share_amount*my_share_course) - (my_share_amount*my_share_buy_price)}' + Amount owned: {my_share_amount}\nWin/Lose: {(my_share_amount*my_share_course) - (my_share_amount*my_share_buy_price)}' bot.send_message(chat_id=user_id, text=my_update_message) @@ -232,12 +233,12 @@ def send_share_price(message): bot.reply_to(message, str_share_price) -@bot.message_handler(commands=['news']) -def send_news(message): +@bot.message_handler(commands=['allnews']) +def send_all_news(message): """ Get news for keywords of user :type message: message object bot - :param message: message that was reacted to, in this case always containing '/news' + :param message: message that was reacted to, in this case always containing '/allnews' :raises: none @@ -246,13 +247,22 @@ def send_news(message): user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) + + if keywords == None: + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure too connect your telegram id (/id) on https://gruppe1.testsites.info') + return + + if not keywords: + bot.send_message(chat_id=user_id, text='You have no keywords. Please add some keywords with /news') + return + keywords_search = ' OR '.join(keywords) print(keywords_search) now = dt.datetime.now().date() from_date = now - dt.timedelta(days=7) from_date_formatted = dt.datetime.strftime(from_date, '%Y-%m-%d') print(from_date_formatted) - news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)["articles"][:5] + news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)["articles"] if news_list: for article in news_list: @@ -262,6 +272,40 @@ def send_news(message): bot.send_message(chat_id=user_id, text='No news found for your keywords.') +@bot.message_handler(commands=['news']) +def send_news(message): + """ Get news for keywords of user + + :type message: message object bot + :param message: message that was reacted to, in this case always containing '/news' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + keywords = api_handler.get_user_keywords(user_id) + + if keywords == None: + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure too connect your telegram id (/id) on https://gruppe1.testsites.info') + return + + if not keywords: + bot.send_message(chat_id=user_id, text='You have no keywords. Please add some keywords with /addkeyword') + return + + if keywords: + for keyword in keywords: + top_news = news.get_top_news_by_keyword(keyword)["articles"] + if top_news == None: + bot.send_message(chat_id=user_id, text='News Server did not respond correctly. Try again later.') + return + if not top_news: + bot.send_message(chat_id=user_id, text=f'No news found for keyword: *{keyword}*', parse_mode="MARKDOWN") + return + + formatted_article = news.format_article(top_news[0]) + bot.send_message(chat_id=user_id, text=f"_keyword: {keyword}_\n\n" + formatted_article, parse_mode="MARKDOWN") @bot.message_handler(commands=['addkeyword']) @@ -282,8 +326,11 @@ def store_keyword(message): user_id = int(message.from_user.id) print(str(user_id)) keyword = str(message.text).lower() - api_handler.set_keyword(user_id, keyword) - bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" added.') + status = api_handler.set_keyword(user_id, keyword) + if status == 200: + bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" added.') + else: + bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" could not be stored. (statuscode {status})') @bot.message_handler(commands=['removekeyword']) @@ -303,8 +350,11 @@ def remove_keyword(message): def remove_keyword_step(message): user_id = int(message.from_user.id) keyword = str(message.text).lower() - api_handler.delete_keyword(user_id, keyword) - bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" removed.') + status = api_handler.delete_keyword(user_id, keyword) + if status == 200: + bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" removed.') + else: + bot.send_message(chat_id=user_id, text=f'Failed deleting keyword "{keyword}". (statuscode {status})') @bot.message_handler(commands=['keywords']) @@ -319,10 +369,18 @@ def send_keywords(message): """ user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) - bot.send_message(chat_id=user_id, text=f'Your keywords are: {keywords}') + if keywords == None: + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure too connect your telegram id (/id) on https://gruppe1.testsites.info') + return + if not keywords: + bot.send_message(chat_id=user_id, text='No keywords set for this account. Add keywords by using /addkeyword') + return + else: + keywords_str = ', '.join(keywords) + bot.send_message(chat_id=user_id, text=f'Your keywords are: _{keywords_str}_', parse_mode="MARKDOWN") -@bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement +@bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement def echo_all(message): """ Tell that command is not known if it is no known command