added funcs

This commit is contained in:
Rripped
2022-03-29 10:49:02 +02:00
parent 21d5d357b8
commit 4c517998ba
2 changed files with 34 additions and 258 deletions

View File

@@ -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