Skip to content

Commit

Permalink
Refactor filter names and imports in bot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rabilrbl committed Dec 28, 2023
1 parent 8a3482e commit 3fb6980
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
17 changes: 6 additions & 11 deletions gemini_pro_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from telegram.ext import (
CommandHandler,
MessageHandler,
filters,
Application,
)
from gemini_pro_bot.filters import AuthorizedUserFilter
from gemini_pro_bot.filters import AuthFilter, MessageFilter, PhotoFilter
from dotenv import load_dotenv
from gemini_pro_bot.handlers import (
start,
Expand All @@ -25,19 +24,15 @@ def start_bot() -> None:
application = Application.builder().token(os.getenv("BOT_TOKEN")).build()

# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start, filters=AuthorizedUserFilter()))
application.add_handler(CommandHandler("help", help_command, filters=AuthorizedUserFilter()))
application.add_handler(CommandHandler("new", newchat_command, filters=AuthorizedUserFilter()))
application.add_handler(CommandHandler("start", start, filters=AuthFilter))
application.add_handler(CommandHandler("help", help_command, filters=AuthFilter))
application.add_handler(CommandHandler("new", newchat_command, filters=AuthFilter))

# Any text message is sent to LLM to generate a response
application.add_handler(
MessageHandler( AuthorizedUserFilter() & ~filters.COMMAND & filters.TEXT, handle_message)
)
application.add_handler(MessageHandler(MessageFilter, handle_message))

# Any image is sent to LLM to generate a response
application.add_handler(
MessageHandler( AuthorizedUserFilter() & ~filters.COMMAND & filters.PHOTO, handle_image)
)
application.add_handler(MessageHandler(PhotoFilter, handle_image))

# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
20 changes: 16 additions & 4 deletions gemini_pro_bot/filters.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import os
from telegram import Update
from telegram.ext.filters import UpdateFilter
from telegram.ext.filters import UpdateFilter, COMMAND, TEXT, PHOTO
from dotenv import load_dotenv

load_dotenv()

AUTHORIZED_USERS = [i.strip() for i in os.getenv("AUTHORIZED_USERS", "").split(",") if i.strip()]
_AUTHORIZED_USERS = [
i.strip() for i in os.getenv("AUTHORIZED_USERS", "").split(",") if i.strip()
]


class AuthorizedUserFilter(UpdateFilter):
def filter(self, update: Update):
if not AUTHORIZED_USERS:
if not _AUTHORIZED_USERS:
return True
return update.message.from_user.username in AUTHORIZED_USERS or str(update.message.from_user.id) in AUTHORIZED_USERS
return (
update.message.from_user.username in _AUTHORIZED_USERS
or str(update.message.from_user.id) in _AUTHORIZED_USERS
)


AuthFilter = AuthorizedUserFilter()
MessageFilter = AuthFilter & ~COMMAND & TEXT
PhotoFilter = AuthFilter & ~COMMAND & PHOTO

0 comments on commit 3fb6980

Please sign in to comment.