|
| 1 | +import requests |
| 2 | +import time |
| 3 | +from os import environ |
| 4 | +from dotenv import load_dotenv |
| 5 | + |
| 6 | +load_dotenv('config.env', override=True) |
| 7 | + |
| 8 | +STOCK_NAME = "TSLA" |
| 9 | +STOCK_ENDPOINT = "https://www.alphavantage.co/query" |
| 10 | +NEWS_ENDPOINT = "https://newsapi.org/v2/everything" |
| 11 | +STOCK_API = environ.get("ALPHA_VANTAGE_API_KEY") |
| 12 | +NEWS_API_KEY = environ.get("NEWS_API_KEY") |
| 13 | +BOT_TOKEN = environ.get("BOT_TOKEN") |
| 14 | +CHAT_ID = environ.get("CHAT_ID") |
| 15 | + |
| 16 | +def telegram_bot_send_text(bot_message): |
| 17 | + bot_token = BOT_TOKEN |
| 18 | + bot_chatID = CHAT_ID |
| 19 | + send_text = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={bot_chatID}&parse_mode=Markdown&text={bot_message}' |
| 20 | + bot_response = requests.get(send_text) |
| 21 | + return bot_response.json() |
| 22 | + |
| 23 | +def get_change(current, previous): |
| 24 | + if current == previous: |
| 25 | + return 100.0 |
| 26 | + try: |
| 27 | + return round(abs(current - previous) / previous, 5) * 100.0 |
| 28 | + except ZeroDivisionError: |
| 29 | + return 0 |
| 30 | + |
| 31 | +while True: |
| 32 | + alpha_vantage_parameters = { |
| 33 | + "function": "TIME_SERIES_DAILY_ADJUSTED", |
| 34 | + "symbol": STOCK_NAME, |
| 35 | + "apikey": STOCK_API, |
| 36 | + } |
| 37 | + |
| 38 | + news_api_parameters = { |
| 39 | + "q": f"{STOCK_NAME} Market Update", |
| 40 | + "pageSize": 3, |
| 41 | + "apiKey": NEWS_API_KEY, |
| 42 | + } |
| 43 | + |
| 44 | + alpha_vantage_response = requests.get(STOCK_ENDPOINT, params=alpha_vantage_parameters) |
| 45 | + alpha_vantage_response.raise_for_status() |
| 46 | + stock_data = alpha_vantage_response.json() |
| 47 | + |
| 48 | + news_api_response = requests.get(NEWS_ENDPOINT, params=news_api_parameters) |
| 49 | + news_api_response.raise_for_status() |
| 50 | + news_data = news_api_response.json() |
| 51 | + |
| 52 | + closing_prices = [ |
| 53 | + stock_data['Time Series (Daily)'][date]['4. close'] |
| 54 | + for _, date in zip(range(3), stock_data['Time Series (Daily)']) |
| 55 | + ] |
| 56 | + |
| 57 | + yd_price = float(closing_prices[0]) |
| 58 | + dyd_price = float(closing_prices[1]) |
| 59 | + |
| 60 | + difference = yd_price - dyd_price |
| 61 | + up_down = None |
| 62 | + up_down = "🔺" if difference > 0 else "🔻" |
| 63 | + |
| 64 | + if get_change(yd_price, dyd_price) >= 5: |
| 65 | + for article in range(3): |
| 66 | + market_performance = [ |
| 67 | + f"*{STOCK_NAME}:* {up_down} `{get_change(yd_price, dyd_price)}%`" |
| 68 | + ] |
| 69 | + news_headlines = [f"*Headline:* {news_data['articles'][article]['title']}"] |
| 70 | + news_description = [ |
| 71 | + f"*Description:* `{news_data['articles'][article]['description']}`" |
| 72 | + ] |
| 73 | + news = (market_performance + news_headlines + news_description) |
| 74 | + joined_string = "\n".join(news) |
| 75 | + telegram_bot_send_text(joined_string) |
| 76 | + time.sleep(3600) # Wait for 1 hour before running the bot again |
0 commit comments