From cf6b03e31a68d3ee07273ee5156fa5098c88cdc2 Mon Sep 17 00:00:00 2001 From: Anson Date: Tue, 11 Jun 2019 05:37:10 -0700 Subject: [PATCH] ported news and info commands from telegram bot --- bot.py | 18 ++++++++++++++++++ functions.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/bot.py b/bot.py index 3407ed3..ab94150 100644 --- a/bot.py +++ b/bot.py @@ -22,10 +22,28 @@ async def on_message(message): await message.channel.send(reply) else: await message.channel.send("No tickers found.") + + elif message.content.startswith("/news"): + replies = tickerNews(getTickers(message.content)) + if replies: + for tick, reply in replies.items(): + await message.channel.send(reply) + else: + await message.channel.send("No tickers found.") + + elif message.content.startswith("/info"): + replies = tickerInfo(getTickers(message.content)) + if replies: + for tick, reply in replies.items(): + await message.channel.send(reply) + else: + await message.channel.send("No tickers found.") + elif message.content.startswith('/help'): """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/discord/)" await message.channel.send(message) + # If no commands, check for any tickers. else: replies = tickerDataReply(getTickers(message.content)) diff --git a/functions.py b/functions.py index 7df7498..fef6fd3 100644 --- a/functions.py +++ b/functions.py @@ -78,3 +78,46 @@ 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 + + +def tickerInfo(tickers: list): + messages = {} + + for ticker in tickers: + IEXurl = ( + f"https://cloud.iexapis.com/stable/stock/{ticker}/company?token={IEX_TOKEN}" + ) + with urllib.request.urlopen(IEXurl) as url: + data = json.loads(url.read().decode()) + if data: + messages[ + ticker + ] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n" + + else: + messages[ + ticker + ] = f"No information found for: {ticker}\nEither today is boring or the ticker does not exist." + + return messages +