Skip to content

Commit

Permalink
V1.6: Add command to check api status
Browse files Browse the repository at this point in the history
Signed-off-by: starry69 <starry369126@outlook.com>
  • Loading branch information
starry-shivam committed May 5, 2020
1 parent 998b7bc commit f79734e
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions starrybot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from telegram.ext import (
Updater, CommandHandler,
run_async)
run_async, Filters)

from telegram import (
Chat, Update,
Expand Down Expand Up @@ -51,8 +51,6 @@

dispatcher = updater.dispatcher

################ START & HELPER FUCS ##############

# Good bots should send actions.
def send_action(action):
"""Sends `action` while processing func command."""
Expand Down Expand Up @@ -95,7 +93,9 @@ def start(update, context):
def error(update, context):
LOGGER.warning('Update "%s" caused error "%s"', update, context.error)

############ WALL FUNCTIONS #############
# WALL FUNCTIONS

BASE_URL = 'https://pixabay.com/api/'

@run_async
@send_action(ChatAction.UPLOAD_PHOTO)
Expand All @@ -110,7 +110,7 @@ def wall(update, context):
return
query = query.replace(" ", "+")
contents = requests.get(
f"https://pixabay.com/api/?key={PIX_API}&q={query}&page=1&per_page=200"
f"{BASE_URL}?key={PIX_API}&q={query}&page=1&per_page=200"
).json()

hits = contents.get('hits')
Expand Down Expand Up @@ -186,7 +186,7 @@ def wallcolor(update, context):
return

contents = requests.get(
f"https://pixabay.com/api/?key={PIX_API}&colors={color}&page=2&per_page=200"
f"{BASE_URL}?key={PIX_API}&colors={color}&page=2&per_page=200"
).json()

hits = contents.get('hits')
Expand Down Expand Up @@ -241,7 +241,7 @@ def editorschoice(update, context):
chat = update.effective_chat

contents = requests.get(
f"https://pixabay.com/api/?key={PIX_API}&editors_choice=true&page=2&per_page=200"
f"{BASE_URL}?key={PIX_API}&editors_choice=true&page=2&per_page=200"
).json()

hits = contents.get('hits')
Expand Down Expand Up @@ -292,7 +292,7 @@ def randomwalls(update, context):
chat = update.effective_chat

contents = requests.get(
f"https://pixabay.com/api/?key={PIX_API}&page=2&per_page=200"
f"{BASE_URL}?key={PIX_API}&page=2&per_page=200"
).json()

hits = contents.get('hits')
Expand Down Expand Up @@ -372,8 +372,30 @@ def about(update, context):
context.bot.sendMessage(chat.id, ABOUT_STR,
parse_mode=ParseMode.HTML)

@run_async
@send_action(ChatAction.TYPING)
def api_status(update, context):
msg = update.effective_message
r = requests.get(
f"{BASE_URL}?key={PIX_API}")
if r.status_code == 200:
status = 'functional'
elif r.status_code == 429:
status = 'limit exceeded!'
else:
status = f'Error! {r.status_code}'

############### HANDLERS #################
ratelimit = r.headers['X-RateLimit-Limit']
remaining = r.headers['X-RateLimit-Remaining']

text = f"API status: <code>{status}</code>\n"
text += f"Requests limit: <code>{ratelimit}</code>\n"
text += f"Requests remaining: <code>{remaining}</code>"

msg.reply_text(text,
parse_mode=ParseMode.HTML)

# HANDLERS
start_handler = CommandHandler('start', start)
help_handler = CommandHandler('help', helper)
wall_handler = CommandHandler(["wall", "wallpaper"], wall)
Expand All @@ -382,6 +404,8 @@ def about(update, context):
editors_handler = CommandHandler('editors', editorschoice)
colors_handler = CommandHandler('colors', colors)
about_handler = CommandHandler('about', about)
apistatus_handler = CommandHandler('status', api_status, filters=Filters.user(894380120))

# Register handlers to dispatcher
dispatcher.add_handler(start_handler)
dispatcher.add_handler(help_handler)
Expand All @@ -391,8 +415,10 @@ def about(update, context):
dispatcher.add_handler(random_handler)
dispatcher.add_handler(colors_handler)
dispatcher.add_handler(about_handler)
dispatcher.add_handler(apistatus_handler)
dispatcher.add_error_handler(error)
############### BOT ENGINE ################

# BOT ENGINE
if WEBHOOK:
LOGGER.info("Starting WallpaperRobot | Using webhook...")
updater.start_webhook(listen="0.0.0.0",
Expand Down

0 comments on commit f79734e

Please sign in to comment.