diff --git a/README.md b/README.md index 763623c..db8e975 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,38 @@ Prospect Capital Corp. Declares June 2019 Dividend of $0.06 Per Share The dividend is in: 38 Days 3 Hours 53 Minutes 22 Seconds. ``` +💡 you can also call the dividend command using /div + +### /news + +To get the latest news about a stock symbol use `/news` followed by any text that has symbols with a dollar sign in front of them. So, the following command: +``` +/news $psec +``` +Would return news for Prospect Capital: + +News for PSEC: + + [Yield-Starved Investors Still Accumulating BDCs Paying More Than 10% Annually](https://cloud.iexapis.com/v1/news/article/d994b8b5-9fbf-4ceb-afbe-e6defcfc6352) + + [Assessing Main Street Capital's Results For Q1 2019 (Includes Updated Price Target And Investment Ratings Analysis)](https://cloud.iexapis.com/v1/news/article/e60899bc-5230-4388-a609-fc2b8736a7d4) + + [Fully Assessing Prospect Capital's Fiscal Q3 2019 (Includes Current Recommendation And Price Target)](https://cloud.iexapis.com/v1/news/article/08881160-72c5-4f5d-885b-1751187d24eb) + +### /info + +To get information about a stock type `/info` followed by any text that has symbols with a dollar sign in front of them. So, the following command: +``` +/info $psec +``` +Would return information about Prospect Capitals: + +Company Name: [Prospect Capital Corp.](http://www.prospectstreet.com/) +Industry: Investment Managers +Sector: Finance +CEO: John Francis Barry +Description: Prospect Capital Corp. is a business development company, which engages in lending to and investing in private businesses. It also involves in generating current income and long-term capital appreciation through debt and equity investments. The company was founded on April 13, 2004 and is headquartered in New York, NY. + ## 🏁 Getting Started You can either choose to use the hosted version of the bot by [clicking here](https://t.me/SimpleStockBot) or you can host your own bot with the instructions below. diff --git a/bot.py b/bot.py index 0daec43..d0b3954 100644 --- a/bot.py +++ b/bot.py @@ -27,7 +27,7 @@ def start(bot, update): def help(bot, update): """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)" + message = "[Please see the docs for Bot information](https://simple-stock-bots.gitlab.io/site/telegram/)" update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) @@ -66,7 +66,44 @@ 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 info(bot, update): + """ + waits for /info command and then finds 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 tickerInfo(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 +122,8 @@ 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)) + dp.add_handler(CommandHandler("info", info)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, tickerDetect)) @@ -102,4 +141,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/functions.py b/functions.py index a714205..fef6fd3 100644 --- a/functions.py +++ b/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,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 +