diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index c06e6ee..c5bc997 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -12,6 +12,7 @@ __license__ = "None" import sys import os import requests as r +from croniter import croniter @@ -32,7 +33,6 @@ class API_Handler: delete_share(user_id, symbol): deletes the share of the user 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 @@ -322,6 +322,10 @@ class API_Handler: Returns: int: status code """ + if not croniter.is_valid(cron_interval): + print("Error: Invalid cron format") + return -1 + with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.put(self.db_adress + "/user/setCron", json={"cron": cron_interval}, headers=headers) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index b1d985b..e2cb748 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -437,7 +437,56 @@ 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']) +def send_interval(message): + """ send interval for user + :type message: message object bot + :param message: message that was reacted to, in this case always '/interval' + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + interval = api_handler.get_user(user_id)['cron'] + if interval == 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 and set an interval with /setinterval') + return + else: + interval = str(interval) + formatted_interval = str(interval).replace(' ', '_') + bot.send_message(chat_id=user_id, text=f'Your update interval: {interval} (https://crontab.guru/#{formatted_interval})', parse_mode="MARKDOWN") + + +@bot.message_handler(commands=['setinterval']) +def set_new_interval(message): + """ Set new interval for user + :type message: message object bot + :param message: message that was reacted to, in this case always '/setinterval' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + bot.send_message(chat_id=user_id, text='Type interval in cron format (https://crontab.guru/):') + bot.register_next_step_handler(message, set_new_interval_step) + +def set_new_interval_step(message): + + user_id = int(message.from_user.id) + interval = str(message.text) + status = api_handler.set_cron_interval(user_id, interval) + + if status == 200: + bot.send_message(chat_id=user_id, text='Interval succesfully set.') + return + + if status == -1: + bot.send_message(chat_id=user_id, text='Invalid interval. Try again with /setinterval.') + return + else: + bot.send_message(chat_id=user_id, text=f'Failed setting interval. (statuscode {status})') @bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement diff --git a/telegram_bot/requirements.txt b/telegram_bot/requirements.txt index 2e8462d..146877f 100644 --- a/telegram_bot/requirements.txt +++ b/telegram_bot/requirements.txt @@ -5,3 +5,4 @@ newsapi-python~=0.2.6 python-dotenv~=0.20.0 requests~=2.27.1 APScheduler~=3.9.1 +cronitor~=1.3.4