fixed errors

This commit is contained in:
Linus E 2022-03-15 14:24:11 +01:00
parent 38f004d705
commit 05d2b37482
2 changed files with 10 additions and 7 deletions

View File

@ -37,7 +37,6 @@ class User: # Currently saving users in this class to test functionality -> late
self.chat_id = int(p_chat_id)
self.user_name = str(p_user_name)
print(os.getenv('BOT_API_KEY'))
bot = telebot.TeleBot(os.getenv('BOT_API_KEY'))
@bot.message_handler(commands=['start']) # /start -> saving as new user and sending welcome
@ -89,10 +88,16 @@ def send_update(message):
@bot.message_handler(commands=['news']) # /news -> send news by keyword
def send_news(message):
keyword = "business"
user_id = int(message.from_user.id)
#Get Information for user with this id
articles = news_fetcher.get_top_news_by_keyword("bitcoin") #tbd: get keyword from db
formatted_article = news_fetcher.format_article(articles["articles"][0])
articles = news_fetcher.get_top_news_by_keyword(keyword) #tbd: get keyword from db
try:
formatted_article = news_fetcher.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=formatted_article)

View File

@ -31,15 +31,13 @@ sources = newsapi.get_sources()
def get_top_news_by_keyword(keyword):
top_headlines = newsapi.get_top_headlines(q=keyword, sources='bbc-news,the-verge,cnn', language='en')
out_file = open("top_headline.json", "w")
json.dump(top_headlines, out_file)
return top_headlines
def format_article(article):
sourcename = article["source"]["name"]
headline = article["title"]
url = article["url"]
formatted_article = f"<i>{sourcename}</i>\n{headline}\n\n<a href=\"{url}\">text</a>"
formatted_article = f"_{sourcename}_\n*{headline}*\n\n{url}"
return formatted_article