mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 15:17:28 +00:00
added /news functionality #22
This commit is contained in:
parent
99f0bf68d1
commit
fd1baad811
22
bot.py
22
bot.py
@ -66,7 +66,26 @@ def dividend(bot, update):
|
||||
update.message.reply_text(
|
||||
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
|
||||
|
||||
def news(bot, update):
|
||||
"""
|
||||
waits for /news command and then finds news info on that ticker.
|
||||
"""
|
||||
message = update.message.text
|
||||
chat_id = update.message.chat_id
|
||||
tickers = getTickers(message)
|
||||
|
||||
if tickers:
|
||||
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||
|
||||
for symbol, reply in tickerNews(tickers).items():
|
||||
|
||||
update.message.reply_text(
|
||||
text=reply, parse_mode=telegram.ParseMode.MARKDOWN
|
||||
)
|
||||
|
||||
|
||||
def error(bot, update, error):
|
||||
"""Log Errors caused by Updates."""
|
||||
logger.warning('Update "%s" caused error "%s"', update, error)
|
||||
@ -85,6 +104,7 @@ def main():
|
||||
dp.add_handler(CommandHandler("help", help))
|
||||
dp.add_handler(CommandHandler("dividend", dividend))
|
||||
dp.add_handler(CommandHandler("div", dividend))
|
||||
dp.add_handler(CommandHandler("news", news))
|
||||
|
||||
# on noncommand i.e message - echo the message on Telegram
|
||||
dp.add_handler(MessageHandler(Filters.text, tickerDetect))
|
||||
|
22
functions.py
22
functions.py
@ -6,6 +6,8 @@ import urllib.request
|
||||
from datetime import datetime
|
||||
|
||||
IEX_TOKEN = os.environ["IEX"]
|
||||
|
||||
|
||||
def getTickers(text: str):
|
||||
"""
|
||||
Takes a blob of text and returns any stock tickers found.
|
||||
@ -76,3 +78,23 @@ def tickerDividend(tickers: list):
|
||||
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
|
||||
|
||||
return messages
|
||||
|
||||
|
||||
def tickerNews(tickers: list):
|
||||
messages = {}
|
||||
|
||||
for ticker in tickers:
|
||||
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/news/last/3?token={IEX_TOKEN}"
|
||||
with urllib.request.urlopen(IEXurl) as url:
|
||||
data = json.loads(url.read().decode())
|
||||
if data:
|
||||
messages[ticker] = f"News for **{ticker.upper()}**:\n"
|
||||
for news in data:
|
||||
message = f"\t[{news['headline']}]({news['url']})\n\n"
|
||||
messages[ticker] = messages[ticker] + message
|
||||
else:
|
||||
messages[
|
||||
ticker
|
||||
] = f"No news found for: {ticker}\nEither today is boring or the ticker does not exist."
|
||||
|
||||
return messages
|
||||
|
Loading…
x
Reference in New Issue
Block a user