From 9324573cde14aeddb5fb11980a1e1f081e192d89 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 10 May 2022 18:43:41 +0200 Subject: [PATCH] fixed errors --- telegram_bot/bot.py | 12 ++++++------ telegram_bot/helper_functions.py | 24 +++++++++++++++++++++--- telegram_bot/news/news_fetcher.py | 9 ++++++--- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/telegram_bot/bot.py b/telegram_bot/bot.py index 89e9574..5366f29 100644 --- a/telegram_bot/bot.py +++ b/telegram_bot/bot.py @@ -84,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='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 @@ -317,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=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: bot.send_message(chat_id=user_id, text='No news found for your keywords.') @@ -356,8 +356,8 @@ def send_news(message): else: 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 + 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="MARKDOWNV2") # do not use v2 because of bugs related t "." in links @bot.message_handler(commands=['addkeyword', 'Addkeyword']) # /addkeyword -> add keyword to user @@ -458,9 +458,9 @@ def send_portfolio(message): else: # send 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 - 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"])) - 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 diff --git a/telegram_bot/helper_functions.py b/telegram_bot/helper_functions.py index c69e6dc..92c9d77 100644 --- a/telegram_bot/helper_functions.py +++ b/telegram_bot/helper_functions.py @@ -6,7 +6,7 @@ __date__ = "10.05.2022" __version__ = "1.0.0" __license__ = "None" -def contains_markdown_symbols(text): +def contains_markdownv1_symbols(text): """ checks if text contains markdown symbols :type text: string @@ -32,12 +32,30 @@ def make_markdown_proof(text): # used to avoid errors related to markdown parsem :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("%", "\\%") + return text diff --git a/telegram_bot/news/news_fetcher.py b/telegram_bot/news/news_fetcher.py index bd083b5..d29d0a2 100644 --- a/telegram_bot/news/news_fetcher.py +++ b/telegram_bot/news/news_fetcher.py @@ -10,6 +10,8 @@ import sys import os import requests +import helper_functions as hf + from newsapi import NewsApiClient from dotenv import load_dotenv @@ -68,13 +70,14 @@ def format_article(article): Returns: String: formatted article """ - sourcename = article["source"]["name"] - headline = article["title"] - url = article["url"] + sourcename = hf.make_markdown_proof(article["source"]["name"]) # make attributes markdownv2 proof + headline = hf.make_markdown_proof(article["title"]) + url = hf.make_markdown_proof(article["url"]) formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}" # formatting in Markdown syntax return formatted_article + if __name__ == '__main__': # only execute if script is called directly -> for simple testing print("this is a module and should not be run directly")