From 8d5d777de60031efe5ed2cfd21074d5c9799ae0a Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 28 Mar 2022 18:07:39 +0200 Subject: [PATCH 01/52] implemented apihandler class and functions --- telegram_bot/api_handler.py | 49 ++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py index 69c4974..aa9b4da 100644 --- a/telegram_bot/api_handler.py +++ b/telegram_bot/api_handler.py @@ -4,4 +4,51 @@ script for communicating with webservice to get data from database __author__ = "Florian Kellermann, Linus Eickhoff" __date__ = "16.03.2022" __version__ = "0.0.1" -__license__ = "None" \ No newline at end of file +__license__ = "None" + +#side-dependencies: none +#Work in Progress + +import sys +import os +import requests as r + +class API_Handler: + #class for communicating with webservice to get data from database + def __init__(self, db_adress): + self.db_adress = db_adress + return + + def get_auth_token(self, email, password): + payload = {'email': email, 'password': password} + with r.Session() as s: + p = s.post(self.db_adress + "/user/login", json=payload) + if p.status_code == 200: + self.token = p.json()["data"]['token'] + return p.json()["data"]['token'] + else: + #print server response + print(p.text) + return None + + def get_user_keywords(self, token, user_id): + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + token + ":" + user_id} + p = s.get(self.db_adress + "/api/keywords", headers=headers) + keywords_json = p.json()["data"] + keywords = [] + for keyword in keywords_json: + keywords.append(keyword["keyword"]) + return keywords + + +if __name__ == "__main__": + + print("This is a module for the telegram bot. It is not intended to be run directly.") + handler = API_Handler("https://aktienbot.flokaiser.com/api") + try: + handler.get_auth_token("bot@example.com", "bot") + except None: + print("Could not connect to server.") + + sys.exit(1) \ No newline at end of file From 4b23261bd1f7fd2daa6fcb8cd1829aa625804243 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 28 Mar 2022 18:55:52 +0200 Subject: [PATCH 02/52] api_handler functions completed (tbd: testing) --- telegram_bot/api_handler.py | 189 +++++++++++++++++++++++++++++++++--- 1 file changed, 174 insertions(+), 15 deletions(-) diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py index aa9b4da..45c5de4 100644 --- a/telegram_bot/api_handler.py +++ b/telegram_bot/api_handler.py @@ -15,11 +15,31 @@ import requests as r class API_Handler: #class for communicating with webservice to get data from database - def __init__(self, db_adress): - self.db_adress = db_adress - return + def __init__(self, db_adress, email, password): + """initializes the API_Handler class - def get_auth_token(self, email, password): + Args: + db_adress (string): adress of the database + email (string): email of the user + password (string): password of the user + """ + self.db_adress = db_adress + + payload = {'email': email, 'password': password} + with r.Session() as s: + p = s.post(self.db_adress + "/user/login", json=payload) + if p.status_code == 200: + self.token = p.json()["data"]['token'] + else: + self.token = None + + def reauthorize(self, email, password): + """reauthorizes the user + + Args: + email (string): email of the user + password (string): password of the user + """ payload = {'email': email, 'password': password} with r.Session() as s: p = s.post(self.db_adress + "/user/login", json=payload) @@ -27,28 +47,167 @@ class API_Handler: self.token = p.json()["data"]['token'] return p.json()["data"]['token'] else: - #print server response - print(p.text) + self.token = None return None - def get_user_keywords(self, token, user_id): + def get_user_keywords(self, user_id): + """gets the keywords of the user + + Args: + user_id (int): id of the user + + Returns: + list: list of keywords + """ with r.Session() as s: - headers = {'Authorization': 'Bearer ' + token + ":" + user_id} - p = s.get(self.db_adress + "/api/keywords", headers=headers) - keywords_json = p.json()["data"] + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/keywords", headers=headers) + print(req.status_code) + keywords_json = req.json()["data"] keywords = [] for keyword in keywords_json: keywords.append(keyword["keyword"]) + return keywords + def set_keyword(self, user_id, keyword): + """sets the keyword of the user + + Args: + user_id (int): id of the user + keyword (int): keyword of the user + + Returns: + int: status code + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.post(self.db_adress + "/keyword", json={"keyword": keyword}, headers=headers) + + return req.status_code + + def delete_keyword(self, user_id, keyword): + """deletes the keyword of the user + + Args: + user_id (int): id of the user + keyword (string): keyword of the user + + Returns: + int: status code + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.delete(self.db_adress + "/keyword", json={"keyword": keyword}, headers=headers) + + return req.status_code + + def get_user_shares(self, user_id): + """gets the shares of the user + + Args: + user_id (int): id of the user + + Returns: + list: list of shares + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/shares", headers=headers) + shares_json = req.json()["data"] + shares = [] + for share in shares_json: + shares.append(share["symbol"]) + + return shares + + def set_share(self, user_id, symbol): + """sets the share of the user + + Args: + user_id (int): id of the user + symbol (string): symbol of the share + + Returns: + int: status code + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.post(self.db_adress + "/share", json={"symbol": symbol}, headers=headers) + return req.status_code + + def delete_share(self, user_id, symbol): + """deletes the share of the user + + Args: + user_id (int): id of the user + symbol (string): symbol of the share + + 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={"symbol": symbol}, headers=headers) + return req.status_code + + def get_user_transactions(self, user_id): + """gets the transactions of the user + + Args: + user_id (int): id of the user + + Returns: + dict: dictionary of transactions + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/transactions", headers=headers) + transactions_dict = dict(req.json()["data"]) + return transactions_dict + + def set_transaction(self, user_id, count, price, symbol, timestamp): + """sets the transaction of the user + + Args: + user_id (int): id of the user + count (int): count of the transaction + price (float): price of the transaction + symbol (string): symbol of the transaction + timestamp (string): timestamp 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} + req = s.post(self.db_adress + "/transaction", json=transaction, headers=headers) + return req.status_code + + def get_user_portfolio(self, user_id): + """gets the portfolio of the user + + Args: + user_id (int): id of the user + + Returns: + dict: dictionary of portfolio + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/portfolio", headers=headers) + portfolio_dict = dict(req.json()["data"]) + return portfolio_dict + + + if __name__ == "__main__": print("This is a module for the telegram bot. It is not intended to be run directly.") - handler = API_Handler("https://aktienbot.flokaiser.com/api") - try: - handler.get_auth_token("bot@example.com", "bot") - except None: - print("Could not connect to server.") + handler = API_Handler("https://aktienbot.flokaiser.com/api", "bot@example.com", "bot") + keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is mine (Linus) + print(keywords) sys.exit(1) \ No newline at end of file From c489e9ab46a3a6723669bd483ce59495b3dcc58b Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 28 Mar 2022 19:39:37 +0200 Subject: [PATCH 03/52] added keywords from db (not working currently) --- telegram_bot/api_handler.py | 6 +++--- telegram_bot/bot.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py index 45c5de4..93d5c2f 100644 --- a/telegram_bot/api_handler.py +++ b/telegram_bot/api_handler.py @@ -34,7 +34,7 @@ class API_Handler: self.token = None def reauthorize(self, email, password): - """reauthorizes the user + """set new credentials Args: email (string): email of the user @@ -207,7 +207,7 @@ if __name__ == "__main__": print("This is a module for the telegram bot. It is not intended to be run directly.") handler = API_Handler("https://aktienbot.flokaiser.com/api", "bot@example.com", "bot") - - keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is mine (Linus) + print(handler.token) + keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) print(keywords) sys.exit(1) \ No newline at end of file diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index e3e4f28..c62083f 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -28,12 +28,17 @@ import shares.share_fetcher as share_fetcher from telebot import types from dotenv import load_dotenv +from api_handler import API_Handler + load_dotenv() bot_version = "0.2.1" user_list = [] +#create api handler +api_handler = API_Handler("https://aktienbot.flokaiser.com", os.getenv("BOT_EMAIL"), os.getenv("BOT_PASSWORD")) + class User: # Currently saving users in this class to test functionality -> later database def __init__(self, p_user_id, p_user_name, p_chat_id): @@ -222,11 +227,11 @@ def send_news(message): :rtype: none """ - keyword = "bitcoin" user_id = int(message.from_user.id) - #Get Information for user with this id + keywords = api_handler.get_keywords(user_id) + keyword = keywords[0] - articles = news.get_top_news_by_keyword(keyword) #tbd: get keyword from db + articles = news.get_top_news_by_keyword(keywords[0]) try: formatted_article = news.format_article(articles["articles"][0]) except IndexError: @@ -235,6 +240,27 @@ 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']) +def add_keyword(message): + """ Add keyword to user + :type message: message object bot + :param message: message that was reacted to, in this case always '/addkeyword' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + bot.send_message(chat_id=user_id, text='Type keyword to add:') + bot.register_next_step_handler(message, store_keyword) + +def store_keyword(message): + user_id = int(message.from_user.id) + keyword = str(message.text) + api_handler.add_keyword(user_id, keyword) + bot.send_message(chat_id=user_id, text=f'Keyword {keyword} added.') + + @bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement def echo_all(message): From 218f70f541ecf74e00ba09739ed73f674a9bca2b Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 08:31:58 +0200 Subject: [PATCH 04/52] added status function --- telegram_bot/bot.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c62083f..5fc363f 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -154,6 +154,21 @@ def send_id(message): bot.reply_to(message, answer) +#function that sends telegram status(running or offline) as message from telegram bot to user +@bot.message_handler(commands=['status']) +def send_status(message): + + """ Sends status to user + :type message: message object bot + :param message: message that was reacted to, if no other command handler gets called + + :raises: none + + :rtype: none + """ + bot.reply_to(message, "bot is running") + + @bot.message_handler(commands=['update']) def send_update(message): @@ -306,9 +321,6 @@ def main_loop(): :rtype: none """ bot.infinity_polling() - while 1: - time.sleep(3) - if __name__ == '__main__': try: From bf8fdbf848a0bf17b1ff40d199fde80aefe71e62 Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 08:41:09 +0200 Subject: [PATCH 05/52] formatting and docu additions --- telegram_bot/api_handler.py | 33 ++++++++++++++++++++++++++++++++- telegram_bot/db_handler.py | 15 --------------- 2 files changed, 32 insertions(+), 16 deletions(-) delete mode 100644 telegram_bot/db_handler.py diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py index 93d5c2f..049ae93 100644 --- a/telegram_bot/api_handler.py +++ b/telegram_bot/api_handler.py @@ -13,8 +13,29 @@ import sys import os import requests as r + + class API_Handler: - #class for communicating with webservice to get data from database + """class for interacting with the api webservice + + Attributes: + db_adress (string): adress of the database + token (string): auth token for api + + Methods: + reauthorize(email, password): set new credentials + get_user_keywords(user_id): gets the keywords of the user + set_keyword(user_id, keyword): sets the keyword of the user + delete_keyword(user_id, keyword): deletes the keyword of the user + get_user_shares(user_id): gets the shares of the user + set_share(user_id, symbol): sets the share of the user + 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 + """ + + def __init__(self, db_adress, email, password): """initializes the API_Handler class @@ -33,6 +54,7 @@ class API_Handler: else: self.token = None + def reauthorize(self, email, password): """set new credentials @@ -50,6 +72,7 @@ class API_Handler: self.token = None return None + def get_user_keywords(self, user_id): """gets the keywords of the user @@ -70,6 +93,7 @@ class API_Handler: return keywords + def set_keyword(self, user_id, keyword): """sets the keyword of the user @@ -86,6 +110,7 @@ class API_Handler: return req.status_code + def delete_keyword(self, user_id, keyword): """deletes the keyword of the user @@ -102,6 +127,7 @@ class API_Handler: return req.status_code + def get_user_shares(self, user_id): """gets the shares of the user @@ -121,6 +147,7 @@ class API_Handler: return shares + def set_share(self, user_id, symbol): """sets the share of the user @@ -136,6 +163,7 @@ class API_Handler: req = s.post(self.db_adress + "/share", json={"symbol": symbol}, headers=headers) return req.status_code + def delete_share(self, user_id, symbol): """deletes the share of the user @@ -151,6 +179,7 @@ class API_Handler: req = s.delete(self.db_adress + "/share", json={"symbol": symbol}, headers=headers) return req.status_code + def get_user_transactions(self, user_id): """gets the transactions of the user @@ -166,6 +195,7 @@ class API_Handler: transactions_dict = dict(req.json()["data"]) return transactions_dict + def set_transaction(self, user_id, count, price, symbol, timestamp): """sets the transaction of the user @@ -185,6 +215,7 @@ class API_Handler: req = s.post(self.db_adress + "/transaction", json=transaction, headers=headers) return req.status_code + def get_user_portfolio(self, user_id): """gets the portfolio of the user diff --git a/telegram_bot/db_handler.py b/telegram_bot/db_handler.py deleted file mode 100644 index 816af71..0000000 --- a/telegram_bot/db_handler.py +++ /dev/null @@ -1,15 +0,0 @@ -""" -script for database interaction -""" -__author__ = "Florian Kellermann, Linus Eickhoff" -__date__ = "15.03.2022" -__version__ = "0.0.1" -__license__ = "None" - -# get db_key from env - -class DB_Handler: - - def __init__(self, db_adress): - # tbd - return \ No newline at end of file From 456bba027b70427bac0b062fe066206eb3fa8026 Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 08:44:40 +0200 Subject: [PATCH 06/52] updated env example with bot creds --- telegram_bot/.env.example | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/telegram_bot/.env.example b/telegram_bot/.env.example index bcd6a02..a36e6ec 100644 --- a/telegram_bot/.env.example +++ b/telegram_bot/.env.example @@ -6,3 +6,7 @@ NEWS_API_KEY= # Flask secret key SECRET_KEY= + +# bot credentials +BOT_EMAIL= +BOT_PASSWORD= \ No newline at end of file From 1ba19a219fe7dac9640cfdfb0490ef23bae5d8f0 Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 08:49:32 +0200 Subject: [PATCH 07/52] small change --- telegram_bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 5fc363f..617b23e 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -31,7 +31,7 @@ from dotenv import load_dotenv from api_handler import API_Handler -load_dotenv() +load_dotenv(dotenv_path='.env') bot_version = "0.2.1" user_list = [] From 21d5d357b861f5cc2c3d4efb7ec97166d7683276 Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 09:26:50 +0200 Subject: [PATCH 08/52] moved api handler in subfolder --- telegram_bot/api_handling/__init__.py | 0 telegram_bot/api_handling/api_handler.py | 245 +++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 telegram_bot/api_handling/__init__.py create mode 100644 telegram_bot/api_handling/api_handler.py diff --git a/telegram_bot/api_handling/__init__.py b/telegram_bot/api_handling/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py new file mode 100644 index 0000000..97eb937 --- /dev/null +++ b/telegram_bot/api_handling/api_handler.py @@ -0,0 +1,245 @@ +""" +script for communicating with webservice to get data from database +""" +__author__ = "Florian Kellermann, Linus Eickhoff" +__date__ = "16.03.2022" +__version__ = "0.0.1" +__license__ = "None" + +#side-dependencies: none +#Work in Progress + +import sys +import os +import requests as r + + + +class API_Handler: + """class for interacting with the api webservice + + Attributes: + db_adress (string): adress of the database + token (string): auth token for api + + Methods: + reauthorize(email, password): set new credentials + get_user_keywords(user_id): gets the keywords of the user + set_keyword(user_id, keyword): sets the keyword of the user + delete_keyword(user_id, keyword): deletes the keyword of the user + get_user_shares(user_id): gets the shares of the user + set_share(user_id, symbol): sets the share of the user + 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 + """ + + + def __init__(self, db_adress, email, password): + """initializes the API_Handler class + + Args: + db_adress (string): adress of the database + email (string): email of the user + password (string): password of the user + """ + self.db_adress = db_adress + + payload = {'email': email, 'password': password} + with r.Session() as s: + p = s.post(self.db_adress + "/user/login", json=payload) + if p.status_code == 200: + self.token = p.json()["data"]['token'] + else: + print("Error: " + str(p.status_code) + " invalid credentials") + self.token = None + + + def reauthorize(self, email, password): + """set new credentials + + Args: + email (string): email of the user + password (string): password of the user + """ + payload = {'email': email, 'password': password} + with r.Session() as s: + p = s.post(self.db_adress + "/user/login", json=payload) + if p.status_code == 200: + self.token = p.json()["data"]['token'] + return p.json()["data"]['token'] + else: + self.token = None + return None + + + def get_user_keywords(self, user_id): + """gets the keywords of the user + + Args: + user_id (int): id of the user + + Returns: + list: list of keywords + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/keywords", headers=headers) + print(req.status_code) + keywords_json = req.json()["data"] + keywords = [] + for keyword in keywords_json: + keywords.append(keyword["keyword"]) + + return keywords + + + def set_keyword(self, user_id, keyword): + """sets the keyword of the user + + Args: + user_id (int): id of the user + keyword (int): keyword of the user + + Returns: + int: status code + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.post(self.db_adress + "/keyword", json={"keyword": keyword}, headers=headers) + + return req.status_code + + + def delete_keyword(self, user_id, keyword): + """deletes the keyword of the user + + Args: + user_id (int): id of the user + keyword (string): keyword of the user + + Returns: + int: status code + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.delete(self.db_adress + "/keyword", json={"keyword": keyword}, headers=headers) + + return req.status_code + + + def get_user_shares(self, user_id): + """gets the shares of the user + + Args: + user_id (int): id of the user + + Returns: + list: list of shares + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/shares", headers=headers) + shares_json = req.json()["data"] + shares = [] + for share in shares_json: + shares.append(share["symbol"]) + + return shares + + + def set_share(self, user_id, symbol): + """sets the share of the user + + Args: + user_id (int): id of the user + symbol (string): symbol of the share + + Returns: + int: status code + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.post(self.db_adress + "/share", json={"symbol": symbol}, headers=headers) + return req.status_code + + + def delete_share(self, user_id, symbol): + """deletes the share of the user + + Args: + user_id (int): id of the user + symbol (string): symbol of the share + + 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={"symbol": symbol}, headers=headers) + return req.status_code + + + def get_user_transactions(self, user_id): + """gets the transactions of the user + + Args: + user_id (int): id of the user + + Returns: + dict: dictionary of transactions + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/transactions", headers=headers) + transactions_dict = dict(req.json()["data"]) + return transactions_dict + + + def set_transaction(self, user_id, count, price, symbol, timestamp): + """sets the transaction of the user + + Args: + user_id (int): id of the user + count (int): count of the transaction + price (float): price of the transaction + symbol (string): symbol of the transaction + timestamp (string): timestamp 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} + req = s.post(self.db_adress + "/transaction", json=transaction, headers=headers) + return req.status_code + + + def get_user_portfolio(self, user_id): + """gets the portfolio of the user + + Args: + user_id (int): id of the user + + Returns: + dict: dictionary of portfolio + """ + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/portfolio", headers=headers) + portfolio_dict = dict(req.json()["data"]) + return portfolio_dict + + + + +if __name__ == "__main__": + + print("This is a module for the telegram bot. It is not intended to be run directly.") + handler = API_Handler("https://aktienbot.flokaiser.com/api", "bot@example.com", "bot") + print(handler.token) + keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) + print(keywords) + sys.exit(1) \ No newline at end of file From 4c517998ba0150bb0c877aa5400a7343e0cca8f0 Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 10:49:02 +0200 Subject: [PATCH 09/52] added funcs --- telegram_bot/api_handler.py | 244 ------------------------------------ telegram_bot/bot.py | 48 ++++--- 2 files changed, 34 insertions(+), 258 deletions(-) delete mode 100644 telegram_bot/api_handler.py diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py deleted file mode 100644 index 049ae93..0000000 --- a/telegram_bot/api_handler.py +++ /dev/null @@ -1,244 +0,0 @@ -""" -script for communicating with webservice to get data from database -""" -__author__ = "Florian Kellermann, Linus Eickhoff" -__date__ = "16.03.2022" -__version__ = "0.0.1" -__license__ = "None" - -#side-dependencies: none -#Work in Progress - -import sys -import os -import requests as r - - - -class API_Handler: - """class for interacting with the api webservice - - Attributes: - db_adress (string): adress of the database - token (string): auth token for api - - Methods: - reauthorize(email, password): set new credentials - get_user_keywords(user_id): gets the keywords of the user - set_keyword(user_id, keyword): sets the keyword of the user - delete_keyword(user_id, keyword): deletes the keyword of the user - get_user_shares(user_id): gets the shares of the user - set_share(user_id, symbol): sets the share of the user - 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 - """ - - - def __init__(self, db_adress, email, password): - """initializes the API_Handler class - - Args: - db_adress (string): adress of the database - email (string): email of the user - password (string): password of the user - """ - self.db_adress = db_adress - - payload = {'email': email, 'password': password} - with r.Session() as s: - p = s.post(self.db_adress + "/user/login", json=payload) - if p.status_code == 200: - self.token = p.json()["data"]['token'] - else: - self.token = None - - - def reauthorize(self, email, password): - """set new credentials - - Args: - email (string): email of the user - password (string): password of the user - """ - payload = {'email': email, 'password': password} - with r.Session() as s: - p = s.post(self.db_adress + "/user/login", json=payload) - if p.status_code == 200: - self.token = p.json()["data"]['token'] - return p.json()["data"]['token'] - else: - self.token = None - return None - - - def get_user_keywords(self, user_id): - """gets the keywords of the user - - Args: - user_id (int): id of the user - - Returns: - list: list of keywords - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.get(self.db_adress + "/keywords", headers=headers) - print(req.status_code) - keywords_json = req.json()["data"] - keywords = [] - for keyword in keywords_json: - keywords.append(keyword["keyword"]) - - return keywords - - - def set_keyword(self, user_id, keyword): - """sets the keyword of the user - - Args: - user_id (int): id of the user - keyword (int): keyword of the user - - Returns: - int: status code - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.post(self.db_adress + "/keyword", json={"keyword": keyword}, headers=headers) - - return req.status_code - - - def delete_keyword(self, user_id, keyword): - """deletes the keyword of the user - - Args: - user_id (int): id of the user - keyword (string): keyword of the user - - Returns: - int: status code - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.delete(self.db_adress + "/keyword", json={"keyword": keyword}, headers=headers) - - return req.status_code - - - def get_user_shares(self, user_id): - """gets the shares of the user - - Args: - user_id (int): id of the user - - Returns: - list: list of shares - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.get(self.db_adress + "/shares", headers=headers) - shares_json = req.json()["data"] - shares = [] - for share in shares_json: - shares.append(share["symbol"]) - - return shares - - - def set_share(self, user_id, symbol): - """sets the share of the user - - Args: - user_id (int): id of the user - symbol (string): symbol of the share - - Returns: - int: status code - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.post(self.db_adress + "/share", json={"symbol": symbol}, headers=headers) - return req.status_code - - - def delete_share(self, user_id, symbol): - """deletes the share of the user - - Args: - user_id (int): id of the user - symbol (string): symbol of the share - - 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={"symbol": symbol}, headers=headers) - return req.status_code - - - def get_user_transactions(self, user_id): - """gets the transactions of the user - - Args: - user_id (int): id of the user - - Returns: - dict: dictionary of transactions - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.get(self.db_adress + "/transactions", headers=headers) - transactions_dict = dict(req.json()["data"]) - return transactions_dict - - - def set_transaction(self, user_id, count, price, symbol, timestamp): - """sets the transaction of the user - - Args: - user_id (int): id of the user - count (int): count of the transaction - price (float): price of the transaction - symbol (string): symbol of the transaction - timestamp (string): timestamp 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} - req = s.post(self.db_adress + "/transaction", json=transaction, headers=headers) - return req.status_code - - - def get_user_portfolio(self, user_id): - """gets the portfolio of the user - - Args: - user_id (int): id of the user - - Returns: - dict: dictionary of portfolio - """ - with r.Session() as s: - headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} - req = s.get(self.db_adress + "/portfolio", headers=headers) - portfolio_dict = dict(req.json()["data"]) - return portfolio_dict - - - - -if __name__ == "__main__": - - print("This is a module for the telegram bot. It is not intended to be run directly.") - handler = API_Handler("https://aktienbot.flokaiser.com/api", "bot@example.com", "bot") - print(handler.token) - keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) - print(keywords) - sys.exit(1) \ No newline at end of file diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 617b23e..9f75ecf 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -28,7 +28,7 @@ import shares.share_fetcher as share_fetcher from telebot import types from dotenv import load_dotenv -from api_handler import API_Handler +from api_handling.api_handler import API_Handler load_dotenv(dotenv_path='.env') @@ -37,7 +37,8 @@ bot_version = "0.2.1" user_list = [] #create api handler -api_handler = API_Handler("https://aktienbot.flokaiser.com", os.getenv("BOT_EMAIL"), os.getenv("BOT_PASSWORD")) +api_handler = API_Handler("https://aktienbot.flokaiser.com/api", str(os.getenv("BOT_EMAIL")), str(os.getenv("BOT_PASSWORD"))) +print(api_handler.token) class User: # Currently saving users in this class to test functionality -> later database def __init__(self, p_user_id, p_user_name, p_chat_id): @@ -243,16 +244,14 @@ def send_news(message): """ user_id = int(message.from_user.id) - keywords = api_handler.get_keywords(user_id) - keyword = keywords[0] + keywords = api_handler.get_user_keywords(user_id) + keyword_search = 'OR'.join(keywords) - articles = news.get_top_news_by_keyword(keywords[0]) - try: - formatted_article = news.format_article(articles["articles"][0]) - except IndexError: - bot.send_message(chat_id=user_id, text=f"no news currently available for keyword: {keyword}") - return - bot.send_message(chat_id=user_id, text=f"_keyword: {keyword}_\n\n" + formatted_article, parse_mode="MARKDOWN") + news_list = api_handler.get_news_for_keyword(keyword_search)['articles'] + + for news in news_list: + formatted_article = news.format_article(news) + bot.send_message(chat_id=user_id, text=formatted_article, parse_mode="MARKDOWN") @bot.message_handler(commands=['addkeyword']) @@ -271,9 +270,30 @@ def add_keyword(message): def store_keyword(message): user_id = int(message.from_user.id) - keyword = str(message.text) - api_handler.add_keyword(user_id, keyword) - bot.send_message(chat_id=user_id, text=f'Keyword {keyword} added.') + print(str(user_id)) + keyword = str(message.text).lower() + api_handler.set_keyword(user_id, keyword) + bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" added.') + +@bot.message_handler(commands=['removekeyword']) +def remove_keyword(message): + """ Remove keyword from user + :type message: message object bot + :param message: message that was reacted to, in this case always '/removekeyword' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + bot.send_message(chat_id=user_id, text='Type keyword to remove:') + bot.register_next_step_handler(message, remove_keyword_step) + +def remove_keyword_step(message): + user_id = int(message.from_user.id) + keyword = str(message.text).lower() + api_handler.delete_keyword(user_id, keyword) + bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" removed.') @bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement From 4afd1ab87be8adcff99b2263529eb1b058fe0558 Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 12:04:49 +0200 Subject: [PATCH 10/52] workaround for connection problems --- telegram_bot/api_handling/api_handler.py | 22 ++++++++++++++-------- telegram_bot/bot.py | 24 ++++++++++++++++++++---- telegram_bot/news/news_fetcher.py | 7 +++++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 97eb937..8c14833 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -83,16 +83,20 @@ class API_Handler: Returns: list: list of keywords """ + keywords = [] with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.get(self.db_adress + "/keywords", headers=headers) - print(req.status_code) - keywords_json = req.json()["data"] - keywords = [] - for keyword in keywords_json: - keywords.append(keyword["keyword"]) + if(req.status_code == 200): + keywords_json = req.json()["data"] + for keyword in keywords_json: + keywords.append(keyword["keyword"]) + + return keywords + + else: + return self.get_user_keywords(user_id) # might end in infinite loop!! - return keywords def set_keyword(self, user_id, keyword): @@ -193,8 +197,10 @@ class API_Handler: with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.get(self.db_adress + "/transactions", headers=headers) - transactions_dict = dict(req.json()["data"]) - return transactions_dict + + if req.status_code == 200: + transactions_dict = dict(req.json()["data"]) + return transactions_dict def set_transaction(self, user_id, count, price, symbol, timestamp): diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 9f75ecf..43d8845 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -245,12 +245,12 @@ def send_news(message): user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) - keyword_search = 'OR'.join(keywords) + keywords_search = ','.join(keywords) - news_list = api_handler.get_news_for_keyword(keyword_search)['articles'] + news_list = news.get_top_news_by_keyword(keywords_search)["articles"] - for news in news_list: - formatted_article = news.format_article(news) + for article in news_list: + formatted_article = news.format_article(article) bot.send_message(chat_id=user_id, text=formatted_article, parse_mode="MARKDOWN") @@ -275,6 +275,7 @@ def store_keyword(message): api_handler.set_keyword(user_id, keyword) bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" added.') + @bot.message_handler(commands=['removekeyword']) def remove_keyword(message): """ Remove keyword from user @@ -296,6 +297,21 @@ def remove_keyword_step(message): bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" removed.') +@bot.message_handler(commands=['keywords']) +def send_keywords(message): + """ Send keywords of user + :type message: message object bot + :param message: message that was reacted to, in this case always '/keywords' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + keywords = api_handler.get_user_keywords(user_id) + bot.send_message(chat_id=user_id, text=f'Your keywords are: {keywords}') + + @bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement def echo_all(message): diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index 69c0807..e320936 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -6,6 +6,7 @@ __date__ = "15.03.2022" __version__ = "0.0.1" __license__ = "None" +from ast import parse import sys import os import json @@ -13,6 +14,7 @@ import requests from newsapi import NewsApiClient from dotenv import load_dotenv +import urllib.parse as urlparse load_dotenv() @@ -31,8 +33,9 @@ def get_top_news_by_keyword(keyword): Returns: JSON/dict: dict containing articles - """ - top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en') + """ + keyword_url = urlparse.quote(keyword) + top_headlines = newsapi.get_top_headlines(q=keyword_url, sources=str_sources, language='en') return top_headlines def format_article(article): From 4bd53c01169fea417372efa1d4c586641cb1f41d Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 12:20:58 +0200 Subject: [PATCH 11/52] news working --- telegram_bot/bot.py | 15 ++++++++++----- telegram_bot/news/news_fetcher.py | 4 +--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 43d8845..c1f21f2 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -245,13 +245,18 @@ def send_news(message): user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) - keywords_search = ','.join(keywords) - + keywords_search = ' OR '.join(keywords) + print(keywords_search) news_list = news.get_top_news_by_keyword(keywords_search)["articles"] - for article in news_list: - formatted_article = news.format_article(article) - bot.send_message(chat_id=user_id, text=formatted_article, parse_mode="MARKDOWN") + if news_list: + for article in news_list: + formatted_article = news.format_article(article) + bot.send_message(chat_id=user_id, text=formatted_article, parse_mode="MARKDOWN") + else: + bot.send_message(chat_id=user_id, text='No news found for your keywords.') + + @bot.message_handler(commands=['addkeyword']) diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index e320936..bf2aeb9 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -14,7 +14,6 @@ import requests from newsapi import NewsApiClient from dotenv import load_dotenv -import urllib.parse as urlparse load_dotenv() @@ -34,8 +33,7 @@ def get_top_news_by_keyword(keyword): Returns: JSON/dict: dict containing articles """ - keyword_url = urlparse.quote(keyword) - top_headlines = newsapi.get_top_headlines(q=keyword_url, sources=str_sources, language='en') + top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en') return top_headlines def format_article(article): From 4005e8dccf696cd81c9581825841dcb3ecf6c17e Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 12:28:28 +0200 Subject: [PATCH 12/52] fetching news by keywords --- telegram_bot/bot.py | 2 +- telegram_bot/news/news_fetcher.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c1f21f2..2fcc146 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -247,7 +247,7 @@ def send_news(message): keywords = api_handler.get_user_keywords(user_id) keywords_search = ' OR '.join(keywords) print(keywords_search) - news_list = news.get_top_news_by_keyword(keywords_search)["articles"] + news_list = news.get_all_news_by_keyword(keywords_search)["articles"][:5] if news_list: for article in news_list: diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index bf2aeb9..b10d390 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -25,8 +25,8 @@ sources = source_json["sources"] str_sources = ",".join([source["id"] for source in sources]) -def get_top_news_by_keyword(keyword): - """get top news to keyword +def get_all_news_by_keyword(keyword, from_date="2022-01-01", to_date="2022-03-29"): # hard coded will change soon + """get all news to keyword Args: keyword (String): keyword for search From 0aee85d718fe4f7dd2b806c43e1b6410bd0afc4f Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 12:29:13 +0200 Subject: [PATCH 13/52] bug fix --- telegram_bot/news/news_fetcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index b10d390..f8764ba 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -56,6 +56,6 @@ if __name__ == '__main__': print("fetching top news by keyword business...") - articles = get_top_news_by_keyword("bitcoin") + articles = get_all_news_by_keyword("bitcoin") formatted_article = format_article(articles["articles"][0]) print(formatted_article) \ No newline at end of file From 6289e5dc71d37d4b5ef91a71959bcd9c9b42a72d Mon Sep 17 00:00:00 2001 From: Rripped Date: Tue, 29 Mar 2022 13:37:44 +0200 Subject: [PATCH 14/52] added date parameter --- telegram_bot/bot.py | 7 ++++++- telegram_bot/news/news_fetcher.py | 9 ++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 2fcc146..b24599a 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -24,6 +24,7 @@ import json import news.news_fetcher as news import shares.share_fetcher as share_fetcher +import datetime as dt from telebot import types from dotenv import load_dotenv @@ -247,7 +248,11 @@ def send_news(message): keywords = api_handler.get_user_keywords(user_id) keywords_search = ' OR '.join(keywords) print(keywords_search) - news_list = news.get_all_news_by_keyword(keywords_search)["articles"][:5] + now = dt.datetime.now().date() + from_date = now - dt.timedelta(days=7) + from_date_formatted = dt.datetime.strftime(from_date, '%Y-%m-%d') + print(from_date_formatted) + news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)["articles"][:5] if news_list: for article in news_list: diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index f8764ba..f913257 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -11,6 +11,7 @@ import sys import os import json import requests +import datetime as dt from newsapi import NewsApiClient from dotenv import load_dotenv @@ -25,15 +26,16 @@ sources = source_json["sources"] str_sources = ",".join([source["id"] for source in sources]) -def get_all_news_by_keyword(keyword, from_date="2022-01-01", to_date="2022-03-29"): # hard coded will change soon +def get_all_news_by_keyword(keyword, from_date="2000-01-01"): # hard coded will change soon """get all news to keyword Args: keyword (String): keyword for search + from_date (String): min date for search Returns: JSON/dict: dict containing articles """ - top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en') + top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date) return top_headlines def format_article(article): @@ -54,7 +56,8 @@ def format_article(article): if __name__ == '__main__': - print("fetching top news by keyword business...") + print("this is a module and should not be run directly") + print("fetching top news by keyword bitcoin...") articles = get_all_news_by_keyword("bitcoin") formatted_article = format_article(articles["articles"][0]) From cc70f8c47850fd6756983508c4ebf8e405b0fdda Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Mon, 4 Apr 2022 16:38:15 +0200 Subject: [PATCH 15/52] share handler in api_handler --- telegram_bot/api_handling/api_handler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 8c14833..bc5ef79 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -248,4 +248,6 @@ if __name__ == "__main__": print(handler.token) keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) print(keywords) + shares = handler.get_user_portfolio(user_id = 1709356058) + print(shares) sys.exit(1) \ No newline at end of file From 0b8ae845ede4b1cb7f20b7fb6ad5c099409f8e07 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 4 Apr 2022 16:54:25 +0200 Subject: [PATCH 16/52] added max req params --- telegram_bot/api_handling/api_handler.py | 49 ++++++++++++++++++------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index bc5ef79..88810ce 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -74,15 +74,19 @@ class API_Handler: return None - def get_user_keywords(self, user_id): + def get_user_keywords(self, user_id, max_retries=10): """gets the keywords of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: list: list of keywords """ + if max_retries <= 0: + return None + keywords = [] with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} @@ -95,7 +99,7 @@ class API_Handler: return keywords else: - return self.get_user_keywords(user_id) # might end in infinite loop!! + return self.get_user_keywords(user_id, max_retries-1) @@ -133,24 +137,32 @@ class API_Handler: return req.status_code - def get_user_shares(self, user_id): + def get_user_shares(self, user_id, max_retries=10): """gets the shares of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: list: list of shares """ + if max_retries <= 0: + return None + with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.get(self.db_adress + "/shares", headers=headers) - shares_json = req.json()["data"] - shares = [] - for share in shares_json: - shares.append(share["symbol"]) + if(req.status_code == 200): + shares_json = req.json()["data"] + shares = [] + for share in shares_json: + shares.append(share["symbol"]) - return shares + return shares + + else: + return self.get_user_shares(user_id, max_retries-1) def set_share(self, user_id, symbol): @@ -185,15 +197,19 @@ class API_Handler: return req.status_code - def get_user_transactions(self, user_id): + def get_user_transactions(self, user_id, max_retries=10): """gets the transactions of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: dict: dictionary of transactions """ + if max_retries <= 0: + return None + with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.get(self.db_adress + "/transactions", headers=headers) @@ -201,6 +217,8 @@ class API_Handler: if req.status_code == 200: transactions_dict = dict(req.json()["data"]) return transactions_dict + else: + return self.get_user_transactions(user_id, max_retries-1) def set_transaction(self, user_id, count, price, symbol, timestamp): @@ -223,20 +241,27 @@ class API_Handler: return req.status_code - def get_user_portfolio(self, user_id): + def get_user_portfolio(self, user_id, max_retries=10): """gets the portfolio of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: dict: dictionary of portfolio """ + if max_retries <= 0: + return None + with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.get(self.db_adress + "/portfolio", headers=headers) - portfolio_dict = dict(req.json()["data"]) - return portfolio_dict + if req.status_code == 200: + portfolio_dict = dict(req.json()["data"]) + return portfolio_dict + else: + return self.get_user_portfolio(user_id, max_retries-1) From a2aed53bc89451f4d1259849f12e2b3d57b1b483 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 4 Apr 2022 17:33:54 +0200 Subject: [PATCH 17/52] api adress updated --- telegram_bot/bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index b24599a..be23eb4 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -38,8 +38,8 @@ bot_version = "0.2.1" user_list = [] #create api handler -api_handler = API_Handler("https://aktienbot.flokaiser.com/api", str(os.getenv("BOT_EMAIL")), str(os.getenv("BOT_PASSWORD"))) -print(api_handler.token) +api_handler = API_Handler("https://gruppe1.testsites.info/api", str(os.getenv("BOT_EMAIL")), str(os.getenv("BOT_PASSWORD"))) +print("Webserver Token: " + str(api_handler.token)) class User: # Currently saving users in this class to test functionality -> later database def __init__(self, p_user_id, p_user_name, p_chat_id): From 643c040b32cefdb180468d606b0e01ca0d7cc742 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:57:59 +0200 Subject: [PATCH 18/52] updated and added functions --- telegram_bot/news/news_fetcher.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index f913257..332dafd 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -36,7 +36,26 @@ def get_all_news_by_keyword(keyword, from_date="2000-01-01"): # hard coded will JSON/dict: dict containing articles """ top_headlines = newsapi.get_everything(q=keyword, sources=str_sources, language='en', from_param=from_date) - return top_headlines + if(top_headlines["status"] == "ok"): + return top_headlines + else: + return None + + +def get_top_news_by_keyword(keyword): + """get top news to keyword + Args: + keyword (String): keyword for search + + Returns: + JSON/dict: dict containing articles + """ + top_headlines = newsapi.get_top_headlines(q=keyword, sources=str_sources, language='en') + if(top_headlines["status"] == "ok"): + return top_headlines + else: + return None + def format_article(article): """format article for messaging (using markdown syntax) From 96e96b4612922f155278fc205a67a23f4546ae0f Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:59:00 +0200 Subject: [PATCH 19/52] updated functions, added fail handling and messages --- telegram_bot/bot.py | 88 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 15 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index be23eb4..c50ad61 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -14,6 +14,7 @@ __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 @@ -45,8 +46,8 @@ class User: # Currently saving users in this class to test functionality -> late def __init__(self, p_user_id, p_user_name, p_chat_id): """ Initialize a new user - :type self: - :param self: for class + :type self: User + :param self: object of the class :type p_user_id: int :param p_user_id: telegram user id @@ -115,7 +116,7 @@ def send_welcome(message): :rtype: none """ - bot.reply_to(message, "/id or /auth for authentication. /update to get updates on your shares. /users to see all users. /news to get current use for your keywords. /share to get price of specific share. For further details see aktienbot.flokaiser.com") + bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/users see all users.\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\n_For further details see https://gruppe1.testsites.info _", parse_mode='MARKDOWN') @bot.message_handler(commands=['users']) @@ -152,7 +153,7 @@ def send_id(message): :rtype: none """ - answer = 'Your ID/Authentication Code is: [' + str(message.from_user.id) + ']. Enter this code in the settings on aktienbot.flokaiser.com to get updates on your shares.' + answer = 'Your ID/Authentication Code is: [' + str(message.from_user.id) + ']. Enter this code in the settings on https://gruppe1.testsites.info to get updates on your shares.' bot.reply_to(message, answer) @@ -205,7 +206,7 @@ def send_update(message): 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)}' + 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) @@ -232,12 +233,12 @@ def send_share_price(message): bot.reply_to(message, str_share_price) -@bot.message_handler(commands=['news']) -def send_news(message): +@bot.message_handler(commands=['allnews']) +def send_all_news(message): """ Get news for keywords of user :type message: message object bot - :param message: message that was reacted to, in this case always containing '/news' + :param message: message that was reacted to, in this case always containing '/allnews' :raises: none @@ -246,13 +247,22 @@ def send_news(message): user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) + + if keywords == 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 keywords: + bot.send_message(chat_id=user_id, text='You have no keywords. Please add some keywords with /news') + return + keywords_search = ' OR '.join(keywords) print(keywords_search) now = dt.datetime.now().date() from_date = now - dt.timedelta(days=7) from_date_formatted = dt.datetime.strftime(from_date, '%Y-%m-%d') print(from_date_formatted) - news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)["articles"][:5] + news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)["articles"] if news_list: for article in news_list: @@ -262,6 +272,40 @@ def send_news(message): bot.send_message(chat_id=user_id, text='No news found for your keywords.') +@bot.message_handler(commands=['news']) +def send_news(message): + """ Get news for keywords of user + + :type message: message object bot + :param message: message that was reacted to, in this case always containing '/news' + + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + keywords = api_handler.get_user_keywords(user_id) + + if keywords == 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 keywords: + bot.send_message(chat_id=user_id, text='You have no keywords. Please add some keywords with /addkeyword') + return + + if keywords: + for keyword in keywords: + top_news = news.get_top_news_by_keyword(keyword)["articles"] + if top_news == None: + bot.send_message(chat_id=user_id, text='News Server did not respond correctly. Try again later.') + return + if not top_news: + bot.send_message(chat_id=user_id, text=f'No news found for keyword: *{keyword}*', parse_mode="MARKDOWN") + return + + formatted_article = news.format_article(top_news[0]) + bot.send_message(chat_id=user_id, text=f"_keyword: {keyword}_\n\n" + formatted_article, parse_mode="MARKDOWN") @bot.message_handler(commands=['addkeyword']) @@ -282,8 +326,11 @@ def store_keyword(message): user_id = int(message.from_user.id) print(str(user_id)) keyword = str(message.text).lower() - api_handler.set_keyword(user_id, keyword) - bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" added.') + status = api_handler.set_keyword(user_id, keyword) + 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.message_handler(commands=['removekeyword']) @@ -303,8 +350,11 @@ def remove_keyword(message): def remove_keyword_step(message): user_id = int(message.from_user.id) keyword = str(message.text).lower() - api_handler.delete_keyword(user_id, keyword) - bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" removed.') + status = api_handler.delete_keyword(user_id, keyword) + if status == 200: + bot.send_message(chat_id=user_id, text=f'Keyword "{keyword}" removed.') + else: + bot.send_message(chat_id=user_id, text=f'Failed deleting keyword "{keyword}". (statuscode {status})') @bot.message_handler(commands=['keywords']) @@ -319,10 +369,18 @@ def send_keywords(message): """ user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) - bot.send_message(chat_id=user_id, text=f'Your keywords are: {keywords}') + if keywords == 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 keywords: + bot.send_message(chat_id=user_id, text='No keywords set for this account. Add keywords by using /addkeyword') + return + else: + keywords_str = ', '.join(keywords) + bot.send_message(chat_id=user_id, text=f'Your keywords are: _{keywords_str}_', parse_mode="MARKDOWN") -@bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement +@bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement def echo_all(message): """ Tell that command is not known if it is no known command From 3711331338b61bd954e93ad462c44deeda482c3c Mon Sep 17 00:00:00 2001 From: Rripped <75929322+Rripped@users.noreply.github.com> Date: Mon, 4 Apr 2022 19:01:23 +0200 Subject: [PATCH 20/52] Update README.md --- documentation/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/README.md b/documentation/README.md index 3bdec2f..80c8665 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -1,7 +1,7 @@ # Dokumentation ## Swagger Documentation -Visit https://aktienbot.flokaiser.com/api/docs +Visit https://gruppe1.testsites.info/api/docs ## API - `api/openapi.json` From 9d10b1a048f42df4073b0df79087be0d121d41d3 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 10:07:03 +0200 Subject: [PATCH 21/52] New file for regularly sending bot updates --- telegram_bot/bot_updates.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 telegram_bot/bot_updates.py diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py new file mode 100644 index 0000000..aaacd7b --- /dev/null +++ b/telegram_bot/bot_updates.py @@ -0,0 +1,7 @@ +""" +script for regularly sending updates on shares and news based on user interval +""" +__author__ = "Florian Kellermann, Linus Eickhoff" +__date__ = "05.04.2022" +__version__ = "0.0.1" +__license__ = "None" \ No newline at end of file From e85bbacdde74f751a6101335540e83a6b1470fb2 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 12:38:20 +0200 Subject: [PATCH 22/52] Example and description of crontab syntax --- telegram_bot/bot_updates.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index aaacd7b..08a3db8 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -4,4 +4,17 @@ script for regularly sending updates on shares and news based on user interval __author__ = "Florian Kellermann, Linus Eickhoff" __date__ = "05.04.2022" __version__ = "0.0.1" -__license__ = "None" \ No newline at end of file +__license__ = "None" + +''' +* * * * * code +┬ ┬ ┬ ┬ ┬ +│ │ │ │ │ +│ │ │ │ └──── weekday (0-7, Sunday is 0 or 7) +│ │ │ └────── Month (1-12) +│ │ └──────── Day (1-31) +│ └────────── Hour (0-23) +└──────────── Minute (0-59) + +example 0 8 * * * -> daily update at 8am +''' From 40d444f36c41fa9bc2265905c0e3620a01f04b41 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 14:00:33 +0200 Subject: [PATCH 23/52] bot-updates got a send to user function and will be working with crontab --- telegram_bot/bot.py | 2 +- telegram_bot/bot_updates.py | 40 ++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c50ad61..83582ba 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -419,7 +419,7 @@ def query_text(inline_query): def main_loop(): - """ Get Information about bot status every 3 seconds + """ Start bot :raises: none :rtype: none diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 08a3db8..88c3e86 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -6,11 +6,16 @@ __date__ = "05.04.2022" __version__ = "0.0.1" __license__ = "None" +from shares.share_fetcher import get_share_price +import time +from bot import bot +import sys + ''' * * * * * code ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ -│ │ │ │ └──── weekday (0-7, Sunday is 0 or 7) +│ │ │ │ └──── weekday (0->Monday, 7->Sunday) │ │ │ └────── Month (1-12) │ │ └──────── Day (1-31) │ └────────── Hour (0-23) @@ -18,3 +23,36 @@ __license__ = "None" example 0 8 * * * -> daily update at 8am ''' + +def main_loop(): + """ main loop for regularly sending updates + :raises: none + + :rtype: none + """ + + +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 + """ + bot.send_message(chat_id=pUser_id, text=pText) + + +if __name__ == "__main__": + print('This script shall not be run directly. Starting main_loop for debugging purposes.') + try: + main_loop() + sys.exit(-1) + except KeyboardInterrupt: + print("Ending") + sys.exit(-1) \ No newline at end of file From 2c0bbe31111cd4c6e9a0ab316346add54cc8ddcc Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 14:12:12 +0200 Subject: [PATCH 24/52] Work in progress, need some communication first. Working on bot_updates --- telegram_bot/bot_updates.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 88c3e86..d5f351b 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -24,6 +24,9 @@ import sys example 0 8 * * * -> daily update at 8am ''' +user_ids = [] +user_crontab = [] + def main_loop(): """ main loop for regularly sending updates :raises: none @@ -31,6 +34,9 @@ def main_loop(): :rtype: none """ + current_time = time.ctime() + send_to_user(current_time) + def send_to_user(pText, pUser_id = 1770205310): From 7b99be963311696451a954938062a40ded09ebea Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 15:07:06 +0200 Subject: [PATCH 25/52] Updating contrab based on time is working, need api calls --- telegram_bot/bot_updates.py | 51 +++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index d5f351b..e1fe445 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -8,8 +8,10 @@ __license__ = "None" from shares.share_fetcher import get_share_price import time +import datetime from bot import bot import sys +from multiprocessing import Process ''' * * * * * code @@ -34,8 +36,53 @@ def main_loop(): :rtype: none """ - current_time = time.ctime() - send_to_user(current_time) + current_time_datetime = datetime.datetime.now() + + print(time.ctime()) # Debug + + p1 = Process(target= update_crontab, args=(current_time_datetime, )) #Test threading instead of multiprocessing + p1.start() + p3 = Process(target= update_based_on_crontab, args=(current_time_datetime, ) ) + p3.daemon = True + p3.start() + p1.join() + p3.terminate() + p1.terminate() + + +def update_crontab(pCurrent_Time): + """ Updating crontab lists every hour + :type pCurrent_Time: time when starting crontab update + :param pCurrent_Time: datetime + + :raises: none + + :rtype: none + """ + + # Update user info now + + print('in update_crontab') + + user_ids.clear() # Example for me (Florian Kellermann) + user_crontab.clear() + user_ids.append(1770205310) + user_crontab.append("0 8 * * *") + + while True: + time_difference = datetime.datetime.now() - pCurrent_Time + if float(str(time_difference).split(':')[0]) >=1: + update_crontab(datetime.datetime.now()) + + +def update_based_on_crontab(pCurrent_Time): + current_time_ctime = time.ctime() + for i in range(len(user_crontab)): + print('tbd') + + + time.sleep(60) + def send_to_user(pText, pUser_id = 1770205310): From 5311c891939baa452bf89f2bc832e7da16ac83e6 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 15:12:51 +0200 Subject: [PATCH 26/52] minor updates --- telegram_bot/bot_updates.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index e1fe445..e729e8a 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -40,7 +40,7 @@ def main_loop(): print(time.ctime()) # Debug - p1 = Process(target= update_crontab, args=(current_time_datetime, )) #Test threading instead of multiprocessing + p1 = Process(target= update_crontab, args=(current_time_datetime, )) p1.start() p3 = Process(target= update_based_on_crontab, args=(current_time_datetime, ) ) p3.daemon = True @@ -73,6 +73,7 @@ def update_crontab(pCurrent_Time): time_difference = datetime.datetime.now() - pCurrent_Time if float(str(time_difference).split(':')[0]) >=1: update_crontab(datetime.datetime.now()) + break def update_based_on_crontab(pCurrent_Time): From aee793deb4ed2775ef1cdf8a1e4fa8732e5f5024 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 15:17:04 +0200 Subject: [PATCH 27/52] creating sends from crontab in work --- telegram_bot/bot_updates.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index e729e8a..5467aad 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -78,13 +78,29 @@ def update_crontab(pCurrent_Time): def update_based_on_crontab(pCurrent_Time): current_time_ctime = time.ctime() + + if str(current_time_ctime).split(' ')[0] == "Mon": + current_day = 0 + elif str(current_time_ctime).split(' ')[0] == "Tue": + current_day = 1 + elif str(current_time_ctime).split(' ')[0] == "Wed": + current_day = 3 + elif str(current_time_ctime).split(' ')[0] == "Thu": + current_day = 4 + elif str(current_time_ctime).split(' ')[0] == "Fri": + current_day = 5 + elif str(current_time_ctime).split(' ')[0] == "Sat": + current_day = 6 + elif str(current_time_ctime).split(' ')[0] == "Sun": + current_day = 7 + else: + print('Error with time code') + sys.exit(-1) + for i in range(len(user_crontab)): print('tbd') - time.sleep(60) - - def send_to_user(pText, pUser_id = 1770205310): From 84754dd54f46b2b2d6ecc9f74e80cf89941ce984 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 5 Apr 2022 18:23:11 +0200 Subject: [PATCH 28/52] Update to bot_updates --- telegram_bot/bot_updates.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 5467aad..a709bf7 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -78,28 +78,49 @@ def update_crontab(pCurrent_Time): def update_based_on_crontab(pCurrent_Time): current_time_ctime = time.ctime() + ctime_split = str(current_time_ctime).split(' ') - if str(current_time_ctime).split(' ')[0] == "Mon": + if [0] == "Mon": current_day = 0 - elif str(current_time_ctime).split(' ')[0] == "Tue": + elif ctime_split[0] == "Tue": current_day = 1 - elif str(current_time_ctime).split(' ')[0] == "Wed": + elif ctime_split[0] == "Wed": current_day = 3 - elif str(current_time_ctime).split(' ')[0] == "Thu": + elif ctime_split[0] == "Thu": current_day = 4 - elif str(current_time_ctime).split(' ')[0] == "Fri": + elif ctime_split[0] == "Fri": current_day = 5 - elif str(current_time_ctime).split(' ')[0] == "Sat": + elif ctime_split[0] == "Sat": current_day = 6 - elif str(current_time_ctime).split(' ')[0] == "Sun": + elif ctime_split[0] == "Sun": current_day = 7 else: print('Error with time code') sys.exit(-1) for i in range(len(user_crontab)): - print('tbd') + day_match = False + hour_match = False + user_crontab[i] = user_crontab.strip() + + contrab_split = str(user_crontab[i]).split(' ') + + if ',' in contrab_split[4]: # if the user wants to be alerted on multiple specific days + contrab_days = contrab_split[4].split(',') + else: + contrab_days = contrab_days + + for element in contrab_days: + if str(current_day) == element or element=='*': + hour_match = True + + + + + if hour_match and day_match: + send_to_user("regular update", pUser_id=user_crontab[i]) + def send_to_user(pText, pUser_id = 1770205310): From 813231449e6b0101a742bd7913bcd828e589082a Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Wed, 6 Apr 2022 14:26:52 +0200 Subject: [PATCH 29/52] Mistake corrected --- telegram_bot/bot_updates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index a709bf7..821de90 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -80,7 +80,7 @@ def update_based_on_crontab(pCurrent_Time): current_time_ctime = time.ctime() ctime_split = str(current_time_ctime).split(' ') - if [0] == "Mon": + if ctime_split[0] == "Mon": current_day = 0 elif ctime_split[0] == "Tue": current_day = 1 From 27858a3b5993eacd16da6d5004d8c6f23394259f Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 10:08:41 +0200 Subject: [PATCH 30/52] added crontab put function --- telegram_bot/api_handling/api_handler.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 88810ce..bc8bc8a 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -263,7 +263,20 @@ class API_Handler: else: return self.get_user_portfolio(user_id, max_retries-1) + def set_cron_interval(self, user_id, cron_interval): + """sets the cron interval of the user + Args: + user_id (int): id of the user + cron_interval (String): Update interval in cron format => see https://crontab.guru/ for formatting + + Returns: + int: status code + """ + 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) + return req.status_code if __name__ == "__main__": From 2e48d2a27ee0a3b5621f471f2a81d07ad92f11d1 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 10:12:35 +0200 Subject: [PATCH 31/52] removed unused --- telegram_bot/news/news_fetcher.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index 332dafd..2b79721 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -6,7 +6,6 @@ __date__ = "15.03.2022" __version__ = "0.0.1" __license__ = "None" -from ast import parse import sys import os import json @@ -26,7 +25,7 @@ sources = source_json["sources"] str_sources = ",".join([source["id"] for source in sources]) -def get_all_news_by_keyword(keyword, from_date="2000-01-01"): # hard coded will change soon +def get_all_news_by_keyword(keyword, from_date="2000-01-01"): """get all news to keyword Args: keyword (String): keyword for search @@ -80,4 +79,5 @@ if __name__ == '__main__': articles = get_all_news_by_keyword("bitcoin") formatted_article = format_article(articles["articles"][0]) - print(formatted_article) \ No newline at end of file + print(formatted_article) + sys.exit(1) \ No newline at end of file From 30bccbf2381d884096f348626a6a16763c0cd169 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 10:41:48 +0200 Subject: [PATCH 32/52] added user funcs of api --- telegram_bot/api_handling/api_handler.py | 48 +++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index bc8bc8a..33c9a41 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -74,6 +74,51 @@ class API_Handler: return None + def get_user(self, user_id, max_retries=10): + """gets the shares of the user + + Args: + user_id (int): id of the user + max_retries (int): max retries for the request + + Returns: + json: json of user infos + """ + if max_retries <= 0: + return None + + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/user", headers=headers) + if(req.status_code == 200): + return req.json()["data"] + + else: + return self.get_user(user_id, max_retries-1) + + + def get_all_users(self, max_retries=10): + """gets all users + + Args: + max_retries (int): max retries for the request + + Returns: + list: list of users + """ + if max_retries <= 0: + return None + + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token} + req = s.get(self.db_adress + "/users", headers=headers) + if(req.status_code == 200): + return req.json()["data"] + + else: + return self.get_all_users(max_retries-1) + + def get_user_keywords(self, user_id, max_retries=10): """gets the keywords of the user @@ -282,10 +327,11 @@ class API_Handler: if __name__ == "__main__": print("This is a module for the telegram bot. It is not intended to be run directly.") - handler = API_Handler("https://aktienbot.flokaiser.com/api", "bot@example.com", "bot") + handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot") print(handler.token) keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) print(keywords) shares = handler.get_user_portfolio(user_id = 1709356058) + print(handler.set_cron_interval(user_id = 1709356058, cron_interval = "0 0 * * *")) print(shares) sys.exit(1) \ No newline at end of file From 561ce58d73274d147569f54613940cf054f18c0b Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 11:32:33 +0200 Subject: [PATCH 33/52] added new lib --- telegram_bot/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/telegram_bot/requirements.txt b/telegram_bot/requirements.txt index 6b0de09..2e8462d 100644 --- a/telegram_bot/requirements.txt +++ b/telegram_bot/requirements.txt @@ -4,3 +4,4 @@ yfinance~=0.1.70 newsapi-python~=0.2.6 python-dotenv~=0.20.0 requests~=2.27.1 +APScheduler~=3.9.1 From c8d461b747ea6746d0dfc6065a831c003f4083c7 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 11:33:21 +0200 Subject: [PATCH 34/52] tested new methods --- telegram_bot/api_handling/api_handler.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 33c9a41..b1ce853 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -332,6 +332,10 @@ if __name__ == "__main__": keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) print(keywords) shares = handler.get_user_portfolio(user_id = 1709356058) - print(handler.set_cron_interval(user_id = 1709356058, cron_interval = "0 0 * * *")) + print("set cron with status: "+ str(handler.set_cron_interval(user_id = 1709356058, cron_interval = "0 0 * * *"))) + user = handler.get_user(user_id = 1709356058) + print(user) + all_users = handler.get_all_users() + print(all_users) print(shares) sys.exit(1) \ No newline at end of file From 1095c6378850de3dbb831f51eb5a43809c0f4697 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 11:33:53 +0200 Subject: [PATCH 35/52] added scheduler --- telegram_bot/bot_scheduler.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 telegram_bot/bot_scheduler.py diff --git a/telegram_bot/bot_scheduler.py b/telegram_bot/bot_scheduler.py new file mode 100644 index 0000000..e69de29 From acdd46780c27991247dc55a391bcf1e73c35fefa Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Sun, 17 Apr 2022 10:49:42 +0200 Subject: [PATCH 36/52] added small error handling --- telegram_bot/bot_scheduler.py | 37 +++++++++++++++++++++++++++++++ telegram_bot/news/news_fetcher.py | 10 ++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/telegram_bot/bot_scheduler.py b/telegram_bot/bot_scheduler.py index e69de29..8fcef6f 100644 --- a/telegram_bot/bot_scheduler.py +++ b/telegram_bot/bot_scheduler.py @@ -0,0 +1,37 @@ +""" +script for regularly sending updates on shares and news based on user interval +""" +__author__ = "Florian Kellermann, Linus Eickhoff" +__date__ = "05.04.2022" +__version__ = "0.0.1" +__license__ = "None" + +import shares.share_fetcher as share_fetcher +import news.news_fetcher as news_fetcher + +import datetime +import sys +from apscheduler.schedulers.blocking import BlockingScheduler + +''' +* * * * * code +┬ ┬ ┬ ┬ ┬ +│ │ │ │ │ +│ │ │ │ └──── weekday (0->Monday, 7->Sunday) +│ │ │ └────── Month (1-12) +│ │ └──────── Day (1-31) +│ └────────── Hour (0-23) +└──────────── Minute (0-59) + +example 0 8 * * * -> daily update at 8am +''' + +def user_updates(): + """sends timed updates automatically to user + + Args: + + Returns: + + """ + return \ No newline at end of file diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index 2b79721..96f50ff 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -20,9 +20,13 @@ load_dotenv() # Init api_key = os.getenv('NEWS_API_KEY') newsapi = NewsApiClient(api_key=api_key) -source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json() -sources = source_json["sources"] -str_sources = ",".join([source["id"] for source in sources]) +try: + source_json = requests.get(f"https://newsapi.org/v2/top-headlines/sources?apiKey={api_key}&language=en").json() + sources = source_json["sources"] + str_sources = ",".join([source["id"] for source in sources]) +except KeyError: + print("Error: Could not get sources") + sys.exit(1) def get_all_news_by_keyword(keyword, from_date="2000-01-01"): From 5078660ce19bbb15f83c9b4dec8442b257aee96c Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Sun, 17 Apr 2022 11:21:49 +0200 Subject: [PATCH 37/52] fixed dict errors --- telegram_bot/api_handling/api_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index b1ce853..fa4a37e 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -260,7 +260,7 @@ class API_Handler: req = s.get(self.db_adress + "/transactions", headers=headers) if req.status_code == 200: - transactions_dict = dict(req.json()["data"]) + transactions_dict = req.json()["data"] return transactions_dict else: return self.get_user_transactions(user_id, max_retries-1) @@ -303,7 +303,7 @@ class API_Handler: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} req = s.get(self.db_adress + "/portfolio", headers=headers) if req.status_code == 200: - portfolio_dict = dict(req.json()["data"]) + portfolio_dict = req.json()["data"] return portfolio_dict else: return self.get_user_portfolio(user_id, max_retries-1) 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 38/52] 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): From b06b02432680a642cc06016a6ac3ff11f2cdd753 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Sun, 17 Apr 2022 12:20:53 +0200 Subject: [PATCH 39/52] bot new commands and funcs + api updates --- telegram_bot/api_handling/api_handler.py | 6 ++- telegram_bot/bot.py | 49 ++++++++++++++++++++++++ telegram_bot/requirements.txt | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) 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 From caa90a577f4b339a0f97af74540f4b3cd3704a91 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Sun, 17 Apr 2022 12:35:59 +0200 Subject: [PATCH 40/52] testing --- telegram_bot/bot.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index e2cb748..7b5e38d 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -115,7 +115,7 @@ def send_welcome(message): :rtype: none """ - bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/users see all users.\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\n_For further details see https://gruppe1.testsites.info _", parse_mode='MARKDOWN') + bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/users see all users.\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']) @@ -379,7 +379,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']) +@bot.message_handler(commands=['portfolio']) #tbd def send_portfolio(message): """ Send portfolio of user :type message: message object bot @@ -402,7 +402,7 @@ def send_portfolio(message): bot.send_message(chat_id=user_id, text=f'Your portfolio is: _{portfolio_str}_', parse_mode="MARKDOWN") -@bot.message_handler(commands=['newtransaction']) +@bot.message_handler(commands=['newtransaction']) #tbd def set_new_transaction(message): """ Set new transaction for user :type message: message object bot @@ -437,7 +437,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']) #tbd def send_interval(message): """ send interval for user :type message: message object bot @@ -448,12 +448,12 @@ def send_interval(message): :rtype: none """ user_id = int(message.from_user.id) - interval = api_handler.get_user(user_id)['cron'] + interval = api_handler.get_user(user_id) 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) + interval = str(interval['cron']) 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") @@ -469,7 +469,7 @@ def set_new_interval(message): :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.send_message(chat_id=user_id, text='Type interval in cron format:\n(https://crontab.guru/)') bot.register_next_step_handler(message, set_new_interval_step) def set_new_interval_step(message): @@ -482,7 +482,7 @@ def set_new_interval_step(message): bot.send_message(chat_id=user_id, text='Interval succesfully set.') return - if status == -1: + if status == -1: # only -1 when interval is invalid bot.send_message(chat_id=user_id, text='Invalid interval. Try again with /setinterval.') return else: From 26b41f2c94576c38980346008996c4e3786d1027 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Sun, 17 Apr 2022 13:07:57 +0200 Subject: [PATCH 41/52] fixing and debugging --- telegram_bot/bot.py | 60 +++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 7b5e38d..c3ab17e 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -115,7 +115,7 @@ def send_welcome(message): :rtype: none """ - bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/users see all users.\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.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']) @@ -140,7 +140,26 @@ def send_all_users(message): answer = str(known_user.user_id) + ' : ' + known_user.user_name bot.send_message(chat_id=user_id, text=answer) + +@bot.message_handler(commands=['me']) +def send_user(message): + """ Send user data + :type message: message object bot + :param message: message that was reacted to, in this case always containing '/me' + :raises: none + + :rtype: none + """ + user_id = int(message.from_user.id) + user_data = api_handler.get_user(user_id) # tbd: formatting + if not user_data or user_data == None: + bot.reply_to(message, "This didn\'t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info") + return + + 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 def send_id(message): @@ -248,7 +267,7 @@ def send_all_news(message): keywords = api_handler.get_user_keywords(user_id) if keywords == 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') + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info') return if not keywords: @@ -286,7 +305,7 @@ def send_news(message): keywords = api_handler.get_user_keywords(user_id) if keywords == 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') + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info') return if not keywords: @@ -369,7 +388,7 @@ def send_keywords(message): user_id = int(message.from_user.id) keywords = api_handler.get_user_keywords(user_id) if keywords == 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') + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info') return if not keywords: bot.send_message(chat_id=user_id, text='No keywords set for this account. Add keywords by using /addkeyword') @@ -392,17 +411,18 @@ def send_portfolio(message): 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') + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure to 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") + # tbd: format portfolio + + bot.send_message(chat_id=user_id, text=f'Your portfolio is: _{str(portfolio)}_', parse_mode="MARKDOWN") -@bot.message_handler(commands=['newtransaction']) #tbd +@bot.message_handler(commands=['newtransaction']) #tbd not working rn def set_new_transaction(message): """ Set new transaction for user :type message: message object bot @@ -413,14 +433,14 @@ 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. Try again with /newtransaction.') + 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.') return transaction_data = str(message.text).split(',') @@ -428,7 +448,8 @@ def set_new_transaction_step(message): amount = float(transaction_data[1]) price = float(transaction_data[2]) time = dt.datetime.now() - + #print("\n\n\n\n\n") + #print(f"{symbol},{amount},{price},{time}") status = api_handler.set_transaction(user_id, amount, price, symbol, time) if status == 200: @@ -448,14 +469,17 @@ def send_interval(message): :rtype: none """ user_id = int(message.from_user.id) - interval = api_handler.get_user(user_id) - 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') + user_data = api_handler.get_user(user_id) + if user_data == None: + bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info and set an interval with /setinterval') return else: - interval = str(interval['cron']) + interval = str(user_data['cron']) + if interval == 'None': + bot.send_message(chat_id=user_id, text='You do not have an interval set. Set one with /setinterval') + return 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.send_message(chat_id=user_id, text=f'Your update interval: {interval} (https://crontab.guru/#{formatted_interval})') @bot.message_handler(commands=['setinterval']) @@ -483,7 +507,7 @@ def set_new_interval_step(message): return if status == -1: # only -1 when interval is invalid - bot.send_message(chat_id=user_id, text='Invalid interval. Try again with /setinterval.') + bot.send_message(chat_id=user_id, text='Invalid interval format. Try again with\n /setinterval.') return else: bot.send_message(chat_id=user_id, text=f'Failed setting interval. (statuscode {status})') From 1803bba222b56ec7e196a5721589b9d91f3bfdb7 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Sun, 17 Apr 2022 13:26:12 +0200 Subject: [PATCH 42/52] Not working fully yet, still problems with scheduler, might try other module later --- telegram_bot/bot_updates.py | 98 ++++++++++++++++------------------- telegram_bot/requirements.txt | 3 +- telegram_bot/testfile.py | 41 +++++++++++++++ 3 files changed, 87 insertions(+), 55 deletions(-) create mode 100644 telegram_bot/testfile.py diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 821de90..7ed1bf2 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -6,12 +6,16 @@ __date__ = "05.04.2022" __version__ = "0.0.1" __license__ = "None" +from calendar import month from shares.share_fetcher import get_share_price import time import datetime from bot import bot import sys from multiprocessing import Process +from apscheduler.schedulers.blocking import BlockingScheduler +from api_handling.api_handler import API_Handler + ''' * * * * * code @@ -25,7 +29,6 @@ from multiprocessing import Process example 0 8 * * * -> daily update at 8am ''' - user_ids = [] user_crontab = [] @@ -37,20 +40,24 @@ def main_loop(): """ current_time_datetime = datetime.datetime.now() + my_handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot") print(time.ctime()) # Debug - p1 = Process(target= update_crontab, args=(current_time_datetime, )) + """p1 = Process(target= update_crontab, args=(current_time_datetime, my_handler )) p1.start() - p3 = Process(target= update_based_on_crontab, args=(current_time_datetime, ) ) + time.sleep(5) + p3 = Process(target= update_based_on_crontab, args=() ) p3.daemon = True p3.start() p1.join() p3.terminate() - p1.terminate() + p1.terminate()""" + + update_crontab(current_time_datetime, my_handler) -def update_crontab(pCurrent_Time): +def update_crontab(pCurrent_Time, p_my_handler): """ Updating crontab lists every hour :type pCurrent_Time: time when starting crontab update :param pCurrent_Time: datetime @@ -60,14 +67,23 @@ def update_crontab(pCurrent_Time): :rtype: none """ - # Update user info now + global user_crontab + global user_ids - print('in update_crontab') + p_my_handler.set_cron_interval(user_id = 1770205310, cron_interval = "50 11 * * *") - user_ids.clear() # Example for me (Florian Kellermann) - user_crontab.clear() - user_ids.append(1770205310) - user_crontab.append("0 8 * * *") + all_users = p_my_handler.get_all_users() + + user_ids = [] + user_crontab = [] + + for element in all_users: + if element["cron"] != '' and element["telegram_user_id"] != '': + user_ids.append(int(element["telegram_user_id"])) + user_crontab.append(str(element["cron"])) + + print(user_ids) + update_based_on_crontab() while True: time_difference = datetime.datetime.now() - pCurrent_Time @@ -76,52 +92,26 @@ def update_crontab(pCurrent_Time): break -def update_based_on_crontab(pCurrent_Time): - current_time_ctime = time.ctime() - ctime_split = str(current_time_ctime).split(' ') +def update_based_on_crontab(): - if ctime_split[0] == "Mon": - current_day = 0 - elif ctime_split[0] == "Tue": - current_day = 1 - elif ctime_split[0] == "Wed": - current_day = 3 - elif ctime_split[0] == "Thu": - current_day = 4 - elif ctime_split[0] == "Fri": - current_day = 5 - elif ctime_split[0] == "Sat": - current_day = 6 - elif ctime_split[0] == "Sun": - current_day = 7 - else: - print('Error with time code') - sys.exit(-1) + my_scheduler = BlockingScheduler() - for i in range(len(user_crontab)): - day_match = False - hour_match = False - - user_crontab[i] = user_crontab.strip() - - contrab_split = str(user_crontab[i]).split(' ') - - if ',' in contrab_split[4]: # if the user wants to be alerted on multiple specific days - contrab_days = contrab_split[4].split(',') - else: - contrab_days = contrab_days - - for element in contrab_days: - if str(current_day) == element or element=='*': - hour_match = True - - - - - if hour_match and day_match: - send_to_user("regular update", pUser_id=user_crontab[i]) + print("update based on cron") + + print(len(user_ids)) #Debug + + for i in range(len(user_ids)): + print("in loop") + cron_split = user_crontab[i].split(" ") + print(cron_split[4], cron_split[1], cron_split[0], cron_split[3], cron_split[2]) + my_scheduler.add_job(update_for_user, 'cron', day_of_week = cron_split[4] , hour= cron_split[1] , minute = cron_split[0], month= cron_split[3] , day=cron_split[2], args=(user_ids[i], )) + + my_scheduler.start() + + print("update based on crontab") - +def update_for_user(p_user_id): + print("updating for {p_user_id}") def send_to_user(pText, pUser_id = 1770205310): diff --git a/telegram_bot/requirements.txt b/telegram_bot/requirements.txt index 146877f..6620615 100644 --- a/telegram_bot/requirements.txt +++ b/telegram_bot/requirements.txt @@ -5,4 +5,5 @@ newsapi-python~=0.2.6 python-dotenv~=0.20.0 requests~=2.27.1 APScheduler~=3.9.1 -cronitor~=1.3.4 +croniter~=1.3.4 +tzlocal==2.1 diff --git a/telegram_bot/testfile.py b/telegram_bot/testfile.py new file mode 100644 index 0000000..4369096 --- /dev/null +++ b/telegram_bot/testfile.py @@ -0,0 +1,41 @@ +from multiprocessing import Process +import time + +class Array_Class: + def __init__(self): + self.array = [] + + def SetArray(self, parray): + array = parray + + def GetArray(self): + return self.array + +my_array = Array_Class() + + +def update_crontab(): + global my_array + array = [] + time.sleep(5) + array.append(1) + my_array.SetArray(array) + print("set done") + +def update_based_on_crontab(): + time.sleep(5) + print(my_array.GetArray()) + + +"""update_crontab() +update_based_on_crontab()""" + +if __name__ == "__main__": + p1 = Process(target= update_crontab, args=()) + p1.start() + p3 = Process(target= update_based_on_crontab, args=() ) + p3.daemon = True + p3.start() + p1.join() + p3.terminate() + p1.terminate() \ No newline at end of file From 689a8cb1fcf98cfc9381b8eba4cddd7b3a095ad7 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Sun, 17 Apr 2022 13:27:25 +0200 Subject: [PATCH 43/52] Removed testfile --- telegram_bot/testfile.py | 41 ---------------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 telegram_bot/testfile.py diff --git a/telegram_bot/testfile.py b/telegram_bot/testfile.py deleted file mode 100644 index 4369096..0000000 --- a/telegram_bot/testfile.py +++ /dev/null @@ -1,41 +0,0 @@ -from multiprocessing import Process -import time - -class Array_Class: - def __init__(self): - self.array = [] - - def SetArray(self, parray): - array = parray - - def GetArray(self): - return self.array - -my_array = Array_Class() - - -def update_crontab(): - global my_array - array = [] - time.sleep(5) - array.append(1) - my_array.SetArray(array) - print("set done") - -def update_based_on_crontab(): - time.sleep(5) - print(my_array.GetArray()) - - -"""update_crontab() -update_based_on_crontab()""" - -if __name__ == "__main__": - p1 = Process(target= update_crontab, args=()) - p1.start() - p3 = Process(target= update_based_on_crontab, args=() ) - p3.daemon = True - p3.start() - p1.join() - p3.terminate() - p1.terminate() \ No newline at end of file From 44c57aa881856030e81a6ff4f7cea809a523cef6 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Mon, 18 Apr 2022 17:11:02 +0200 Subject: [PATCH 44/52] Almost working, having some troubles with the blockers still --- telegram_bot/bot_updates.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 7ed1bf2..ff0bf40 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -70,7 +70,7 @@ def update_crontab(pCurrent_Time, p_my_handler): global user_crontab global user_ids - p_my_handler.set_cron_interval(user_id = 1770205310, cron_interval = "50 11 * * *") + p_my_handler.set_cron_interval(user_id = 1770205310, cron_interval = "8 17 * * *") all_users = p_my_handler.get_all_users() @@ -83,16 +83,30 @@ def update_crontab(pCurrent_Time, p_my_handler): user_crontab.append(str(element["cron"])) print(user_ids) - update_based_on_crontab() + update_based_on_crontab(user_ids, user_crontab) + + p1 = Process(target= sleeping_time, args=(pCurrent_Time, )) + p1.start() + p3 = Process(target= update_based_on_crontab, args=(user_ids, user_crontab ) ) + p3.daemon = True + p3.start() + p1.join() + p3.terminate() + p1.terminate() + update_crontab(datetime.datetime.now(), p_my_handler) + + + +def sleeping_time(pCurrent_Time): while True: time_difference = datetime.datetime.now() - pCurrent_Time - if float(str(time_difference).split(':')[0]) >=1: + print(time_difference) + if float(str(time_difference).split(':')[1]) >=1: # every minute (0:00:03.646070) -> example time code update_crontab(datetime.datetime.now()) break - -def update_based_on_crontab(): +def update_based_on_crontab(p_user_ids, p_user_crontab): my_scheduler = BlockingScheduler() @@ -100,15 +114,13 @@ def update_based_on_crontab(): print(len(user_ids)) #Debug - for i in range(len(user_ids)): + for i in range(len(p_user_ids)): print("in loop") - cron_split = user_crontab[i].split(" ") + cron_split = p_user_crontab[i].split(" ") print(cron_split[4], cron_split[1], cron_split[0], cron_split[3], cron_split[2]) - my_scheduler.add_job(update_for_user, 'cron', day_of_week = cron_split[4] , hour= cron_split[1] , minute = cron_split[0], month= cron_split[3] , day=cron_split[2], args=(user_ids[i], )) + my_scheduler.add_job(update_for_user, 'cron', day_of_week = cron_split[4] , hour= cron_split[1] , minute = cron_split[0], month= cron_split[3] , day=cron_split[2], args=(p_user_ids[i], )) my_scheduler.start() - - print("update based on crontab") def update_for_user(p_user_id): print("updating for {p_user_id}") @@ -130,6 +142,7 @@ def send_to_user(pText, pUser_id = 1770205310): if __name__ == "__main__": + print('This script shall not be run directly. Starting main_loop for debugging purposes.') try: main_loop() From 6db3a7ce32e1c65d5ff73bc8d292c23d115572a6 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Mon, 18 Apr 2022 23:32:19 +0200 Subject: [PATCH 45/52] Update message working, regularity still needs implementation --- telegram_bot/bot_updates.py | 40 ++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index ff0bf40..0121d32 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -7,6 +7,7 @@ __version__ = "0.0.1" __license__ = "None" from calendar import month +from symtable import Symbol from shares.share_fetcher import get_share_price import time import datetime @@ -42,17 +43,9 @@ def main_loop(): current_time_datetime = datetime.datetime.now() my_handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot") - print(time.ctime()) # Debug - """p1 = Process(target= update_crontab, args=(current_time_datetime, my_handler )) - p1.start() - time.sleep(5) - p3 = Process(target= update_based_on_crontab, args=() ) - p3.daemon = True - p3.start() - p1.join() - p3.terminate() - p1.terminate()""" + # update_for_user(5270256395, my_handler) # Debug (running test update for kevins shares) + update_crontab(current_time_datetime, my_handler) @@ -122,8 +115,31 @@ def update_based_on_crontab(p_user_ids, p_user_crontab): my_scheduler.start() -def update_for_user(p_user_id): - print("updating for {p_user_id}") +def update_for_user(p_user_id, p_my_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/api to change your settings and add shares.") + def send_to_user(pText, pUser_id = 1770205310): From 76df6f27becf788f8f304b290378d64c19f9f66e Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 19 Apr 2022 08:30:25 +0200 Subject: [PATCH 46/52] Forgot one user id in the code --- telegram_bot/bot_updates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 0121d32..7c6a4a0 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -138,7 +138,7 @@ def update_for_user(p_user_id, p_my_handler): 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/api to change your settings and add shares.") + send_to_user("No shares found for your account. Check https://gruppe1.testsites.info/api to change your settings and add shares.", pUser_id=p_user_id) def send_to_user(pText, pUser_id = 1770205310): From 71fed7fd18c5ab7eade55c67e07c84e8eab51edb Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 19 Apr 2022 21:08:47 +0200 Subject: [PATCH 47/52] added formatting and adapted to isin --- telegram_bot/bot.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c3ab17e..0f32fe6 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -417,9 +417,12 @@ def send_portfolio(message): bot.send_message(chat_id=user_id, text='You do not have any stocks in your portfolio.') return else: - # tbd: format portfolio - - bot.send_message(chat_id=user_id, text=f'Your portfolio is: _{str(portfolio)}_', parse_mode="MARKDOWN") + for stock in portfolio: + comment = str(stock["comment"]) + count = "{:.2f}".format(float(stock["count"])) + isin = str(stock["isin"]) + worth = "{:.2f}".format(float(stock["current_price"]) * float(stock["count"])) + 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 @@ -458,7 +461,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']) #tbd +@bot.message_handler(commands=['interval']) def send_interval(message): """ send interval for user :type message: message object bot 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 48/52] 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 From 2b18e026239a0c62fe7480270303c7efea79e0bc Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Wed, 20 Apr 2022 11:03:21 +0200 Subject: [PATCH 49/52] 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 From fa8b511ef4757425e666620576e54fd679aa33de Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Thu, 21 Apr 2022 08:23:48 +0200 Subject: [PATCH 50/52] Scheduler working --- telegram_bot/bot_updates.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 7c6a4a0..b0501c4 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -15,6 +15,7 @@ from bot import bot import sys from multiprocessing import Process from apscheduler.schedulers.blocking import BlockingScheduler +from apscheduler.schedulers.background import BackgroundScheduler from api_handling.api_handler import API_Handler @@ -63,7 +64,7 @@ def update_crontab(pCurrent_Time, p_my_handler): global user_crontab global user_ids - p_my_handler.set_cron_interval(user_id = 1770205310, cron_interval = "8 17 * * *") + #p_my_handler.set_cron_interval(user_id = 1770205310, cron_interval = "23 08 * * *") all_users = p_my_handler.get_all_users() @@ -76,44 +77,30 @@ def update_crontab(pCurrent_Time, p_my_handler): user_crontab.append(str(element["cron"])) print(user_ids) - update_based_on_crontab(user_ids, user_crontab) - - p1 = Process(target= sleeping_time, args=(pCurrent_Time, )) - p1.start() - p3 = Process(target= update_based_on_crontab, args=(user_ids, user_crontab ) ) - p3.daemon = True - p3.start() - p1.join() - p3.terminate() - p1.terminate() + + update_based_on_crontab(user_ids, user_crontab, p_my_handler) update_crontab(datetime.datetime.now(), p_my_handler) +def update_based_on_crontab(p_user_ids, p_user_crontab, p_my_handler): -def sleeping_time(pCurrent_Time): - while True: - time_difference = datetime.datetime.now() - pCurrent_Time - print(time_difference) - if float(str(time_difference).split(':')[1]) >=1: # every minute (0:00:03.646070) -> example time code - update_crontab(datetime.datetime.now()) - break - -def update_based_on_crontab(p_user_ids, p_user_crontab): - - my_scheduler = BlockingScheduler() + my_scheduler = BackgroundScheduler() print("update based on cron") print(len(user_ids)) #Debug for i in range(len(p_user_ids)): - print("in loop") cron_split = p_user_crontab[i].split(" ") print(cron_split[4], cron_split[1], cron_split[0], cron_split[3], cron_split[2]) - my_scheduler.add_job(update_for_user, 'cron', day_of_week = cron_split[4] , hour= cron_split[1] , minute = cron_split[0], month= cron_split[3] , day=cron_split[2], args=(p_user_ids[i], )) + my_scheduler.add_job(update_for_user, 'cron', day_of_week = cron_split[4] , hour= cron_split[1] , minute = cron_split[0], month= cron_split[3] , day=cron_split[2], args=(p_user_ids[i], p_my_handler )) my_scheduler.start() + + print("60 second sleep") + time.sleep( 60 ) + my_scheduler.shutdown() def update_for_user(p_user_id, p_my_handler): From cac5883b99d4b0e4689cd631e21668791ca605d0 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Thu, 21 Apr 2022 10:06:08 +0200 Subject: [PATCH 51/52] Better code documentation and removing unnecessary stuff --- telegram_bot/bot_updates.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index b0501c4..6269b5e 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -3,7 +3,7 @@ script for regularly sending updates on shares and news based on user interval """ __author__ = "Florian Kellermann, Linus Eickhoff" __date__ = "05.04.2022" -__version__ = "0.0.1" +__version__ = "1.0.1" __license__ = "None" from calendar import month @@ -14,7 +14,6 @@ import datetime from bot import bot import sys from multiprocessing import Process -from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.schedulers.background import BackgroundScheduler from api_handling.api_handler import API_Handler @@ -85,9 +84,22 @@ def update_crontab(pCurrent_Time, p_my_handler): def update_based_on_crontab(p_user_ids, p_user_crontab, p_my_handler): - my_scheduler = BackgroundScheduler() + """ Check all the crontab codes and add jobs to start in time + :type p_user_ids: array + :param p_user_ids: user id array of all users + + :type p_user_crontab: array + :param p_user_crontab: crontabs for all users equivalent to the user array + + :type p_my_handler: Api_Handler + :param p_my_handler: get database stuff + + :raises: none + + :rtype: none + """ - print("update based on cron") + my_scheduler = BackgroundScheduler() print(len(user_ids)) #Debug @@ -98,12 +110,22 @@ def update_based_on_crontab(p_user_ids, p_user_crontab, p_my_handler): my_scheduler.start() - print("60 second sleep") - time.sleep( 60 ) + time.sleep( 600 ) my_scheduler.shutdown() def update_for_user(p_user_id, p_my_handler): + """ Pull shares and send updates for specific user id + :type p_user_id: integer + :param p_user_id: user id of user that shall receive update + + :type p_my_handler: Api_Handler + :param p_my_handler: handle the api and pull from database + + :raises: none + + :rtype: none + """ share_symbols = [] share_amounts = [] share_courses = [] @@ -146,7 +168,7 @@ def send_to_user(pText, pUser_id = 1770205310): if __name__ == "__main__": - print('This script shall not be run directly. Starting main_loop for debugging purposes.') + print('bot_updates.py starting.') try: main_loop() sys.exit(-1) From 78dfde7a869866d784c2535e3b84fc43e837f8a4 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Mon, 25 Apr 2022 08:05:30 +0200 Subject: [PATCH 52/52] new bot version --- telegram_bot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c30bfdf..6655ad5 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -34,7 +34,7 @@ from api_handling.api_handler import API_Handler load_dotenv(dotenv_path='.env') -bot_version = "0.2.1" +bot_version = "1.0.1" user_list = [] #create api handler