mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 15:17:28 +00:00
updated standard reply for tickers in text
This commit is contained in:
parent
bfca21f176
commit
3113c17efb
23
bot.py
23
bot.py
@ -38,30 +38,17 @@ def tickerDetect(bot, update):
|
||||
"""
|
||||
message = update.message.text
|
||||
chat_id = update.message.chat_id
|
||||
|
||||
# Let user know bot is working
|
||||
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
|
||||
tickers = getTickers(message)
|
||||
|
||||
data = tickerData(tickers) if tickers else {}
|
||||
# Let user know bot is working
|
||||
if tickers:
|
||||
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
replies = tickerDataReply(tickers)
|
||||
|
||||
for ticker in data:
|
||||
for symbol, reply in replies.items():
|
||||
|
||||
# Keep track of which tickers had a return from tickerData()
|
||||
if ticker.lower() in tickers:
|
||||
tickers.remove(ticker.lower())
|
||||
|
||||
reply = tickerDataReply(data[ticker])
|
||||
update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN)
|
||||
|
||||
# For any tickers that didnt have data, return that they don't exist.
|
||||
for ticker in tickers:
|
||||
update.message.reply_text(
|
||||
ticker.upper()
|
||||
+ " does not exist, you should search for a real stock like $PSEC"
|
||||
)
|
||||
|
||||
|
||||
def news(bot, update):
|
||||
"""
|
||||
|
56
functions.py
56
functions.py
@ -4,6 +4,8 @@ import time
|
||||
import urllib.request
|
||||
from datetime import datetime
|
||||
|
||||
import cred
|
||||
|
||||
|
||||
def getTickers(text: str):
|
||||
"""
|
||||
@ -15,60 +17,32 @@ def getTickers(text: str):
|
||||
return list(set(re.findall(TICKER_REGEX, text)))
|
||||
|
||||
|
||||
def tickerData(tickers: list):
|
||||
def tickerDataReply(tickers: list):
|
||||
"""
|
||||
Takes a list of tickers and returns a dictionary of information on ticker.
|
||||
example:
|
||||
input list: ["aapl","tsla"]
|
||||
returns:
|
||||
{
|
||||
"AAPL": {"name": "Apple Inc.", "price": 200.72, "change": -0.01074},
|
||||
"TSLA": {"name": "Tesla Inc.", "price": 241.98, "change": -0.01168},
|
||||
}
|
||||
Takes a list of tickers and returns a list of strings with information about the ticker.
|
||||
"""
|
||||
|
||||
stockData = {}
|
||||
tickerReplies = {}
|
||||
for ticker in tickers:
|
||||
IEXURL = (
|
||||
"https://api.iextrading.com/1.0/stock/market/batch?symbols="
|
||||
+ ",".join(tickers)
|
||||
+ "&types=quote"
|
||||
f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={cred.secret}"
|
||||
)
|
||||
with urllib.request.urlopen(IEXURL) as url:
|
||||
IEXData = json.loads(url.read().decode())
|
||||
|
||||
for ticker in tickers:
|
||||
ticker = ticker.upper()
|
||||
|
||||
# Makes sure ticker exists before populating a dictionary
|
||||
if ticker in IEXData:
|
||||
stockData[ticker] = {}
|
||||
stockData[ticker]["name"] = IEXData[ticker]["quote"]["companyName"]
|
||||
stockData[ticker]["price"] = IEXData[ticker]["quote"]["latestPrice"]
|
||||
stockData[ticker]["change"] = IEXData[ticker]["quote"]["changePercent"]
|
||||
|
||||
return stockData
|
||||
|
||||
|
||||
def tickerDataReply(ticker: dict):
|
||||
"""
|
||||
Takes a dictionary, likely produced from tickerData(), and returns a markdown string with information on the ticker.
|
||||
example:
|
||||
input dict: {"AAPL": {"name": "Apple Inc.", "price": 200.72, "change": -0.01074}}
|
||||
returns:
|
||||
The current stock price of Apple Inc. is $**200.72**, the stock is currently **down -1.07**%
|
||||
"""
|
||||
reply = f"The current stock price of {ticker['name']} is $**{ticker['price']}**"
|
||||
reply = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**"
|
||||
|
||||
# Determine wording of change text
|
||||
change = round(ticker["change"] * 100, 2)
|
||||
change = round(IEXData["changePercent"] * 100, 2)
|
||||
if change > 0:
|
||||
changeText = f", the stock is currently **up {change}%**"
|
||||
reply += f", the stock is currently **up {change}%**"
|
||||
elif change < 0:
|
||||
changeText = f", the stock is currently **down {change}%**"
|
||||
reply += f", the stock is currently **down {change}%**"
|
||||
else:
|
||||
changeText = ", the stock hasn't shown any movement today."
|
||||
reply += ", the stock hasn't shown any movement today."
|
||||
|
||||
return reply + changeText
|
||||
tickerReplies[ticker] = reply
|
||||
|
||||
return tickerReplies
|
||||
|
||||
|
||||
def tickerNews(tickers: list):
|
||||
|
Loading…
x
Reference in New Issue
Block a user