Skip to content

Commit

Permalink
added AUTH_CHANNEL
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh0253 committed Jan 14, 2021
1 parent 107c79a commit c730947
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.session
*.session-journal
.vscode
*test.py
*test*.py
setup.cfg

# Byte-compiled / optimized / DLL files
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Check `sample_info.py` before editing `info.py` file
* `CACHE_TIME`: The maximum amount of time in seconds that the result of the inline query may be cached on the server
* `USE_CAPTION_FILTER`: Whether bot should use captions to improve search results. (True/False)
* `AUTH_USERS`: Username or ID of users to give access of inline search. Separate multiple users by space. Leave it empty if you don't want to restrict bot usage.
* `AUTH_CHANNEL`: Username or ID of channel. Without subscribing this channel users cannot use bot.
* `INVITE_MSG`: Auth channel invitation message.

### Admin commands
```
Expand Down
10 changes: 10 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"value": "",
"required": false
},
"AUTH_CHANNEL": {
"description": "Username or ID of channel. Without subscribing this channel users cannot use bot.",
"value": "",
"required": false
},
"INVITE_MSG": {
"description": "Auth channel invitation message",
"value": "'Please join @.... to use this bot'",
"required": false
},
"USE_CAPTION_FILTER": {
"description": "Whether bot should use captions to improve search results. (True False)",
"value": "False",
Expand Down
11 changes: 8 additions & 3 deletions info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from os import environ

id_pattern = re.compile(r'^.\d+$')

# Bot information
SESSION = environ.get('SESSION', 'Media_search')
USER_SESSION = environ.get('USER_SESSION', 'User_Bot')
Expand All @@ -14,10 +16,12 @@
USE_CAPTION_FILTER = bool(environ.get('USE_CAPTION_FILTER', False))

# Admins, Channels & Users
ADMINS = [int(admin) if re.search('^\d+$', admin) else admin for admin in environ['ADMINS'].split()]
CHANNELS = [int(ch) if re.search('^.\d+$', ch) else ch for ch in environ['CHANNELS'].split()]
auth_users = [int(user) if re.search('^\d+$', user) else user for user in environ['AUTH_USERS'].split()]
ADMINS = [int(admin) if id_pattern.search(admin) else admin for admin in environ['ADMINS'].split()]
CHANNELS = [int(ch) if id_pattern.search(ch) else ch for ch in environ['CHANNELS'].split()]
auth_users = [int(user) if id_pattern.search(user) else user for user in environ['AUTH_USERS'].split()]
AUTH_USERS = (auth_users + ADMINS) if auth_users else []
auth_channel = environ.get('AUTH_CHANNEL')
AUTH_CHANNEL = int(auth_channel) if auth_channel and id_pattern.search(auth_channel) else auth_channel

# MongoDB information
DATABASE_URI = environ['DATABASE_URI']
Expand All @@ -32,3 +36,4 @@
"""

SHARE_BUTTON_TEXT = 'Checkout {username} for searching files'
INVITE_MSG = environ.get('INVITE_MSG', 'Please join @.... to use this bot')
17 changes: 10 additions & 7 deletions plugins/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from info import START_MSG, CHANNELS, ADMINS
from info import START_MSG, CHANNELS, ADMINS, INVITE_MSG
from utils import Media

logger = logging.getLogger(__name__)
Expand All @@ -11,12 +11,15 @@
@Client.on_message(filters.command('start'))
async def start(bot, message):
"""Start command handler"""
buttons = [[
InlineKeyboardButton('Search Here', switch_inline_query_current_chat=''),
InlineKeyboardButton('Go Inline', switch_inline_query=''),
]]
reply_markup = InlineKeyboardMarkup(buttons)
await message.reply(START_MSG, reply_markup=reply_markup)
if len(message.command) > 1 and message.command[1] == 'subscribe':
await message.reply(INVITE_MSG)
else:
buttons = [[
InlineKeyboardButton('Search Here', switch_inline_query_current_chat=''),
InlineKeyboardButton('Go Inline', switch_inline_query=''),
]]
reply_markup = InlineKeyboardMarkup(buttons)
await message.reply(START_MSG, reply_markup=reply_markup)


@Client.on_message(filters.command('channel') & filters.user(ADMINS))
Expand Down
29 changes: 28 additions & 1 deletion plugins/inline.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import logging
from urllib.parse import quote

from pyrogram import Client, emoji, filters
from pyrogram.errors import UserNotParticipant
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultCachedDocument

from utils import get_search_results
from info import MAX_RESULTS, CACHE_TIME, SHARE_BUTTON_TEXT, AUTH_USERS
from info import MAX_RESULTS, CACHE_TIME, SHARE_BUTTON_TEXT, AUTH_USERS, AUTH_CHANNEL

logger = logging.getLogger(__name__)


@Client.on_inline_query(filters.user(AUTH_USERS) if AUTH_USERS else None)
async def answer(bot, query):
"""Show search results for given inline query"""

if AUTH_CHANNEL and not await is_subscribed(bot, query):
await query.answer(results=[],
cache_time=0,
switch_pm_text='You have to subscribe channel',
switch_pm_parameter="subscribe")
return

results = []
if '|' in query.query:
string, file_type = query.query.split('|', maxsplit=1)
Expand Down Expand Up @@ -75,3 +88,17 @@ def get_size(size):
i += 1
size /= 1024.0
return "%.2f %s" % (size, units[i])


async def is_subscribed(bot, query):
try:
user = await bot.get_chat_member(AUTH_CHANNEL, query.from_user.id)
except UserNotParticipant:
pass
except Exception as e:
logger.exception(e)
else:
if not user.status == 'kicked':
return True

return False
4 changes: 3 additions & 1 deletion sample_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ADMINS = [12345789, 'admin123', 98765432]
CHANNELS = [-10012345678, -100987654321, 'channelusername']
AUTH_USERS = []
AUTH_CHANNEL = None

# MongoDB information
DATABASE_URI = "mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb]?retryWrites=true&w=majority"
Expand All @@ -27,4 +28,5 @@
Here you can search files in inline mode. Just press follwing buttons and start searching.
"""

SHARE_BUTTON_TEXT = 'Checkout {username} for searching files'
SHARE_BUTTON_TEXT = 'Checkout {username} for searching files'
INVITE_MSG = 'Please join @.... to use this bot'

0 comments on commit c730947

Please sign in to comment.