From 7a4a8aaedca5b0e6e0508980a3b1d8dedb72788e Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 19 Apr 2022 21:36:16 +0200 Subject: [PATCH] fixed case sensitive commands --- telegram_bot/api_handling/api_handler.py | 9 ++-- telegram_bot/bot.py | 55 ++++++++++++------------ 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index c5bc997..49751f0 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -270,22 +270,23 @@ class API_Handler: return self.get_user_transactions(user_id, max_retries-1) - def set_transaction(self, user_id, count, price, symbol, timestamp): + def set_transaction(self, user_id, comment, isin, count, price, time): """sets the transaction of the user Args: user_id (int): id of the user + comment (string): comment of the transaction + isin (string): isin of the transaction count (int): count of the transaction price (float): price of the transaction - symbol (string): symbol of the transaction - timestamp (string): timestamp of the transaction + time (string): time of the transaction Returns: int: status code """ with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - transaction = {"count": count, "price": price, "symbol": symbol, "time": timestamp} + transaction = {"comment": comment, "count": count, "isin": isin, "price": price, "time": time} req = s.post(self.db_adress + "/transaction", json=transaction, headers=headers) return req.status_code diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 0f32fe6..c194789 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -68,7 +68,7 @@ class User: # Currently saving users in this class to test functionality -> late bot = telebot.TeleBot(os.getenv('BOT_API_KEY')) -@bot.message_handler(commands=['start']) # /start -> saving as new user and sending welcome +@bot.message_handler(commands=['start', 'Start']) # /start -> saving as new user and sending welcome def send_start(message): """ Description @@ -90,7 +90,7 @@ def send_start(message): bot.reply_to(message, "Welcome to this share bot project. Type /help to get information on what this bot can do") -@bot.message_handler(commands=['version']) +@bot.message_handler(commands=['version', 'Version']) def send_version(message): """ Sending programm version @@ -104,7 +104,7 @@ def send_version(message): bot.reply_to(message, bot_version) -@bot.message_handler(commands=['help']) # /help -> sending all functions +@bot.message_handler(commands=['help', 'Help']) # /help -> sending all functions def send_welcome(message): """ Send all functions @@ -118,7 +118,7 @@ def send_welcome(message): bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/users see all users.\n/me get my user info\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/portfolio see own portfolio\n/newtransaction add new transaction\n/interval get update interval\n/setinterval set update interval\n_For further details see https://gruppe1.testsites.info _", parse_mode='MARKDOWN') -@bot.message_handler(commands=['users']) +@bot.message_handler(commands=['users', 'Users']) # /users -> sending all users def send_all_users(message): """ Send all users, only possible for admins @@ -141,7 +141,7 @@ def send_all_users(message): bot.send_message(chat_id=user_id, text=answer) -@bot.message_handler(commands=['me']) +@bot.message_handler(commands=['me', 'Me']) # /me -> sending user info def send_user(message): """ Send user data :type message: message object bot @@ -160,7 +160,7 @@ def send_user(message): bot.reply_to(message, 'Your user data:\n' + str(user_data)) -@bot.message_handler(commands=['id', 'auth']) # /auth or /id -> Authentication with user_id over web tool +@bot.message_handler(commands=['id', 'auth', 'Id', 'Auth']) # /auth or /id -> Authentication with user_id over web tool def send_id(message): """ Send user id for authentication with browser @@ -176,7 +176,7 @@ def send_id(message): #function that sends telegram status(running or offline) as message from telegram bot to user -@bot.message_handler(commands=['status']) +@bot.message_handler(commands=['status', 'Status']) def send_status(message): """ Sends status to user @@ -190,7 +190,7 @@ def send_status(message): bot.reply_to(message, "bot is running") -@bot.message_handler(commands=['update']) +@bot.message_handler(commands=['update', 'Update']) # /update -> update shares def send_update(message): """ Send update on shares @@ -228,7 +228,7 @@ def send_update(message): bot.send_message(chat_id=user_id, text=my_update_message) -@bot.message_handler(commands=['share']) +@bot.message_handler(commands=['share', 'Share']) # /share -> get share price def send_share_update(message): """ Send price of a specific share @@ -251,7 +251,7 @@ def send_share_price(message): bot.reply_to(message, str_share_price) -@bot.message_handler(commands=['allnews']) +@bot.message_handler(commands=['allnews', 'Allnews']) # /allnews -> get all news def send_all_news(message): """ Get news for keywords of user @@ -290,7 +290,7 @@ def send_all_news(message): bot.send_message(chat_id=user_id, text='No news found for your keywords.') -@bot.message_handler(commands=['news']) +@bot.message_handler(commands=['news', 'News']) # /news -> get news for specific keyword def send_news(message): """ Get news for keywords of user @@ -326,7 +326,7 @@ 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']) +@bot.message_handler(commands=['addkeyword', 'Addkeyword']) # /addkeyword -> add keyword to user def add_keyword(message): """ Add keyword to user :type message: message object bot @@ -348,10 +348,10 @@ def store_keyword(message): 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.send_message(chat_id=user_id, text=f'Keyword "{keyword}" could not be stored. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info (statuscode {status})') -@bot.message_handler(commands=['removekeyword']) +@bot.message_handler(commands=['removekeyword', 'Removekeyword']) # /removekeyword -> remove keyword from user def remove_keyword(message): """ Remove keyword from user :type message: message object bot @@ -375,7 +375,7 @@ def remove_keyword_step(message): bot.send_message(chat_id=user_id, text=f'Failed deleting keyword "{keyword}". (statuscode {status})') -@bot.message_handler(commands=['keywords']) +@bot.message_handler(commands=['keywords', 'Keywords']) # /keywords -> get keywords of user def send_keywords(message): """ Send keywords of user :type message: message object bot @@ -398,7 +398,7 @@ 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']) #tbd +@bot.message_handler(commands=['portfolio', 'Portfolio']) #tbd def send_portfolio(message): """ Send portfolio of user :type message: message object bot @@ -425,7 +425,7 @@ def send_portfolio(message): bot.send_message(chat_id=user_id, text=f'*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}', parse_mode="MARKDOWN") -@bot.message_handler(commands=['newtransaction']) #tbd not working rn +@bot.message_handler(commands=['newtransaction', 'Newtransaction']) #tbd not working rn def set_new_transaction(message): """ Set new transaction for user :type message: message object bot @@ -436,24 +436,25 @@ def set_new_transaction(message): :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.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 \n(e.g. AAPL,53.2,120.4).\n Try again with /newtransaction.') + 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 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() + desc = str(transaction_data[0]) + isin = str(transaction_data[1]) + amount = float(transaction_data[2]) + price = float(transaction_data[3]) + time = dt.datetime.now().isoformat() #print("\n\n\n\n\n") #print(f"{symbol},{amount},{price},{time}") - status = api_handler.set_transaction(user_id, amount, price, symbol, time) + status = api_handler.set_transaction(user_id, desc, isin, amount, price, time) if status == 200: bot.send_message(chat_id=user_id, text='Transaction succesfully added.') @@ -461,7 +462,7 @@ def set_new_transaction_step(message): bot.send_message(chat_id=user_id, text=f'Failed adding transaction. (statuscode {status})') -@bot.message_handler(commands=['interval']) +@bot.message_handler(commands=['interval', 'Interval']) #tbd def send_interval(message): """ send interval for user :type message: message object bot @@ -485,7 +486,7 @@ def send_interval(message): bot.send_message(chat_id=user_id, text=f'Your update interval: {interval} (https://crontab.guru/#{formatted_interval})') -@bot.message_handler(commands=['setinterval']) +@bot.message_handler(commands=['setinterval', 'Setinterval']) #tbd def set_new_interval(message): """ Set new interval for user :type message: message object bot