-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorize.py
69 lines (62 loc) · 2.61 KB
/
authorize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from bot.helper.telegram_helper.message_utils import sendMessage
from telegram.ext import run_async
from bot import AUTHORIZED_CHATS, dispatcher
from telegram.ext import CommandHandler
from bot.helper.telegram_helper.filters import CustomFilters
from telegram.ext import Filters
from telegram import Update
from bot.helper.telegram_helper.bot_commands import BotCommands
@run_async
def authorize(update,context):
reply_message = update.message.reply_to_message
msg = ''
with open('authorized_chats.txt', 'a') as file:
if reply_message is None:
# Trying to authorize a chat
chat_id = update.effective_chat.id
if chat_id not in AUTHORIZED_CHATS:
file.write(f'{chat_id}\n')
AUTHORIZED_CHATS.add(chat_id)
msg = 'Chat authorized'
else:
msg = 'Already authorized chat'
else:
# Trying to authorize someone in specific
user_id = reply_message.from_user.id
if user_id not in AUTHORIZED_CHATS:
file.write(f'{user_id}\n')
AUTHORIZED_CHATS.add(user_id)
msg = 'Person Authorized to use the bot!'
else:
msg = 'Person already authorized'
sendMessage(msg, context.bot, update)
@run_async
def unauthorize(update,context):
reply_message = update.message.reply_to_message
if reply_message is None:
# Trying to unauthorize a chat
chat_id = update.effective_chat.id
if chat_id in AUTHORIZED_CHATS:
AUTHORIZED_CHATS.remove(chat_id)
msg = 'Chat unauthorized'
else:
msg = 'Already unauthorized chat'
else:
# Trying to authorize someone in specific
user_id = reply_message.from_user.id
if user_id in AUTHORIZED_CHATS:
AUTHORIZED_CHATS.remove(user_id)
msg = 'Person unauthorized to use the bot!'
else:
msg = 'Person already unauthorized!'
with open('authorized_chats.txt', 'a') as file:
file.truncate(0)
for i in AUTHORIZED_CHATS:
file.write(f'{i}\n')
sendMessage(msg, context.bot, update)
authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize,
filters=CustomFilters.owner_filter & Filters.group)
unauthorize_handler = CommandHandler(command=BotCommands.UnAuthorizeCommand, callback=unauthorize,
filters=CustomFilters.owner_filter & Filters.group)
dispatcher.add_handler(authorize_handler)
dispatcher.add_handler(unauthorize_handler)