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

Merge branch 'code-cleanup' into 'master'

Code cleanup

See merge request simple-stock-bots/simple-telegram-bot!16
This commit is contained in:
Anson Biggs 2019-07-10 20:38:02 +00:00
commit c7634967f6
3 changed files with 101 additions and 99 deletions

51
bot.py
View File

@ -1,4 +1,4 @@
# Work with Python 3.7 # Works with Python 3.7
import logging import logging
import os import os
@ -7,7 +7,6 @@ from functions import *
from telegram.ext import CommandHandler, Filters, MessageHandler, Updater from telegram.ext import CommandHandler, Filters, MessageHandler, Updater
TELEGRAM_TOKEN = os.environ["TELEGRAM"] TELEGRAM_TOKEN = os.environ["TELEGRAM"]
TICKER_REGEX = "[$]([a-zA-Z]{1,4})"
# Enable logging # Enable logging
logging.basicConfig( logging.basicConfig(
@ -27,80 +26,80 @@ def start(bot, update):
def help(bot, update): def help(bot, update):
"""Send link to docs when the command /help is issued.""" """Send link to docs when the command /help is issued."""
message = "[Please see the docs for Bot information](https://simple-stock-bots.gitlab.io/site/telegram/)" message = "[Please see the documentaion for Bot information](https://simple-stock-bots.gitlab.io/site/telegram/)"
update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN)
def tickerDetect(bot, update): def symbolDetect(bot, update):
""" """
Runs on any message that doesn't have a command and searches for tickers, then returns the prices of any tickers found. Runs on any message that doesn't have a command and searches for symbols, then returns the prices of any symbols found.
""" """
message = update.message.text message = update.message.text
chat_id = update.message.chat_id chat_id = update.message.chat_id
tickers = getTickers(message) symbols = getSymbols(message)
if tickers: if symbols:
# Let user know bot is working # Let user know bot is working
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerDataReply(tickers).items(): for reply in symbolDataReply(symbols).items():
update.message.reply_text( update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
) )
def dividend(bot, update): def dividend(bot, update):
""" """
waits for /dividend or /div command and then finds dividend info on that ticker. waits for /dividend or /div command and then finds dividend info on that symbol.
""" """
message = update.message.text message = update.message.text
chat_id = update.message.chat_id chat_id = update.message.chat_id
tickers = getTickers(message) symbols = getSymbols(message)
if tickers: if symbols:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerDividend(tickers).items(): for reply in symbolDividend(symbols).items():
update.message.reply_text( update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
) )
def news(bot, update): def news(bot, update):
""" """
waits for /news command and then finds news info on that ticker. waits for /news command and then finds news info on that symbol.
""" """
message = update.message.text message = update.message.text
chat_id = update.message.chat_id chat_id = update.message.chat_id
tickers = getTickers(message) symbols = getSymbols(message)
if tickers: if symbols:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerNews(tickers).items(): for reply in symbolNews(symbols).items():
update.message.reply_text( update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
) )
def info(bot, update): def info(bot, update):
""" """
waits for /info command and then finds info on that ticker. waits for /info command and then finds info on that symbol.
""" """
message = update.message.text message = update.message.text
chat_id = update.message.chat_id chat_id = update.message.chat_id
tickers = getTickers(message) symbols = getSymbols(message)
if tickers: if symbols:
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
for symbol, reply in tickerInfo(tickers).items(): for reply in symbolInfo(symbols).items():
update.message.reply_text( update.message.reply_text(
text=reply, parse_mode=telegram.ParseMode.MARKDOWN text=reply[1], parse_mode=telegram.ParseMode.MARKDOWN
) )
@ -126,7 +125,7 @@ def main():
dp.add_handler(CommandHandler("info", info)) dp.add_handler(CommandHandler("info", info))
# on noncommand i.e message - echo the message on Telegram # on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, tickerDetect)) dp.add_handler(MessageHandler(Filters.text, symbolDetect))
# log all errors # log all errors
dp.add_error_handler(error) dp.add_error_handler(error)
@ -141,4 +140,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,123 +1,126 @@
import json import json
import os import os
import re import re
import time
import urllib.request
from datetime import datetime from datetime import datetime
import requests
IEX_TOKEN = os.environ["IEX"] IEX_TOKEN = os.environ["IEX"]
def getTickers(text: str): def getSymbols(text: str):
""" """
Takes a blob of text and returns any stock tickers found. Takes a blob of text and returns a list of symbols without any repeats.
""" """
TICKER_REGEX = "[$]([a-zA-Z]{1,4})" SYMBOL_REGEX = "[$]([a-zA-Z]{1,4})"
return list(set(re.findall(TICKER_REGEX, text))) return list(set(re.findall(SYMBOL_REGEX, text)))
def tickerDataReply(tickers: list): def symbolDataReply(symbols: list):
""" """
Takes a list of tickers and returns a list of strings with information about the ticker. Takes a list of symbols and returns a dictionary of strings with information about the symbol.
""" """
tickerReplies = {} dataMessages = {}
for ticker in tickers: for symbol in symbols:
IEXURL = ( IEXurl = (
f"https://cloud.iexapis.com/stable/stock/{ticker}/quote?token={IEX_TOKEN}" f"https://cloud.iexapis.com/stable/stock/{symbol}/quote?token={IEX_TOKEN}"
) )
try:
with urllib.request.urlopen(IEXURL) as url:
IEXData = json.loads(url.read().decode())
reply = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**" response = requests.get(IEXurl)
if response.status_code is 200:
IEXData = response.json()
message = f"The current stock price of {IEXData['companyName']} is $**{IEXData['latestPrice']}**"
# Determine wording of change text # Determine wording of change text
change = round(IEXData["changePercent"] * 100, 2) change = round(IEXData["changePercent"] * 100, 2)
if change > 0: if change > 0:
reply += f", the stock is currently **up {change}%**" message += f", the stock is currently **up {change}%**"
elif change < 0: elif change < 0:
reply += f", the stock is currently **down {change}%**" message += f", the stock is currently **down {change}%**"
else: else:
reply += ", the stock hasn't shown any movement today." message += ", the stock hasn't shown any movement today."
except: else:
reply = f"The ticker: {ticker} was not found." message = f"The symbol: {symbol} was not found."
tickerReplies[ticker] = reply dataMessages[symbol] = message
return tickerReplies return dataMessages
def tickerDividend(tickers: list): def symbolDividend(symbols: list):
messages = {} divMessages = {}
for ticker in tickers: for symbol in symbols:
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/dividends/next?token={IEX_TOKEN}" IEXurl = f"https://cloud.iexapis.com/stable/data-points/{symbol}/NEXTDIVIDENDDATE?token={IEX_TOKEN}"
with urllib.request.urlopen(IEXurl) as url: response = requests.get(IEXurl)
data = json.loads(url.read().decode()) if response.status_code is 200:
if data:
# extract date from json
date = response.json()
# Pattern IEX uses for dividend date. # Pattern IEX uses for dividend date.
pattern = "%Y-%m-%d" pattern = "%Y-%m-%d"
divDate = datetime.strptime(date, pattern)
# Convert divDate to seconds, and subtract it from current time. daysDelta = (divDate - datetime.now()).days
dividendSeconds = datetime.strptime( datePretty = divDate.strftime("%A, %B %w")
data["paymentDate"], pattern if daysDelta < 0:
).timestamp() divMessages[
difference = dividendSeconds - int(time.time()) symbol
] = f"{symbol.upper()} dividend was on {datePretty} and a new date hasn't been announced yet."
elif daysDelta > 0:
divMessages[
symbol
] = f"{symbol.upper()} dividend is on {datePretty} which is in {daysDelta} Days."
else:
divMessages[symbol] = f"{symbol.upper()} is today."
# 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)
messages[
ticker
] = f"{data['description']}\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
else: else:
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend." divMessages[symbol] = f"{symbol} either doesn't exist or pays no dividend."
return messages return divMessages
def tickerNews(tickers: list): def symbolNews(symbols: list):
messages = {} newsMessages = {}
for ticker in tickers: for symbol in symbols:
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/news/last/3?token={IEX_TOKEN}" IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/news/last/3?token={IEX_TOKEN}"
with urllib.request.urlopen(IEXurl) as url: response = requests.get(IEXurl)
data = json.loads(url.read().decode()) if response.status_code is 200:
if data: data = response.json()
messages[ticker] = f"News for **{ticker.upper()}**:\n" newsMessages[symbol] = f"News for **{symbol.upper()}**:\n"
for news in data: for news in data:
message = f"\t[{news['headline']}]({news['url']})\n\n" message = f"\t[{news['headline']}]({news['url']})\n\n"
messages[ticker] = messages[ticker] + message newsMessages[symbol] = newsMessages[symbol] + message
else: else:
messages[ newsMessages[
ticker symbol
] = f"No news found for: {ticker}\nEither today is boring or the ticker does not exist." ] = f"No news found for: {symbol}\nEither today is boring or the symbol does not exist."
return messages return newsMessages
def tickerInfo(tickers: list): def symbolInfo(symbols: list):
messages = {} infoMessages = {}
for ticker in tickers: for symbol in symbols:
IEXurl = ( IEXurl = (
f"https://cloud.iexapis.com/stable/stock/{ticker}/company?token={IEX_TOKEN}" f"https://cloud.iexapis.com/stable/stock/{symbol}/company?token={IEX_TOKEN}"
) )
with urllib.request.urlopen(IEXurl) as url: response = requests.get(IEXurl)
data = json.loads(url.read().decode())
if data: if response.status_code is 200:
messages[ data = response.json()
ticker infoMessages[
symbol
] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n" ] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n"
else: else:
messages[ infoMessages[
ticker symbol
] = f"No information found for: {ticker}\nEither today is boring or the ticker does not exist." ] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist."
return messages return infoMessages

View File

@ -1,2 +1,2 @@
telegram==0.0.1 python-telegram-bot==11.1.0
python-telegram-bot==11.1.0 requests==2.21.0