implemented /news and deleted debugbot (edit BOT_API_KEY instead) #2

Merged
Rripped merged 40 commits from bot-features into main 2022-03-16 09:08:43 +00:00
2 changed files with 10 additions and 7 deletions
Showing only changes of commit 05d2b37482 - Show all commits

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.chat_id = int(p_chat_id)
self.user_name = str(p_user_name) self.user_name = str(p_user_name)
print(os.getenv('BOT_API_KEY'))
bot = telebot.TeleBot(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 @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 @bot.message_handler(commands=['news']) # /news -> send news by keyword
def send_news(message): def send_news(message):
keyword = "business"
user_id = int(message.from_user.id) user_id = int(message.from_user.id)
#Get Information for user with this 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) bot.send_message(chat_id=user_id, text=formatted_article)

View File

@ -31,20 +31,18 @@ sources = newsapi.get_sources()
def get_top_news_by_keyword(keyword): def get_top_news_by_keyword(keyword):
top_headlines = newsapi.get_top_headlines(q=keyword, sources='bbc-news,the-verge,cnn', language='en') 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 return top_headlines
def format_article(article): def format_article(article):
sourcename = article["source"]["name"] sourcename = article["source"]["name"]
headline = article["title"] headline = article["title"]
url = article["url"] 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 return formatted_article
if __name__ == '__main__': if __name__ == '__main__':
print("fetching top news by keyword business...") print("fetching top news by keyword business...")
articles = get_top_news_by_keyword("business") articles = get_top_news_by_keyword("business")