fixed all markdown issues

This commit is contained in:
Linus E 2022-05-10 18:22:03 +02:00
parent 7b323595f3
commit a3172cc9c5
2 changed files with 29 additions and 15 deletions

View File

@ -23,6 +23,7 @@ import re
import news.news_fetcher as news
import shares.share_fetcher as share_fetcher
import helper_functions as hf
import datetime as dt
from telebot import types
@ -83,7 +84,7 @@ def send_help(message):
: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='MARKDOWN')
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.message_handler(commands=['users', 'Users']) # /users -> sending all users
@ -304,7 +305,7 @@ def send_all_news(message):
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text='You have no keywords. Please add some keywords with /news')
bot.send_message(chat_id=user_id, text='You have no keywords. Please add some keywords with /addkeyword')
return
keywords_search = ' OR '.join(keywords) # concat all keywords with OR -> NewsAPI can understand OR, AND, NOT etc.
@ -316,7 +317,7 @@ def send_all_news(message):
if news_list: # true if news_list is not empty
for article in news_list:
formatted_article = news.format_article(article)
bot.send_message(chat_id=user_id, text=formatted_article, parse_mode="MARKDOWN") # Markdown allows to write bold text with * etc.
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.
else:
bot.send_message(chat_id=user_id, text='No news found for your keywords.')
@ -350,11 +351,13 @@ def send_news(message):
bot.send_message(chat_id=user_id, text='News Server did not respond correctly. Try again later.')
if not top_news: # true if no news found for keyword (empty list)
bot.send_message(chat_id=user_id, text=f'No news found for keyword: *{keyword}*', parse_mode="MARKDOWN")
keyword = hf.make_markdown_proof(keyword)
bot.send_message(chat_id=user_id, text=f'No news found for keyword: *{keyword}*', parse_mode="MARKDOWNV2")
else:
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")
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
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.message_handler(commands=['addkeyword', 'Addkeyword']) # /addkeyword -> add keyword to user
@ -417,15 +420,21 @@ def send_keywords(message):
"""
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
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: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text='No keywords set for this account. Add keywords by using /addkeyword')
return
else: # send keyword list
keywords_str = ', '.join(keywords)
bot.send_message(chat_id=user_id, text=f'Your keywords are: _{keywords_str}_', parse_mode="MARKDOWN")
keywords_str = hf.make_markdown_proof(keywords_str)
text = f'Your keywords are: _{keywords_str}_'
bot.send_message(chat_id=user_id, text=text, parse_mode="MARKDOWNV2")
@bot.message_handler(commands=['portfolio', 'Portfolio'])
@ -448,11 +457,11 @@ def send_portfolio(message):
return
else: # send portfolio
for stock in portfolio:
comment = 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
isin = 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
bot.send_message(chat_id=user_id, text=f'*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}', parse_mode="MARKDOWN") # 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
@bot.message_handler(commands=['removeshare', 'Removeshare'])
@ -568,8 +577,11 @@ def send_transactions(message):
return
else:
for transaction in transactions:
bot.send_message(chat_id=user_id, text=f'_{transaction["comment"]}_\n{transaction["isin"]}\namount: {transaction["count"]}\nprice: {transaction["price"]}\ntime: {transaction["time"]}', parse_mode="MARKDOWN")
comment = hf.make_markdown_proof(transaction['comment'])
isin = hf.make_markdown_proof(transaction['isin'])
bot.send_message(chat_id=user_id, text=f'_{comment}_\n{isin}\namount: {transaction["count"]}\nprice: {transaction["price"]}\ntime: {transaction["time"]}', parse_mode="MARKDOWNV2")
@bot.message_handler(commands=['shares', 'Shares'])

View File

@ -16,13 +16,13 @@ def contains_markdown_symbols(text):
:rtype: bool
"""
if text.find("_") != -1 or text.find("*") != -1 or text.find("~") != -1 or text.find("`") != -1: # check if text contains markdown symbols
if text.find("_") != -1 or text.find("*") != -1 or text.find("`") != -1: # check if text contains relevant markdown symbols
return True
return False
def make_markdown_proof(text):
def make_markdown_proof(text): # used to avoid errors related to markdown parsemode for telegram messaging
""" makes text markdown proof
:type text: string
@ -37,11 +37,13 @@ def make_markdown_proof(text):
text = text.replace("_", "\\_")
text = text.replace("*", "\\*")
text = text.replace("~", "\\~")
text = text.replace("`", "\\`")
return text
if __name__ == '__main__':
print("this is a module for helper functions for the bot and should not be run directly")
print("this is a module for helper functions for the bot and should not be run directly")
print(make_markdown_proof("_test_"))
text = make_markdown_proof("_test_")
print(f"{text}")