From 543c51397e64fd1808e2e7b69d934fab8dfae10d Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Sun, 17 Apr 2022 11:57:28 +0200 Subject: [PATCH] added transaction and portfolio commands --- telegram_bot/api_handling/api_handler.py | 4 ++ telegram_bot/bot.py | 64 +++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index fa4a37e..c06e6ee 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -33,6 +33,10 @@ class API_Handler: get_user_transactions(user_id): gets the transactions of the user set_transaction(user_id, transaction): sets the transaction of the user delete_transaction(user_id, transaction): deletes the transaction of the user + get_user_portfolio(user_id): gets the portfolio of the user + set_portfolio(user_id, portfolio): sets the portfolio of the user + delete_portfolio(user_id, portfolio): deletes the portfolio of the user + set_cron_interval(user_id, interval): sets the cron interval of the user """ diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 83582ba..b1d985b 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -14,14 +14,13 @@ __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 -import time import sys import logging import json +import re import news.news_fetcher as news import shares.share_fetcher as share_fetcher @@ -380,6 +379,67 @@ def send_keywords(message): bot.send_message(chat_id=user_id, text=f'Your keywords are: _{keywords_str}_', parse_mode="MARKDOWN") +@bot.message_handler(commands=['portfolio']) +def send_portfolio(message): + """ Send portfolio of user + :type message: message object bot + :param message: message that was reacted to, in this case always '/portfolio' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + portfolio = api_handler.get_user_portfolio(user_id) + if portfolio == 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 portfolio: + bot.send_message(chat_id=user_id, text='You do not have any stocks in your portfolio.') + return + else: + portfolio_str = ', '.join(portfolio) # tbd: format portfolio + bot.send_message(chat_id=user_id, text=f'Your portfolio is: _{portfolio_str}_', parse_mode="MARKDOWN") + + +@bot.message_handler(commands=['newtransaction']) +def set_new_transaction(message): + """ Set new transaction for user + :type message: message object bot + :param message: message that was reacted to, in this case always '/newtransaction' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + bot.send_message(chat_id=user_id, text='Type ",," (time of transaction will be set to now):') + 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]+,[0-9]+[.[0-9]+]?,[0-9]+[.[0-9]+]?", message.text): + bot.send_message(chat_id=user_id, text='Invalid format. Try again with /newtransaction.') + return + + transaction_data = str(message.text).split(',') + symbol = str(transaction_data[0]) + amount = float(transaction_data[1]) + price = float(transaction_data[2]) + time = dt.datetime.now() + + status = api_handler.set_transaction(user_id, amount, price, symbol, time) + + if status == 200: + bot.send_message(chat_id=user_id, text='Transaction succesfully added.') + else: + bot.send_message(chat_id=user_id, text=f'Failed adding transaction. (statuscode {status})') + + + + + @bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement def echo_all(message):