mirror of
https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git
synced 2025-06-16 15:06:53 +00:00
Merge branch 'Features' into 'master'
Features See merge request MisterBiggs/simple-telegram-bot!2
This commit is contained in:
commit
fefc631b8d
98
stockBot.py
98
stockBot.py
@ -18,6 +18,7 @@ logging.basicConfig(
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
print("Bot Online")
|
||||
|
||||
|
||||
# Define a few command handlers. These usually take the two arguments bot and
|
||||
@ -32,52 +33,84 @@ def help(bot, update):
|
||||
update.message.reply_text("I don't know how to help yet!")
|
||||
|
||||
|
||||
def stockInfo(bot, update):
|
||||
def news(bot, update):
|
||||
"""Send a message when the /news command is issued."""
|
||||
message = update.message.text
|
||||
chat_id = update.message.chat_id
|
||||
|
||||
try:
|
||||
# regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters
|
||||
tickers = re.findall("[$](\w{1,4})", message)
|
||||
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
|
||||
# Checks if !news is called, and prints news embed if it is
|
||||
if message.startswith("!news"):
|
||||
## Checks if a ticker was passed in
|
||||
if tickers == []:
|
||||
update.message.reply_text(
|
||||
"Please type a ticker after your command with a dollar sign: /news $tsla"
|
||||
)
|
||||
else:
|
||||
tickerData = tickerInfo.tickerQuote(tickers)
|
||||
for ticker in tickers:
|
||||
ticker = ticker.upper()
|
||||
# bot.send_photo(bot.get_updates()
|
||||
# [-1].message.chat_id, tickerData[ticker + 'Image'])
|
||||
name = tickerData[ticker + "Name"]
|
||||
price = tickerData[ticker + "Price"]
|
||||
change = tickerData[ticker + "Change"]
|
||||
#Makes sure ticker exists
|
||||
if tickerData[ticker] == 1:
|
||||
name = tickerData[ticker + "Name"]
|
||||
price = tickerData[ticker + "Price"]
|
||||
change = tickerData[ticker + "Change"]
|
||||
|
||||
message = (
|
||||
"The current stock price of " + name + " is $**" + str(price) + "**"
|
||||
)
|
||||
if change > 0:
|
||||
message = (
|
||||
message + ", the stock is currently **up " + str(change) + "%**"
|
||||
"The current stock price of "
|
||||
+ name
|
||||
+ " is $**"
|
||||
+ str(price)
|
||||
+ "**"
|
||||
)
|
||||
elif change < 0:
|
||||
message = (
|
||||
message
|
||||
+ ", the stock is currently **down"
|
||||
+ str(change)
|
||||
+ "%**"
|
||||
if change > 0:
|
||||
message = (
|
||||
message
|
||||
+ ", the stock is currently **up "
|
||||
+ str(change)
|
||||
+ "%**"
|
||||
)
|
||||
elif change < 0:
|
||||
message = (
|
||||
message
|
||||
+ ", the stock is currently **down"
|
||||
+ str(change)
|
||||
+ "%**"
|
||||
)
|
||||
else:
|
||||
message = (
|
||||
message + ", the stock hasn't shown any movement today."
|
||||
)
|
||||
|
||||
news = tickerInfo.stockNewsList(ticker)
|
||||
for source in news:
|
||||
message = message + "\n[" + source + "](" + news[source] + ")"
|
||||
|
||||
update.message.reply_text(
|
||||
text=message, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
else:
|
||||
message = message + ", the stock hasn't shown any movement today."
|
||||
update.message.reply_text(ticker + " Does not exist.")
|
||||
except:
|
||||
pass
|
||||
|
||||
news = tickerInfo.stockNewsList(ticker)
|
||||
for source in news:
|
||||
message = message + "\n[" + source + "](" + news[source] + ")"
|
||||
|
||||
update.message.reply_text(
|
||||
text=message, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
def stockInfo(bot, update):
|
||||
message = update.message.text
|
||||
chat_id = update.message.chat_id
|
||||
|
||||
else: # If news isnt called, print normal stock price
|
||||
tickerData = tickerInfo.tickerQuote(tickers)
|
||||
for ticker in tickers:
|
||||
ticker = ticker.upper()
|
||||
try:
|
||||
# regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters
|
||||
tickers = re.findall("[$](\w{1,4})", message)
|
||||
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
|
||||
tickerData = tickerInfo.tickerQuote(tickers)
|
||||
for ticker in tickers:
|
||||
ticker = ticker.upper()
|
||||
#Makes sure ticker exists
|
||||
if tickerData[ticker] == 1:
|
||||
name = tickerData[ticker + "Name"]
|
||||
price = tickerData[ticker + "Price"]
|
||||
change = tickerData[ticker + "Change"]
|
||||
@ -96,10 +129,12 @@ def stockInfo(bot, update):
|
||||
+ "%**"
|
||||
)
|
||||
else:
|
||||
move = ", the stock hasn't shown any movement today."
|
||||
message = message + ", the stock hasn't shown any movement today."
|
||||
update.message.reply_text(
|
||||
text=message, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
else:
|
||||
update.message.reply_text(ticker + " Does not exist.")
|
||||
except:
|
||||
pass
|
||||
|
||||
@ -120,6 +155,7 @@ def main():
|
||||
# on different commands - answer in Telegram
|
||||
dp.add_handler(CommandHandler("start", start))
|
||||
dp.add_handler(CommandHandler("help", help))
|
||||
dp.add_handler(CommandHandler("news", news))
|
||||
|
||||
# on noncommand i.e message - echo the message on Telegram
|
||||
dp.add_handler(MessageHandler(Filters.text, stockInfo))
|
||||
|
@ -3,6 +3,7 @@ import json
|
||||
|
||||
|
||||
def tickerQuote(tickers):
|
||||
"""Gathers information from IEX api on stock"""
|
||||
stockData = {}
|
||||
IEXURL = (
|
||||
"https://api.iextrading.com/1.0/stock/market/batch?symbols="
|
||||
@ -15,15 +16,24 @@ def tickerQuote(tickers):
|
||||
|
||||
for ticker in tickers:
|
||||
ticker = ticker.upper()
|
||||
stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"]
|
||||
stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"]
|
||||
stockData[ticker + "Change"] = IEXData[ticker]["quote"]["changePercent"] * 100
|
||||
stockData[ticker + "Image"] = stockLogo(ticker)
|
||||
print("Quote Gathered")
|
||||
|
||||
# Makes sure ticker exists before populating a dictionary
|
||||
if ticker in IEXData:
|
||||
stockData[ticker] = 1
|
||||
stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"]
|
||||
stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"]
|
||||
stockData[ticker + "Change"] = round(
|
||||
(IEXData[ticker]["quote"]["changePercent"] * 100), 2
|
||||
)
|
||||
stockData[ticker + "Image"] = stockLogo(ticker)
|
||||
print(ticker + " Quote Gathered")
|
||||
else:
|
||||
stockData[ticker] = 0
|
||||
return stockData
|
||||
|
||||
|
||||
def stockNewsList(ticker):
|
||||
"""Makes a bunch of strings that are links to news websites for an input ticker"""
|
||||
print("Gather News on " + ticker)
|
||||
news = {
|
||||
"Bravos": "https://bravos.co/" + ticker,
|
||||
@ -39,5 +49,6 @@ def stockNewsList(ticker):
|
||||
|
||||
|
||||
def stockLogo(ticker):
|
||||
"""returns a png of an input ticker"""
|
||||
logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png"
|
||||
return logoURL
|
||||
|
Loading…
x
Reference in New Issue
Block a user