fixed errors

This commit is contained in:
Linus E 2022-05-10 18:43:41 +02:00
parent a3172cc9c5
commit 9324573cde
3 changed files with 33 additions and 12 deletions

View File

@ -84,7 +84,7 @@ def send_help(message):
:rtype: none :rtype: none
""" """
bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/shares get update on interesting shares\n/setAdmin set admin rights of user (ADMIN)\n/users see all users. (ADMIN)\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/transactions get all transactions\n/newtransaction create new transaction\n/share get price of specific share\n/portfolio see own portfolio\n/removeshare removes share from portfolio\n/interval get update interval\n/setinterval set update interval\n_For further details see https://gruppe1.testsites.info _", parse_mode='MARKDOWNV2') bot.reply_to(message, "/id or /auth get your user id\n/update get updates on your shares.\n/shares get update on interesting shares\n/setAdmin set admin rights of user (ADMIN)\n/users see all users. (ADMIN)\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/transactions get all transactions\n/newtransaction create new transaction\n/share get price of specific share\n/portfolio see own portfolio\n/removeshare removes share from portfolio\n/interval get update interval\n/setinterval set update interval\n For further details see https://gruppe1.testsites.info")
@bot.message_handler(commands=['users', 'Users']) # /users -> sending all users @bot.message_handler(commands=['users', 'Users']) # /users -> sending all users
@ -317,7 +317,7 @@ def send_all_news(message):
if news_list: # true if news_list is not empty if news_list: # true if news_list is not empty
for article in news_list: for article in news_list:
formatted_article = news.format_article(article) formatted_article = news.format_article(article)
bot.send_message(chat_id=user_id, text=hf.make_markdown_proof(formatted_article), parse_mode="MARKDOWN") # Markdown allows to write bold text with * etc. bot.send_message(chat_id=user_id, text=formatted_article, parse_mode="MARKDOWNV2") # Markdown allows to write bold text with * etc.
else: else:
bot.send_message(chat_id=user_id, text='No news found for your keywords.') bot.send_message(chat_id=user_id, text='No news found for your keywords.')
@ -356,8 +356,8 @@ def send_news(message):
else: else:
keyword = hf.make_markdown_proof(keyword) keyword = hf.make_markdown_proof(keyword)
formatted_article = hf.make_markdown_proof(news.format_article(top_news[0])) # only format and send most popular news formatted_article = news.format_article(top_news[0]) # only format and send most popular news
bot.send_message(chat_id=user_id, text=f"_keyword: {keyword}_\n\n" + formatted_article, parse_mode="MARKDOWN") # do not use v2 because of bugs related t "." in links bot.send_message(chat_id=user_id, text=f"_keyword: {keyword}_\n\n" + formatted_article, parse_mode="MARKDOWNV2") # do not use v2 because of bugs related t "." in links
@bot.message_handler(commands=['addkeyword', 'Addkeyword']) # /addkeyword -> add keyword to user @bot.message_handler(commands=['addkeyword', 'Addkeyword']) # /addkeyword -> add keyword to user
@ -458,9 +458,9 @@ def send_portfolio(message):
else: # send portfolio else: # send portfolio
for stock in portfolio: for stock in portfolio:
comment = hf.make_markdown_proof(str(stock["comment"])) # comment may be written name of stock, comment is made by user when adding an stock to portfolio comment = hf.make_markdown_proof(str(stock["comment"])) # comment may be written name of stock, comment is made by user when adding an stock to portfolio
count = "{:.2f}".format(float(stock["count"])) # round count to 2 decimal places count = hf.make_markdown_proof("{:.2f}".format(float(stock["count"]))) # round count to 2 decimal places
isin = hf.make_markdown_proof(str(stock["isin"])) isin = hf.make_markdown_proof(str(stock["isin"]))
worth = "{:.2f}".format(float(stock["current_price"]) * float(stock["count"])) # round current_price to 2 decimal places worth = hf.make_markdown_proof("{:.2f}".format(float(stock["current_price"]) * float(stock["count"]))) # round current_price to 2 decimal places
bot.send_message(chat_id=user_id, text=f'*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}', parse_mode="MARKDOWNV2") # formatted message in markdown bot.send_message(chat_id=user_id, text=f'*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}', parse_mode="MARKDOWNV2") # formatted message in markdown

View File

@ -6,7 +6,7 @@ __date__ = "10.05.2022"
__version__ = "1.0.0" __version__ = "1.0.0"
__license__ = "None" __license__ = "None"
def contains_markdown_symbols(text): def contains_markdownv1_symbols(text):
""" checks if text contains markdown symbols """ checks if text contains markdown symbols
:type text: string :type text: string
@ -32,12 +32,30 @@ def make_markdown_proof(text): # used to avoid errors related to markdown parsem
:rtype: string :rtype: string
""" """
if not contains_markdown_symbols(text):
return text
text = text.replace("_", "\\_") text = text.replace("_", "\\_")
text = text.replace("*", "\\*") text = text.replace("*", "\\*")
text = text.replace("`", "\\`") text = text.replace("`", "\\`")
text = text.replace("[", "\\[")
text = text.replace("]", "\\]")
text = text.replace("(", "\\(")
text = text.replace(")", "\\)")
text = text.replace("#", "\\#")
text = text.replace("+", "\\+")
text = text.replace("-", "\\-")
text = text.replace("!", "\\!")
text = text.replace(".", "\\.")
text = text.replace("?", "\\?")
text = text.replace("/", "\\/")
text = text.replace("~", "\\~")
text = text.replace("|", "\\|")
text = text.replace("<", "\\<")
text = text.replace(">", "\\>")
text = text.replace("&", "\\&")
text = text.replace("^", "\\^")
text = text.replace("$", "\\$")
text = text.replace("%", "\\%")
return text return text

View File

@ -10,6 +10,8 @@ import sys
import os import os
import requests import requests
import helper_functions as hf
from newsapi import NewsApiClient from newsapi import NewsApiClient
from dotenv import load_dotenv from dotenv import load_dotenv
@ -68,13 +70,14 @@ def format_article(article):
Returns: Returns:
String: formatted article String: formatted article
""" """
sourcename = article["source"]["name"] sourcename = hf.make_markdown_proof(article["source"]["name"]) # make attributes markdownv2 proof
headline = article["title"] headline = hf.make_markdown_proof(article["title"])
url = article["url"] url = hf.make_markdown_proof(article["url"])
formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" # formatting in Markdown syntax formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" # formatting in Markdown syntax
return formatted_article return formatted_article
if __name__ == '__main__': # only execute if script is called directly -> for simple testing if __name__ == '__main__': # only execute if script is called directly -> for simple testing
print("this is a module and should not be run directly") print("this is a module and should not be run directly")