From 8a08f9a26ea38a74064904cf701ab6c58a4367cb Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 10 May 2022 08:57:56 +0200 Subject: [PATCH] implemented /removeshare --- telegram_bot/api_handling/api_handler.py | 6 ++--- telegram_bot/bot.py | 32 +++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 35aef19..6d637d7 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -223,7 +223,7 @@ class API_Handler: Args: user_id (int): id of the user - isin (string): isin of the share + isin (string): identifier of the share (standard is isin) comment (string): comment of the share Returns: @@ -240,14 +240,14 @@ class API_Handler: Args: user_id (int): id of the user - symbol (string): symbol of the share + isin (string): identifier of the share (standard is isin) Returns: int: status code """ with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.delete(self.db_adress + "/share", json={"isin": isin}, headers=headers) # to delete a share only the isin is needed because it is unique, shares are not transactions! + req = s.delete(self.db_adress + "/share", json={"isin": str(isin)}, headers=headers) # to delete a share only the isin is needed because it is unique, shares are not transactions! return req.status_code diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 6f31bbf..b3158c7 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -455,6 +455,35 @@ def send_portfolio(message): bot.send_message(chat_id=user_id, text=f'*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}', parse_mode="MARKDOWN") # formatted message in markdown +@bot.message_handler(commands=['removeshare', 'Removeshare']) +def remove_share(message): + """ Remove share from portfolio + :type message: message object bot + :param message: message that was reacted to, in this case always '/removeshare' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + + bot.send_message(chat_id=user_id, text='Type ISIN/Symbol/CompanyName of share to remove (if you are unsure do /portfolio, find your share and insert the value above amount):') + bot.register_next_step_handler(message, remove_share_step) # wait for user to send ISIN, then call remove_share_step function + + +def remove_share_step(message): + user_id = int(message.from_user.id) + isin = str(message.text) + + status = api_handler.delete_share(int(user_id), str(isin)) # remove share from portfolio + + if status == 200: # statuscode 200 means share was removed successfully without errors + bot.send_message(chat_id=user_id, text=f'Share "{isin}" removed.') # checking if share to remove is in database are handled in database, not here + + else: + bot.send_message(chat_id=user_id, text=f'Failed deleting share "{isin}". (statuscode {status})\nMake sure that the share is in your portfolio and written exactly like there.') + + @bot.message_handler(commands=['newtransaction', 'Newtransaction']) #tbd not working rn may be deleted in future def set_new_transaction(message): """ Set new transaction for user @@ -469,10 +498,11 @@ def set_new_transaction(message): bot.send_message(chat_id=user_id, text='Type ",,," (time of transaction will be set to now, negative amount is selling, positive is buying):') bot.register_next_step_handler(message, set_new_transaction_step) + def set_new_transaction_step(message): user_id = int(message.from_user.id) - if not re.match(r"[A-Za-z0-9]+,[A-Za-z0-9]+,(-)?[0-9]+(.[0-9]+)?,[0-9]+(.[0-9]+)?", message.text): + if not re.match(r"[A-Za-z0-9 ]+,[A-Za-z0-9]+,(-)?[0-9]+(.[0-9]+)?,[0-9]+(.[0-9]+)?", message.text): bot.send_message(chat_id=user_id, text='Invalid format \n(e.g. Apple,US0378331005,53.2,120.4).\n Try again with /newtransaction.') return