1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-15 23:06:40 +00:00

added status and error handling. #18 #28

This commit is contained in:
Anson Biggs 2021-02-04 23:16:50 -07:00
parent fb9ff6b651
commit fa67a62b5f
2 changed files with 61 additions and 0 deletions

40
bot.py
View File

@ -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"<pre>update = {html.escape(json.dumps(update.to_dict(), indent=2, ensure_ascii=False))}"
"</pre>\n\n"
f"<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n"
f"<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n"
f"<pre>{html.escape(tb_string)}</pre>"
)
# 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))

View File

@ -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.