From 2b18e026239a0c62fe7480270303c7efea79e0bc Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Wed, 20 Apr 2022 11:03:21 +0200 Subject: [PATCH] update /update function --- telegram_bot/bot.py | 68 +++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c194789..c30bfdf 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -190,42 +190,50 @@ def send_status(message): bot.reply_to(message, "bot is running") -@bot.message_handler(commands=['update', 'Update']) # /update -> update shares -def send_update(message): +@bot.message_handler(commands=['update', 'Update']) # /update -> update shares +def update_for_user(message): - """ Send update on shares - :type message: message object bot - :param message: message that was reacted to, in this case always containing '/help' + p_user_id = int(message.from_user.id) + p_my_handler = api_handler + + share_symbols = [] + share_amounts = [] + share_courses = [] + + my_portfolio = p_my_handler.get_user_portfolio(p_user_id) + + for element in my_portfolio: + if element["count"] != '' and element["symbol"]!= '': + print(element["count"], element["symbol"]) + share_symbols.append(element["symbol"]) + share_amounts.append(element["count"]) + share_courses.append(element["current_price"]) + + my_user = p_my_handler.get_user(p_user_id) + send_to_user("Hello %s this is your share update:"%str(my_user["username"]), pUser_id=p_user_id) + + if len(share_symbols) != 0: + for i in range(len(share_symbols)): + my_update_message = f'Symbol: {share_symbols[i]}\nCurrent Price per Share: {share_courses[i]}\nAmount owned: {share_amounts[i]}\nTotal Investment: {float(share_courses[i]) * float(share_amounts[i])}' + send_to_user(my_update_message, pUser_id=p_user_id) + else: + send_to_user("No shares found for your account. Check https://gruppe1.testsites.info to change your settings and add shares.", pUser_id=p_user_id) + + +def send_to_user(pText, pUser_id = 1770205310): + + """ Send message to user + :type pText: string + :param pText: Text to send to user + + :type pUser_id: int + :param pUser_id: user to send to. per default me (Florian Kellermann) :raises: none :rtype: none """ - user_id = int(message.from_user.id) - - #Can be deleted when getting from database - dirname = os.path.dirname(__file__) - json_path = os.path.join(dirname, 'shares/shares_example.json') - - with open(json_path) as json_file: - json_share_data = json.load(json_file) - int_share_count = int(json_share_data['share_count']) - str_username = str(json_share_data['user']) - bot.send_message(chat_id=user_id, text=f'Hello {str_username}. Here is the update on your currently owned shares:') - - - for i in range(int_share_count): - - my_share = json_share_data['shares'][i] - my_share_symbol = str(my_share['symbol']) - my_share_amount = float(my_share['amount_bought']) - my_share_buy_price = float(my_share['price_bought']) - my_share_course = float(share_fetcher.get_share_price(my_share_symbol)) - - - 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)}' - bot.send_message(chat_id=user_id, text=my_update_message) + bot.send_message(chat_id=pUser_id, text=pText) @bot.message_handler(commands=['share', 'Share']) # /share -> get share price