Skip to content

Commit

Permalink
V1.8: Optimize performance using ujosn, use defaults class to reduce …
Browse files Browse the repository at this point in the history
…some repetition

Signed-off-by: starry69 <starry369126@outlook.com>
  • Loading branch information
starry-shivam committed May 6, 2020
1 parent 9a44a4d commit c675b0c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests
python-telegram-bot
ujson
54 changes: 24 additions & 30 deletions starrybot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

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

from telegram import(
ChatAction, ParseMode,
Expand Down Expand Up @@ -47,11 +48,6 @@
else:
from config import TOKEN, PIX_API, WEBHOOK


updater = Updater(TOKEN, use_context=True)

dispatcher = updater.dispatcher

# Good bots should send actions.
def send_action(action):
"""Sends `action` while processing func command."""
Expand All @@ -65,10 +61,14 @@ def command_func(update, context, *args, **kwargs):

return decorator


START_MSG = f"""Hello there! my name is <b>{dispatcher.bot.first_name}</b>.
I'm here to give you some cool high definition wallpapers.
@run_async
@send_action(ChatAction.TYPING)
def start(update, context):
START_MSG = f"""Hello there! my name is <b>{
context.bot.first_name}</b>.
I'm here to give you some cool high definition stock Images.
\nClick: /help to get list of commands!"""
update.effective_message.reply_text(START_MSG)

HELP_MSG = """
Here are the list of available commands i can help you with.\n
Expand All @@ -82,12 +82,8 @@ def command_func(update, context, *args, **kwargs):
@run_async
@send_action(ChatAction.TYPING)
def helper(update, context):
update.effective_message.reply_text(HELP_MSG)

@run_async
@send_action(ChatAction.TYPING)
def start(update, context):
update.effective_message.reply_text(START_MSG, parse_mode=ParseMode.HTML)
update.effective_message.reply_text(HELP_MSG,
parse_mode=None)

# Log Errors caused by Updates

Expand All @@ -97,6 +93,7 @@ def error(update, context):
# WALL FUNCTIONS

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

@run_async
@send_action(ChatAction.UPLOAD_PHOTO)
Expand Down Expand Up @@ -134,7 +131,7 @@ def wall(update, context):
keyboard = [[
InlineKeyboardButton(text="PageLink 🌐", url=imgurl),
InlineKeyboardButton(text="Author 👸",
url=f'https://pixabay.com/users/{author}-{authid}')
url=f'{AUTH_URL}/{author}-{authid}')
]]


Expand All @@ -150,7 +147,6 @@ def wall(update, context):
context.bot.send_photo(chat.id, photo=preview,
caption=(WALL_STR),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.HTML,
timeout=60)

context.bot.send_document(chat.id,
Expand Down Expand Up @@ -210,7 +206,7 @@ def wallcolor(update, context):
keyboard = [[
InlineKeyboardButton(text="PageLink 🌐", url=imgurl),
InlineKeyboardButton(text="Author 👸",
url=f'https://pixabay.com/users/{author}-{authid}')
url=f'{AUTH_URL}/{author}-{authid}')
]]

WCOLOR_STR = f"""
Expand All @@ -225,7 +221,6 @@ def wallcolor(update, context):
context.bot.send_photo(chat.id, photo=preview,
caption=(WCOLOR_STR),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.HTML,
timeout=60)

context.bot.send_document(chat.id,
Expand Down Expand Up @@ -261,7 +256,7 @@ def editorschoice(update, context):
keyboard = [[
InlineKeyboardButton(text="PageLink 🌐", url=imgurl),
InlineKeyboardButton(text="Author 👸",
url=f'https://pixabay.com/users/{author}-{authid}')
url=f'{AUTH_URL}/users/{author}-{authid}')
]]

EDITOR_STR = f"""
Expand All @@ -275,7 +270,6 @@ def editorschoice(update, context):
context.bot.send_photo(chat.id, photo=preview,
caption=(EDITOR_STR),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.HTML,
timeout=60)

context.bot.send_document(chat.id,
Expand Down Expand Up @@ -312,7 +306,7 @@ def randomwalls(update, context):
keyboard = [[
InlineKeyboardButton(text="PageLink 🌐", url=imgurl),
InlineKeyboardButton(text="Author 👸",
url=f'https://pixabay.com/users/{author}-{authid}')
url=f'{AUTH_URL}/{author}-{authid}')
]]

RANDOM_STR = f"""
Expand All @@ -326,7 +320,6 @@ def randomwalls(update, context):
context.bot.send_photo(chat.id, photo=preview,
caption=(RANDOM_STR),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.HTML,
timeout=60)

context.bot.send_document(chat.id,
Expand Down Expand Up @@ -356,8 +349,7 @@ def colors(update, context):
× <code>turquoise</code>, <code>brown</code>.
"""
update.effective_message.reply_text(
COLOR_STR,
parse_mode=ParseMode.HTML)
COLOR_STR)


@run_async
Expand All @@ -377,8 +369,7 @@ def about(update, context):
I'm written on Python3 using PTB library by this <a href="tg://user?id=894380120">person</a>.
Contact him if you're having any trouble using me!
"""
context.bot.sendMessage(chat.id, ABOUT_STR,
parse_mode=ParseMode.HTML)
context.bot.sendMessage(chat.id, ABOUT_STR)

@run_async
@send_action(ChatAction.TYPING)
Expand All @@ -401,16 +392,18 @@ def api_status(update, context):
text += f"Requests limit: <code>{ratelimit}</code>\n"
text += f"Requests remaining: <code>{remaining}</code>"

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

except Exception:
msg.reply_text(f"API status: {status}")



# HANDLERS
def main():
defaults = Defaults(parse_mode=ParseMode.HTML)
updater = Updater(TOKEN, use_context=True, defaults=defaults)
dispatcher = updater.dispatcher

start_handler = CommandHandler('start', start)
help_handler = CommandHandler('help', helper)
wall_handler = CommandHandler(["wall", "wallpaper"], wall)
Expand All @@ -420,6 +413,7 @@ def main():
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 Down

0 comments on commit c675b0c

Please sign in to comment.