-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.py
98 lines (88 loc) · 3.19 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import common
RP = common.load(fname='config.ini')
from base64 import b64decode as decode
from base64 import b64encode as encode
import admin
import string
from random import choice
from functools import wraps
import os
here = os.path.dirname(os.path.realpath(__file__))
import log_help
import logging
LG = logging.getLogger(__name__)
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]
class MyBot(object):
def __init__(self, token, chatIDs):
self.token = token
self.chatIDs = chatIDs
self.me = chatIDs[0]
self.channel = chatIDs[-1]
self.whitelist = ADMINS_id
self.whitelist_id = self.whitelist
self.whitelist_un = ADMINS_un
def __str__(self):
msg = f'Token: {self.token}\n'
msg += 'Chat IDs:\n'
for chid in self.chatIDs:
msg += f' --> {chid}\n'
return msg
def encode_credentials(key, chatids): #, fname='bot.token'):
""" Encode the key and main chatid in a file """
if not isinstance(chatids,list): chatids = [chatids]
key = encode(bytes(key,'utf-8')).decode('utf-8')
chid = []
for chatid in chatids:
chid.append( encode(bytes(chatid,'utf-8')).decode('utf-8') )
return key, chid
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')
bot_chatIDs = [ decode(key).decode('utf-8') for key in api_key[1:] ]
# return bot_token, bot_chatIDs
return MyBot(bot_token, bot_chatIDs)
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(lv): # wrapper
def real_restricted(func):
""" Decorator to restrict the use of certain functions """
@wraps(func)
def wrapped(update, context, *args, **kwargs):
bot = context.bot
user_id = update.effective_user.id
user_nm = update.effective_user.username
chatID = update.message.chat_id
conn,c = admin.connect(RP.DBname)
admin_level = admin.get_user(conn,'chatid',chatID)[0][-2]
if admin_level <= lv: return func(update, context, *args, **kwargs)
else:
txt = "Lo siento, no tienes autorización para usar este comando"
LG.warning(txt)
bot.send_message(chat_id=chatID, text=txt, parse_mode='Markdown')
return
return wrapped
return real_restricted
if __name__ == '__main__':
import sys
args = sys.argv[1:]
if len(args) == 1:
args = args[0]
if args.endswith('token'):
Bot = get_credentials(sys.argv[1])
print(Bot.token)
print(Bot.chatIDs)
else: print('don\'t know what to do')
else:
key,*chatIDs = sys.argv[1:]
key,chatIDs = encode_credentials(key, chatIDs)
print(key)
print(chatIDs)