diff --git a/bot.py b/bot.py index 541b36e..15f14b1 100644 --- a/bot.py +++ b/bot.py @@ -4,6 +4,9 @@ import io import logging import os import random +import html +import json +import traceback import mplfinance as mpf import telegram @@ -59,6 +62,24 @@ def license(update, context): update.message.reply_text(text=s.license, parse_mode=telegram.ParseMode.MARKDOWN) +def status(update, context): + message = "" + try: + # Bot Status + bot_resp = datetime.datetime.utcnow() - update.message.date + message += f"It took {bot_resp.total_seconds()} seconds for the bot to get your message.\n" + + # IEX Status + message += s.iex_status() + "\n" + + # Message Status + message += s.message_status() + except Exception as ex: + message += f"*\n\nERROR ENCOUNTERED:*\n{ex}\n\n*The bot encountered an error while attempting to find errors. Please contact the bot admin.*" + + update.message.reply_text(text=message, parse_mode=telegram.ParseMode.MARKDOWN) + + def donate(update, context): chat_id = update.message.chat_id @@ -358,6 +379,24 @@ def error(update, context): """Log Errors caused by Updates.""" logger.warning('Update "%s" caused error "%s"', update, error) + tb_list = traceback.format_exception( + None, context.error, context.error.__traceback__ + ) + tb_string = "".join(tb_list) + + message = ( + f"An exception was raised while handling an update\n" + f"
update = {html.escape(json.dumps(update.to_dict(), indent=2, ensure_ascii=False))}"
+        "
\n\n" + f"
context.chat_data = {html.escape(str(context.chat_data))}
\n\n" + f"
context.user_data = {html.escape(str(context.user_data))}
\n\n" + f"
{html.escape(tb_string)}
" + ) + + # Finally, send the message + update.message.reply_text(text=message, parse_mode=telegram.ParseMode.HTML) + update.message.reply_text(text="Please inform the bot admin of this issue.") + def main(): """Start the context.bot.""" @@ -384,6 +423,7 @@ def main(): dp.add_handler(CommandHandler("crypto", crypto)) dp.add_handler(CommandHandler("random", rand_pick)) dp.add_handler(CommandHandler("donate", donate)) + dp.add_handler(CommandHandler("status", status)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, symbol_detect)) diff --git a/functions.py b/functions.py index 9efba65..13660ce 100644 --- a/functions.py +++ b/functions.py @@ -96,6 +96,27 @@ _Donations can only be made in a chat directly with @simplestockbot_ if return_df: return symbols, datetime.now() + def iex_status(self): + status = r.get("https://pjmps0c34hp7.statuspage.io/api/v2/status.json").json() + + if status["status"]["indicator"] == "none": + return "IEX Cloud is currently not reporting any issues with its API." + else: + return f"{['status']['indicator']}: {['status']['description']}. Please check the status page for more information. https://status.iexapis.com" + + def message_status(self): + usage = r.get( + f"https://cloud.iexapis.com/stable/account/metadata?token={self.IEX_TOKEN}" + ).json() + + if ( + usage["messagesUsed"] >= usage["messageLimit"] - 10000 + and not usage["payAsYouGoEnabled"] + ): + return "Bot may be out of IEX Credits." + else: + return "Bot has available IEX Credits" + def search_symbols(self, search: str): """ Performs a fuzzy search to find stock symbols closest to a search term.