mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 15:17:28 +00:00
Merge branch 'Features' into 'master'
Fixed news, added dividend functionality See merge request MisterBiggs/simple-telegram-bot!6
This commit is contained in:
commit
8d61cf33df
@ -29,8 +29,9 @@ def start(bot, update):
|
||||
|
||||
|
||||
def help(bot, update):
|
||||
"""Send a message when the command /help is issued."""
|
||||
update.message.reply_text("I don't know how to help yet!")
|
||||
"""Send link to docs when the command /help is issued."""
|
||||
message = "[Please see the docs for Bot information](https://misterbiggs.gitlab.io/simple-telegram-bot)"
|
||||
update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN)
|
||||
|
||||
|
||||
def news(bot, update):
|
||||
@ -45,8 +46,14 @@ def news(bot, update):
|
||||
|
||||
## Checks if a ticker was passed in
|
||||
if tickers == []:
|
||||
message = "No Ticker, showing Market News:"
|
||||
news = tickerInfo.stockNews("market")
|
||||
for i in range(3):
|
||||
message = "{}\n\n[{}]({})".format(
|
||||
message, news["title"][i], news["link"][i]
|
||||
)
|
||||
update.message.reply_text(
|
||||
"Please type a ticker after your command with a dollar sign: /news $tsla"
|
||||
text=message, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
else:
|
||||
tickerData = tickerInfo.tickerQuote(tickers)
|
||||
@ -84,9 +91,11 @@ def news(bot, update):
|
||||
message + ", the stock hasn't shown any movement today."
|
||||
)
|
||||
|
||||
news = tickerInfo.stockNewsList(ticker)
|
||||
for source in news:
|
||||
message = message + "\n[" + source + "](" + news[source] + ")"
|
||||
news = tickerInfo.stockNews(ticker)
|
||||
for i in range(3):
|
||||
message = "{}\n\n[{}]({})".format(
|
||||
message, news["title"][i], news["link"][i]
|
||||
)
|
||||
|
||||
update.message.reply_text(
|
||||
text=message, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
@ -139,6 +148,25 @@ def stockInfo(bot, update):
|
||||
pass
|
||||
|
||||
|
||||
def dividend(bot, update):
|
||||
message = update.message.text
|
||||
chat_id = update.message.chat_id
|
||||
print("div")
|
||||
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)
|
||||
|
||||
for ticker in tickers:
|
||||
message = tickerInfo.stockDividend(ticker)
|
||||
update.message.reply_text(
|
||||
text=message, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def error(bot, update, error):
|
||||
"""Log Errors caused by Updates."""
|
||||
logger.warning('Update "%s" caused error "%s"', update, error)
|
||||
@ -156,6 +184,7 @@ def main():
|
||||
dp.add_handler(CommandHandler("start", start))
|
||||
dp.add_handler(CommandHandler("help", help))
|
||||
dp.add_handler(CommandHandler("news", news))
|
||||
dp.add_handler(CommandHandler("dividend", dividend))
|
||||
|
||||
# on noncommand i.e message - echo the message on Telegram
|
||||
dp.add_handler(MessageHandler(Filters.text, stockInfo))
|
||||
|
@ -1,5 +1,8 @@
|
||||
import urllib.request
|
||||
import json
|
||||
import feedparser
|
||||
from datetime import datetime
|
||||
import time
|
||||
|
||||
|
||||
def tickerQuote(tickers):
|
||||
@ -32,19 +35,19 @@ def tickerQuote(tickers):
|
||||
return stockData
|
||||
|
||||
|
||||
def stockNewsList(ticker):
|
||||
def stockNews(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,
|
||||
"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,
|
||||
}
|
||||
print("News gathered.")
|
||||
|
||||
newsLink = "https://api.iextrading.com/1.0/stock/{}/news/last/5".format(ticker)
|
||||
|
||||
with urllib.request.urlopen(newsLink) as url:
|
||||
data = json.loads(url.read().decode())
|
||||
|
||||
news = {"link": [], "title": []}
|
||||
for i in range(3):
|
||||
news["link"].append(data[i]["url"])
|
||||
news["title"].append(data[i]["headline"])
|
||||
return news
|
||||
|
||||
|
||||
@ -52,3 +55,54 @@ def stockLogo(ticker):
|
||||
"""returns a png of an input ticker"""
|
||||
logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png"
|
||||
return logoURL
|
||||
|
||||
|
||||
def stockInfo(ticker):
|
||||
infoURL = "https://api.iextrading.com/1.0/stock/{}/stats".format(ticker)
|
||||
|
||||
with urllib.request.urlopen(infoURL) as url:
|
||||
data = json.loads(url.read().decode())
|
||||
|
||||
info = {}
|
||||
|
||||
info["companyName"] = data["companyName"]
|
||||
info["marketCap"] = data["marketcap"]
|
||||
info["yearHigh"] = data["week52high"]
|
||||
info["yearLow"] = data["week52low"]
|
||||
info["divRate"] = data["dividendRate"]
|
||||
info["divYield"] = data["dividendYield"]
|
||||
info["divDate"] = data["exDividendDate"]
|
||||
|
||||
return info
|
||||
|
||||
|
||||
def stockDividend(ticker):
|
||||
data = stockInfo(ticker)
|
||||
print(data["divDate"])
|
||||
if data["divDate"] == 0:
|
||||
return "{} has no dividend.".format(data["companyName"])
|
||||
|
||||
line1 = "{} current dividend yield is: {:.3f}%, or ${:.3f} per share.".format(
|
||||
data["companyName"], data["divRate"], data["divYield"]
|
||||
)
|
||||
|
||||
divDate = data["divDate"]
|
||||
|
||||
# Pattern IEX uses for dividend date.
|
||||
pattern = "%Y-%m-%d %H:%M:%S.%f"
|
||||
|
||||
# Convert divDate to seconds, and subtract it from current time.
|
||||
divSeconds = datetime.strptime(divDate, pattern).timestamp()
|
||||
difference = divSeconds - int(time.time())
|
||||
|
||||
# Calculate (d)ays, (h)ours, (m)inutes, and (s)econds
|
||||
d, h = divmod(difference, 86400)
|
||||
h, m = divmod(h, 3600)
|
||||
m, s = divmod(m, 60)
|
||||
|
||||
countdownMessage = "\n\nThe dividend is in: {:.0f} Days {:.0f} Hours {:.0f} Minutes {:.0f} Seconds.".format(
|
||||
d, h, m, s
|
||||
)
|
||||
|
||||
message = line1 + countdownMessage
|
||||
return message
|
||||
|
Loading…
x
Reference in New Issue
Block a user