1
0
mirror of https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git synced 2025-06-16 15:06:53 +00:00

Added news functionality

This commit is contained in:
Anson 2019-01-10 21:18:35 -07:00
parent 1f62e472df
commit adf677f855

View File

@ -12,6 +12,7 @@ import credentials
# Make sure to update credentials.py with your secrets # Make sure to update credentials.py with your secrets
TOKEN = credentials.secrets['TOKEN'] TOKEN = credentials.secrets['TOKEN']
BRAVOS_API = credentials.secrets['BRAVOS_API'] BRAVOS_API = credentials.secrets['BRAVOS_API']
ALPHA_API = credentials.secrets['ALPHA_API']
"""Simple Bot to reply to Telegram messages. """Simple Bot to reply to Telegram messages.
This program is dedicated to the public domain under the CC0 license. This program is dedicated to the public domain under the CC0 license.
@ -26,6 +27,17 @@ bot.
""" """
news = {
'Bravos': 'https://bravos.co/' + ticker,
'Seeking Alpha': 'https://seekingalpha.com/symbol/' + ticker,
'MSN Money': 'https://www.msn.com/en-us/money/stockdetails?symbol=' + ticker,
'Yahoo Finance': 'https://finance.yahoo.com/quote/' + ticker,
'Wall Street Journal': 'https://quotes.wsj.com/' + ticker,
'The Street': 'https://www.thestreet.com/quote/' + ticker + '.html',
'Zacks': 'https://www.zacks.com/stock/quote/' + ticker
}
# Enable logging # Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO) level=logging.INFO)
@ -60,6 +72,7 @@ def stockInfo(bot, update):
data = json.loads(url.read().decode()) data = json.loads(url.read().decode())
for ticker in tickers: # iterate through the tickers and print relevant info one message at a time for ticker in tickers: # iterate through the tickers and print relevant info one message at a time
try: # checks if data is a valid ticker, if it is not tells the user try: # checks if data is a valid ticker, if it is not tells the user
# Get Stock ticker name from Data Object # Get Stock ticker name from Data Object
@ -69,11 +82,18 @@ def stockInfo(bot, update):
# Checks if !news is called, and prints news embed if it is # Checks if !news is called, and prints news embed if it is
if message.startswith('!news'): if message.startswith('!news'):
newsMessage = 'The current stock price of ' + \
nameTicker + ' is $**' + str(priceTicker) + '**'
for source in news:
print(source)
newsMessage = newsMessage + \
'\n [' + source + '](' + news[source] + ')'
update.message.reply_text( update.message.reply_text(
text='The current stock price of ' + nameTicker + ' is $<b>' + str(priceTicker) + '</b>', parse_mode=telegram.ParseMode.HTML) text=newsMessage, parse_mode=telegram.ParseMode.MARKDOWN)
else: # If news embed isnt called, print normal stock price else: # If news embed isnt called, print normal stock price
update.message.reply_text( update.message.reply_text(
text='The current stock price of ' + nameTicker + ' is $<b>' + str(priceTicker) + '</b>', parse_mode=telegram.ParseMode.HTML) text='The current stock price of ' + nameTicker + ' is $**' + str(priceTicker) + '**', parse_mode=telegram.ParseMode.MARKDOWN)
except KeyError: # If searching for the ticker in loaded data fails, then Bravos didnt provide it, so tell the user. except KeyError: # If searching for the ticker in loaded data fails, then Bravos didnt provide it, so tell the user.
update.message.reply_text( update.message.reply_text(