-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.py
63 lines (55 loc) · 2.12 KB
/
credentials.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from base64 import b64decode as decode
from base64 import b64encode as encode
import string
from random import choice
from functools import wraps
import os
here = os.path.dirname(os.path.realpath(__file__))
ADMINS = open(here+'/whitelist.private','r').read().strip().splitlines()
ADMINS_id = [int(x.split(',')[0]) for x in ADMINS]
ADMINS_un = [x.split(',')[1] for x in ADMINS]
def encode_credentials(key, chatid): #, fname='bot.token'):
""" Encode the key and main chatid in a file """
key = encode(bytes(key,'utf-8')).decode('utf-8')
chatid = encode(bytes(chatid,'utf-8')).decode('utf-8')
return key, chatid
def get_credentials(api_file=here+'/telegram_bot.private'):
""" decode the key and main chatid from a file """
api_key = open(api_file,'r').read().strip().splitlines()
bot_token = decode(api_key[0]).decode('utf-8')
bot_chatID = decode(api_key[1]).decode('utf-8')
return bot_token, bot_chatID
def rand_string(pwdSize=8):
""" Generates a random string of letters and digits with pwdSize length """
## all possible letters and numbers
chars = string.ascii_letters + string.digits
return ''.join((choice(chars)) for x in range(pwdSize))
def restricted(func):
""" Decorator to restrict the use of certain functions """
@wraps(func)
def wrapped(update, context, *args, **kwargs):
user_id = update.effective_user.id
user_nm = update.effective_user.username
chatID = update.message.chat_id
if user_id not in ADMINS_id or user_nm not in ADMINS_un:
txt = "Unauthorized access denied for %s (%s)"%(user_nm,user_id)
context.bot.send_message(chat_id=chatID, text=txt, parse_mode='Markdown')
return
return func(update, context, *args, **kwargs)
return wrapped
if __name__ == '__main__':
import sys
try:
key,chatID = sys.argv[1:]
key,chatID = encode_credentials(key, chatID)
print(key)
print(chatID)
except ValueError:
token,chatID = get_credentials(sys.argv[1])
print(token)
print(chatID)
except IndexError:
print('File not specified')
exit()