From f4adfb1915cb0686aee047bdaade3c0719a301f1 Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Sun, 28 Mar 2021 23:31:21 -0700 Subject: [PATCH] added /trending command --- IEX_Symbol.py | 15 +++++++++++++++ T_info.py | 17 +++++++++-------- bot.py | 13 +++++++++++++ cg_Crypto.py | 15 +++++++++++++++ symbol_router.py | 24 ++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/IEX_Symbol.py b/IEX_Symbol.py index 232d190..af03183 100644 --- a/IEX_Symbol.py +++ b/IEX_Symbol.py @@ -424,3 +424,18 @@ class IEX_Symbol: return m else: return f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." + + def trending(self) -> list[str]: + """Gets current coins trending on coingecko + + Returns + ------- + list[str] + list of $$ID: NAME + """ + + stocks = r.get( + f"https://cloud.iexapis.com/stable/stock/market/list/mostactive?token={self.IEX_TOKEN}" + ).json() + + return [f"${s['symbol']}: {s['companyName']}" for s in stocks] diff --git a/T_info.py b/T_info.py index 8a777d4..0ce667b 100644 --- a/T_info.py +++ b/T_info.py @@ -26,14 +26,15 @@ The bot detects _"Symbols"_ using either one or two dollar signs before the symb Simply calling a symbol in any message that the bot can see will also return the price. So a message like: `I wonder if $$btc will go to the Moon now that $tsla accepts it as payment` would return the current price for both Bitcoin and Tesla. **Commands** - - /donate [amount in USD] to donate. đŸŽ—ī¸ - - /dividend $[symbol] will return dividend information for the symbol. 📅 - - /intra $[symbol] Plot of the stocks movement since the last market open. 📈 - - /chart $[symbol] Plot of the stocks movement for the past 1 month. 📊 - - /news $[symbol] News about the symbol. 📰 - - /info $[symbol] General information about the symbol. â„šī¸ - - /stat $[symbol] Key statistics about the symbol. đŸ”ĸ - - /help Get some help using the bot. 🆘 + - `/donate [amount in USD]` to donate. đŸŽ—ī¸ + - `/dividend $[symbol]` Dividend information for the symbol. 📅 + - `/intra $[symbol]` Plot of the stocks movement since the last market open. 📈 + - `/chart $[symbol]` Plot of the stocks movement for the past 1 month. 📊 + - `/news $[symbol]` News about the symbol. 📰 + - `/info $[symbol]` General information about the symbol. â„šī¸ + - `/stat $[symbol]` Key statistics about the symbol. đŸ”ĸ + - `/trending` Trending Stocks and Cryptos. + - `/help` Get some help using the bot. 🆘 **Inline Features** You can type @SimpleStockBot `[search]` in any chat or direct message to search for the stock bots full list of stock symbols and return the price of the ticker. Then once you select the ticker want the bot will send a message as you in that chat with the latest stock price. diff --git a/bot.py b/bot.py index dcb1871..e9dc768 100644 --- a/bot.py +++ b/bot.py @@ -365,6 +365,18 @@ def stat(update: Update, context: CallbackContext): ) +def trending(update: Update, context: CallbackContext): + """ + Trending Symbols + """ + + chat_id = update.message.chat_id + + context.bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + + update.message.reply_text(text=s.trending(), parse_mode=telegram.ParseMode.MARKDOWN) + + def inline_query(update: Update, context: CallbackContext): """ Handles inline query. @@ -448,6 +460,7 @@ def main(): dp.add_handler(CommandHandler("info", info)) dp.add_handler(CommandHandler("stat", stat)) dp.add_handler(CommandHandler("stats", stat)) + dp.add_handler(CommandHandler("trending", trending)) dp.add_handler(CommandHandler("search", search)) dp.add_handler(CommandHandler("intraday", intra)) dp.add_handler(CommandHandler("intra", intra, run_async=True)) diff --git a/cg_Crypto.py b/cg_Crypto.py index 6ba1686..95a2edd 100644 --- a/cg_Crypto.py +++ b/cg_Crypto.py @@ -263,3 +263,18 @@ class cg_Crypto: return f"{symbol} does not have a description available." return f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." + + def trending(self) -> list[str]: + """Gets current coins trending on coingecko + + Returns + ------- + list[str] + list of $$ID: NAME + """ + + coins = r.get("https://api.coingecko.com/api/v3/search/trending").json()[ + "coins" + ] + + return [f"$${c['item']['id'].upper()}: {c['item']['name']}" for c in coins] diff --git a/symbol_router.py b/symbol_router.py index de12731..d8799ed 100644 --- a/symbol_router.py +++ b/symbol_router.py @@ -284,6 +284,30 @@ class Router: return replies + def trending(self) -> str: + """Checks APIs for trending symbols. + + Returns + ------- + list[str] + List of preformatted strings to be sent to user. + """ + + stocks = self.stock.trending() + coins = self.crypto.trending() + + reply = "`Trending Stocks:\n" + reply += "-" * len("Trending Stocks:") + "\n" + for stock in stocks: + reply += stock + "\n" + + reply += "\n\nTrending Crypto:\n" + reply += "-" * len("Trending Crypto:") + "\n" + for coin in coins: + reply += coin + "\n" + + return reply + "`" + def random_pick(self) -> str: choice = random.choice(