From 275444fb958979f58414c73170629083e9b8de1e Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 26 Apr 2022 10:34:17 +0200 Subject: [PATCH 01/13] Removed user class --- telegram_bot/bot.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index a2cc2a6..f5c3bef 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -41,31 +41,6 @@ bot_version = "1.0.1" # version of bot api_handler = API_Handler("https://gruppe1.testsites.info/api", str(os.getenv("BOT_EMAIL")), str(os.getenv("BOT_PASSWORD"))) # get creds from env vars. print("Webserver Token: " + str(api_handler.token)) -class User: # Currently saving users in this class to test functionality -> later database REMOVABLE - def __init__(self, p_user_id, p_user_name, p_chat_id): - - """ Initialize a new user - :type self: User - :param self: object of the class - - :type p_user_id: int - :param p_user_id: telegram user id - - :type p_user_name: str - :param p_user_name: first name of user - - :type p_chat_id: int - :param p_chat_id: telegram chat id - - :raises: - - :rtype: - """ - - self.user_id = int(p_user_id) - self.chat_id = int(p_chat_id) - self.user_name = str(p_user_name) - bot = telebot.TeleBot(os.getenv('BOT_API_KEY')) @bot.message_handler(commands=['start', 'Start']) # /start -> saving as new user and sending welcome From 42a5ad4a12399f16a47c3b6e7f3a163c8794a31d Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:07:56 +0200 Subject: [PATCH 02/13] more commenting and error correction in updater --- telegram_bot/bot.py | 3 ++- telegram_bot/bot_updates.py | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index f5c3bef..67baf98 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -118,7 +118,7 @@ def send_all_users(message): bot.send_message(chat_id=user_id, text=f'Username: {username}\nEmail: {email}\nID: {id}\nCron: {cron}\nAdmin: {admin}') # format user data into readable message text -@bot.message_handler(commands=['setAdmin', 'SetAdmin']) # set admin rights to user TBD: not working!! +@bot.message_handler(commands=['setAdmin', 'SetAdmin', 'setadmin', 'Setadmin']) # set admin rights to user TBD: not working!! def set_admin(message): """ Set admin rights to user @@ -150,6 +150,7 @@ def set_admin_step(message): email = args_message[0] is_admin = args_message[1] + status = api_handler.set_admin(email, is_admin) # set admin in db if(status == 200): diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 46df917..4853df0 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -6,17 +6,18 @@ __date__ = "05.04.2022" __version__ = "1.0.1" __license__ = "None" -from calendar import month -from symtable import Symbol +from asyncio.windows_events import NULL +from calendar import month # unused, remove? +from symtable import Symbol # unused, remove? from dotenv import load_dotenv -from shares.share_fetcher import get_share_price +from shares.share_fetcher import get_share_price # unused, remove? import news.news_fetcher as news_fetcher import time -import datetime +import datetime # unused, remove? import os from bot import bot import sys -from multiprocessing import Process +from multiprocessing import Process # unused, remove? from apscheduler.schedulers.background import BackgroundScheduler from api_handling.api_handler import API_Handler @@ -153,9 +154,16 @@ def update_for_user(p_user_id, p_my_handler): if(keywords): # if keywords exist and array is not empty send_to_user("If you haven't read yet: \nHere are some interesting news according to your keywords:", pUser_id=p_user_id) for keyword in keywords: - news = news_fetcher.get_top_news_by_keyword(keyword)["articles"][0] # only use the most popular news - news_formatted = news_fetcher.format_article(news) # format for message - send_to_user(f"_keyword: {keyword}_\n\n{news_formatted}", pUser_id=p_user_id, md_mode=True) # send news with related keyword in Markdown + news = news_fetcher.get_top_news_by_keyword(keyword)["articles"] + + if not news: # if empty news array + send_to_user(f"No news found for keyword _{keyword}_.", pUser_id=p_user_id, md_mode=True) + + if news == None: # if news is none + send_to_user(f"Server error for keyword _{keyword}_.", pUser_id=p_user_id, md_mode=True) + else: + news_formatted = news_fetcher.format_article(news[0]) # format for message, only use the most popular article + send_to_user(f"_keyword: {keyword}_\n\n{news_formatted}", pUser_id=p_user_id, md_mode=True) # send news with related keyword in Markdown From b347c106531e5c4bb632ff341d76cb2ba75c86a9 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:21:37 +0200 Subject: [PATCH 03/13] fixed /setadmin function --- telegram_bot/api_handling/api_handler.py | 5 +++-- telegram_bot/bot.py | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 181fd71..63cfcdf 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -9,6 +9,7 @@ __license__ = "None" #side-dependencies: none #Work in Progress +from email import header import sys import os import requests as r @@ -340,14 +341,14 @@ class API_Handler: Args: email (string): email of the user - is_admin (String): "true" if user should be Admin, "false" if not + is_admin (bool): "true" if user should be Admin, "false" if not Returns: int: status code """ with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token} # only bot token is needed, user is chosen by email - req = s.put(self.db_adress + "/user/setAdmin", json={"admin": str(is_admin),"email": str(email)}) + req = s.put(self.db_adress + "/user/setAdmin", json={"admin": is_admin,"email": str(email)}, headers=headers) return req.status_code diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 67baf98..331b29d 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -136,7 +136,7 @@ def set_admin(message): bot.reply_to(message, "You have to be an admin to use this command") return - bot.send_message(chat_id=user_id, text='send email and "true" if this account should have admin rights, else "false"\n in format: ,') # ask for email and admin rights to set + bot.send_message(chat_id=user_id, text='send email and true if this account should have admin rights, else false\n in format: ,') # request email and admin rights to change to bot.register_next_step_handler(message, set_admin_step) def set_admin_step(message): @@ -149,8 +149,11 @@ def set_admin_step(message): return email = args_message[0] - is_admin = args_message[1] - + is_admin = False # default: False + + if args_message[1].lower() == "true": # if user types true, set is_admin to true + is_admin = True + status = api_handler.set_admin(email, is_admin) # set admin in db if(status == 200): From 0c9cc0b4ef3288183252a6dec52554e64891b11c Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:36:01 +0200 Subject: [PATCH 04/13] fixed /newtransaction --- telegram_bot/api_handling/api_handler.py | 1 + telegram_bot/bot.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 63cfcdf..51e0ca1 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -288,6 +288,7 @@ class API_Handler: int: status code """ with r.Session() as s: + time = time[:-3] + "Z" # remove last character and add Z to make it a valid date for db headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} transaction = {"comment": str(comment), "count": float(count), "isin": str(isin), "price": float(price), "time": str(time)} # set transaction as JSON with all the attributes needed according to Swagger docs req = s.post(self.db_adress + "/transaction", json=transaction, headers=headers) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 331b29d..90f1866 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -483,8 +483,8 @@ def set_new_transaction_step(message): 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}") + print("\n\n\n\n\n") + print(f"{isin},{amount},{price},{time}") status = api_handler.set_transaction(user_id, desc, isin, amount, price, time) if status == 200: From 3abf39a1a2ace69c44bce488af8262189ef5bfdb Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:41:00 +0200 Subject: [PATCH 05/13] deleted bot creds in main --- telegram_bot/api_handling/api_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 51e0ca1..5d41e95 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -356,7 +356,7 @@ class API_Handler: if __name__ == "__main__": # editable, just for basic on the go testing of new functions print("This is a module for the telegram bot. It is not intended to be run directly.") - handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot") + handler = API_Handler("https://gruppe1.testsites.info/api", "", "") # do not push with real credentials print(handler.token) keywords = handler.get_user_keywords(user_id = 1709356058) #user_id here is currently mine (Linus) print(keywords) From 6e3870fda779d296e4884c448461a0129706f223 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:41:14 +0200 Subject: [PATCH 06/13] deleted bot creds in main --- telegram_bot/api_handling/api_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 5d41e95..bc44bb3 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -356,7 +356,7 @@ class API_Handler: if __name__ == "__main__": # editable, just for basic on the go testing of new functions print("This is a module for the telegram bot. It is not intended to be run directly.") - handler = API_Handler("https://gruppe1.testsites.info/api", "", "") # do not push with real credentials + handler = API_Handler("https://gruppe1.testsites.info/api", "", "") # do not push with real credentials!! print(handler.token) keywords = handler.get_user_keywords(user_id = 1709356058) #user_id here is currently mine (Linus) print(keywords) From 4255c82667a79585a2dc8c5ba8007d9032d58f73 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:42:20 +0200 Subject: [PATCH 07/13] updated date and version --- 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 bc44bb3..50375a1 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -2,8 +2,8 @@ script for communicating with webservice to get data from database """ __author__ = "Florian Kellermann, Linus Eickhoff" -__date__ = "16.03.2022" -__version__ = "0.0.1" +__date__ = "26.04.2022" +__version__ = "1.0.1" __license__ = "None" #side-dependencies: none From 4ec81253939b6bfc5e2247665391ec3ed1217bf8 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:44:52 +0200 Subject: [PATCH 08/13] creds now are pulled from env --- telegram_bot/api_handling/api_handler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 50375a1..5975592 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -14,6 +14,9 @@ import sys import os import requests as r from croniter import croniter # used for checking cron formatting +from dotenv import load_dotenv + +load_dotenv() # loads environment vars # note: for more information about the api visit swagger documentation on https://gruppe1.testsites.info/api/docs#/ @@ -356,7 +359,7 @@ class API_Handler: if __name__ == "__main__": # editable, just for basic on the go testing of new functions print("This is a module for the telegram bot. It is not intended to be run directly.") - handler = API_Handler("https://gruppe1.testsites.info/api", "", "") # do not push with real credentials!! + handler = API_Handler("https://gruppe1.testsites.info/api", str(os.getenv("BOT_EMAIL")), str(os.getenv("BOT_PASSWORD"))) # get creds from env print(handler.token) keywords = handler.get_user_keywords(user_id = 1709356058) #user_id here is currently mine (Linus) print(keywords) From dbc93cc31a7c0e3d5a4b49c9341b25040d4cfde3 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:46:20 +0200 Subject: [PATCH 09/13] updated version and date --- telegram_bot/bot.py | 4 ++-- telegram_bot/bot_updates.py | 4 ++-- telegram_bot/news/news_fetcher.py | 6 ++---- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 90f1866..c17734f 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -3,8 +3,8 @@ script for telegram bot and its functions """ __author__ = "Florian Kellermann, Linus Eickhoff" -__date__ = "11.03.2022" -__version__ = "0.0.4" +__date__ = "26.04.2022" +__version__ = "1.2.2" __license__ = "None" # side-dependencies: none diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index 4853df0..ff76299 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -2,8 +2,8 @@ script for regularly sending updates on shares and news based on user interval """ __author__ = "Florian Kellermann, Linus Eickhoff" -__date__ = "05.04.2022" -__version__ = "1.0.1" +__date__ = "26.04.2022" +__version__ = "1.0.2" __license__ = "None" from asyncio.windows_events import NULL diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index a6dad7e..bd083b5 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -2,15 +2,13 @@ script for news fetching (by keywords) """ __author__ = "Florian Kellermann, Linus Eickhoff" -__date__ = "15.03.2022" -__version__ = "0.0.1" +__date__ = "26.04.2022" +__version__ = "1.0.0" __license__ = "None" import sys import os -import json import requests -import datetime as dt from newsapi import NewsApiClient from dotenv import load_dotenv From fa569ff5b2c17fde1284a361bad7d7e511802fc7 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:47:44 +0200 Subject: [PATCH 10/13] removed unused imports --- telegram_bot/api_handling/api_handler.py | 1 - telegram_bot/bot.py | 2 -- telegram_bot/bot_updates.py | 6 ------ 3 files changed, 9 deletions(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index 5975592..35aef19 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -9,7 +9,6 @@ __license__ = "None" #side-dependencies: none #Work in Progress -from email import header import sys import os import requests as r diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index c17734f..1c5e92e 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -14,13 +14,11 @@ __license__ = "None" # API Documentation https://core.telegram.org/bots/api # Code examples https://github.com/eternnoir/pyTelegramBotAPI#getting-started -import email import os import telebot import sys import logging -import json import re import news.news_fetcher as news diff --git a/telegram_bot/bot_updates.py b/telegram_bot/bot_updates.py index ff76299..22ba398 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -6,18 +6,12 @@ __date__ = "26.04.2022" __version__ = "1.0.2" __license__ = "None" -from asyncio.windows_events import NULL -from calendar import month # unused, remove? -from symtable import Symbol # unused, remove? from dotenv import load_dotenv -from shares.share_fetcher import get_share_price # unused, remove? import news.news_fetcher as news_fetcher import time -import datetime # unused, remove? import os from bot import bot import sys -from multiprocessing import Process # unused, remove? from apscheduler.schedulers.background import BackgroundScheduler from api_handling.api_handler import API_Handler From e96bac3865d75bc6fb9a37a14943a5e8c548024c Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:49:19 +0200 Subject: [PATCH 11/13] removed unused imports --- telegram_bot/shares/share_fetcher.py | 1 - 1 file changed, 1 deletion(-) diff --git a/telegram_bot/shares/share_fetcher.py b/telegram_bot/shares/share_fetcher.py index 1aa3376..c7ec63f 100644 --- a/telegram_bot/shares/share_fetcher.py +++ b/telegram_bot/shares/share_fetcher.py @@ -7,7 +7,6 @@ __version__ = "0.0.2" __license__ = "None" import yfinance -import json def get_share_price(str_symbol): From fffc6a966b877a02db922d6252880750bf3255a1 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 26 Apr 2022 12:54:22 +0200 Subject: [PATCH 12/13] small comment --- 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 1c5e92e..346a610 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -280,7 +280,7 @@ def send_share_update(message): def send_share_price(message): str_share_price = share_fetcher.get_share_price(str(message.text)) - bot.reply_to(message, str_share_price) + bot.reply_to(message, str_share_price) # add dollar symbol etc. @bot.message_handler(commands=['allnews', 'Allnews']) # /allnews -> get all news From 050a1326e9058fd46bdcf9a99924c4fbc33cdbe1 Mon Sep 17 00:00:00 2001 From: Florian Kellermann Date: Tue, 26 Apr 2022 16:32:45 +0200 Subject: [PATCH 13/13] Link mistake fixed --- 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 46df917..3bca738 100644 --- a/telegram_bot/bot_updates.py +++ b/telegram_bot/bot_updates.py @@ -146,7 +146,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.", pUser_id=p_user_id) + 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) keywords = p_my_handler.get_user_keywords(p_user_id) # get keywords as array