continue rewrite
This commit is contained in:
128
xmpp_tg/xmpp.py
128
xmpp_tg/xmpp.py
@@ -266,6 +266,13 @@ class XMPPTelegram(ComponentXMPP):
|
||||
_unknown_command_handler = lambda self, self2: "Unknown command, for a list send '!help'"
|
||||
_on_connect = lambda: None
|
||||
|
||||
class WrongNumberOfArgsError(Exception):
|
||||
pass
|
||||
|
||||
def _min_args(self, num_args):
|
||||
if len(self.arguments) < num_args:
|
||||
raise WrongNumberOfArgumentsError("!{} needs at least {} arguments".format(self._command, num_args))
|
||||
|
||||
def __init__(self, msg):
|
||||
self._command = msg["body"].split(" ")[0][1:]
|
||||
self._handler = getattr(self, self._command, self._unknown_command_handler)
|
||||
@@ -347,8 +354,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
"""Initiates Telegram session
|
||||
|
||||
Usage: !login <phone number in international format>"""
|
||||
if len(hndl.arguments) != 1:
|
||||
return "Wrong number of arguments"
|
||||
hndl._min_args(1)
|
||||
|
||||
phone_no = hndl.arguments[0]
|
||||
|
||||
@@ -366,6 +372,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
return 'Please enter one-time code via !code 12345.'
|
||||
|
||||
def code(hndl, self):
|
||||
hndl._min_args(1)
|
||||
|
||||
code = hndl.arguments[0]
|
||||
jid = hndl.jid
|
||||
@@ -392,6 +399,8 @@ class XMPPTelegram(ComponentXMPP):
|
||||
return 'You are already authenticated. Please use !logout before new login.'
|
||||
|
||||
def password(hndl, self):
|
||||
hndl._min_args(1)
|
||||
|
||||
password = hndl.arguments[1]
|
||||
jid = self.jid
|
||||
|
||||
@@ -433,8 +442,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
|
||||
def add(hndl, self): #1 arg
|
||||
"""Add contact by nickname or t.me link"""
|
||||
if len(hndl.arguments) != 1:
|
||||
return "Wrong number of arguments"
|
||||
hndl._min_args(1)
|
||||
|
||||
argument = hndl.arguments[0]
|
||||
jid = hndl.jid
|
||||
@@ -454,8 +462,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
|
||||
def join(hndl, self): #1 arg
|
||||
"""Join conference via invite link"""
|
||||
if len(hndl.arguments) != 1:
|
||||
return "Wrong number of arguments"
|
||||
hndl._min_args(1)
|
||||
|
||||
argument = hndl.arguments[0]
|
||||
jid = hndl.jid
|
||||
@@ -466,6 +473,7 @@ class XMPPTelegram(ComponentXMPP):
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def group(self): #2 args
|
||||
hndl._min_args(2)
|
||||
# group name? #
|
||||
groupname = parsed[1]
|
||||
|
||||
@@ -477,39 +485,51 @@ class XMPPTelegram(ComponentXMPP):
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def channel(self): #1 arg
|
||||
hndl._min_args(1)
|
||||
groupname = parsed[1]
|
||||
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, broadcast = True))
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def supergroup(self): #1 arg
|
||||
hndl._min_args(1)
|
||||
groupname = parsed[1]
|
||||
self.tg_connections[jid].invoke(CreateChannelRequest(groupname, groupname, megagroup = True))
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def username(self): #1 arg
|
||||
"""Change your Telegram display name"""
|
||||
username = parsed[1]
|
||||
self.tg_connections[jid].invoke(UpdateUsernameRequest(username))
|
||||
"""Change your Telegram nickname"""
|
||||
hndl._min_args(1)
|
||||
|
||||
def name(self): #1 or 2 args
|
||||
"""Change your Telegram display name"""
|
||||
firstname = parsed[1]
|
||||
lastname = parsed[2] if len(parsed) > 2 else None
|
||||
self.tg_connections[jid].invoke(UpdateProfileRequest(first_name = firstname, last_name = lastname))
|
||||
username = hndl.arguments[0]
|
||||
|
||||
def about(self): #>0 args
|
||||
self.tg_connections[hndl.jid].invoke(UpdateUsernameRequest(username))
|
||||
|
||||
def name(hndl, self): #1 or 2 args
|
||||
"""Change your Telegram display name"""
|
||||
hndl._min_args(1)
|
||||
|
||||
firstname = hndl.arguments[0]
|
||||
lastname = hndl.arguments[1] if len(hndl.arguments) > 1 else None
|
||||
|
||||
self.tg_connections[hndl.jid].invoke(UpdateProfileRequest(first_name = firstname, last_name = lastname))
|
||||
|
||||
def about(hndl, self): #>0 args
|
||||
"""Change your Telegram 'about' text"""
|
||||
about = iq['body'][7:]
|
||||
self.tg_connections[jid].invoke(UpdateProfileRequest(about = about))
|
||||
hndl._min_args(1)
|
||||
|
||||
def import_contact(self): #2 args
|
||||
about = hndl.msg['body'][7:]
|
||||
self.tg_connections[hndl.jid].invoke(UpdateProfileRequest(about = about))
|
||||
|
||||
def import_contact(hndl, self): #2 args
|
||||
"""Add contact by phone number"""
|
||||
phone = parsed[1]
|
||||
firstname = parsed[2]
|
||||
lastname = parsed[3] if len(parsed) > 3 else None
|
||||
hndl._min_args(2)
|
||||
|
||||
phone = hndl.arguments[0]
|
||||
firstname = hndl.arguments[1]
|
||||
lastname = hndl.arguments[2] if len(hndl.arguments) > 2 else None
|
||||
|
||||
contact = InputPhoneContact(client_id=generate_random_long(), phone=phone, first_name=firstname, last_name=lastname)
|
||||
self.tg_connections[jid].invoke(ImportContactsRequest([contact]))
|
||||
self.tg_connections[hndl.jid].invoke(ImportContactsRequest([contact]))
|
||||
self.tg_process_dialogs(jid)
|
||||
|
||||
def roster(hndl, self):
|
||||
@@ -518,6 +538,35 @@ class XMPPTelegram(ComponentXMPP):
|
||||
for jid,tid in self.contact_list[hndl.jid].items():
|
||||
response += "{}: {}\n".format(tid, jid)
|
||||
return response
|
||||
|
||||
def oldhelp(hndl, self):
|
||||
"""Old !help message"""
|
||||
return ('=== Available gateway commands ===:\n\n'
|
||||
'!help - Displays this text\n'
|
||||
'!login +123456789 - Initiates Telegram session\n'
|
||||
'!code 12345 - Entering one-time code during auth\n'
|
||||
'!password abc123 - Entering password during two-factor auth\n'
|
||||
'!configure - Configure transport settings\n'
|
||||
#'!list_sessions - List all created sessions at Telegram servers\n'
|
||||
#'!delete_session 123 - Delete session\n'
|
||||
'!logout - Deletes current Telegram session at gate\n'
|
||||
'!reload_dialogs - Reloads dialogs list from Telegram\n\n'
|
||||
|
||||
'!add - Find and add Telegram contact. Any formats accepted (nickname or t.me link)\n\n'
|
||||
|
||||
'!join - Join Telegram conference via invite link \n\n'
|
||||
|
||||
'!import phone firstname lastname - Add Telegram contact with phone number \n\n'
|
||||
|
||||
'!group GroupName @InviteContact - Create a normal group\n'
|
||||
'!supergroup SupergroupName - Create a supergroup\n'
|
||||
'!channel ChannelName - Create a channel\n\n'
|
||||
|
||||
'!name first last - Change your name in Telegram\n'
|
||||
'!about text - Change about text in Telegram\n'
|
||||
'!username - Changes your @username in Telegram\n\n'
|
||||
|
||||
'!roster - Lists yout TG roster\n')
|
||||
|
||||
|
||||
def process_command(self, msg):
|
||||
@@ -540,6 +589,8 @@ class XMPPTelegram(ComponentXMPP):
|
||||
else:
|
||||
if isinstance(e, NotAuthorizedError):
|
||||
reply = str(e)
|
||||
elif isinstance(e, self.MessageHandler.WrongNumberOfArgsError):
|
||||
reply = str(e)
|
||||
else:
|
||||
logging.error("Exception in command from {}, command was '{}'".format(msg["from"],msg["body"]))
|
||||
traceback.print_exc()
|
||||
@@ -548,39 +599,6 @@ class XMPPTelegram(ComponentXMPP):
|
||||
self.gate_reply_message(msg, reply)
|
||||
#msg.reply(reply).send()
|
||||
|
||||
parsed = msg['body'].split(' ')
|
||||
jid = msg['from'].bare
|
||||
|
||||
if parsed[0] == '!help':
|
||||
self.gate_reply_message(msg, '=== Available gateway commands ===:\n\n'
|
||||
|
||||
'!help - Displays this text\n'
|
||||
'!login +123456789 - Initiates Telegram session\n'
|
||||
'!code 12345 - Entering one-time code during auth\n'
|
||||
'!password abc123 - Entering password during two-factor auth\n'
|
||||
'!configure - Configure transport settings\n'
|
||||
#'!list_sessions - List all created sessions at Telegram servers\n'
|
||||
#'!delete_session 123 - Delete session\n'
|
||||
'!logout - Deletes current Telegram session at gate\n'
|
||||
'!reload_dialogs - Reloads dialogs list from Telegram\n\n'
|
||||
|
||||
'!add - Find and add Telegram contact. Any formats accepted (nickname or t.me link)\n\n'
|
||||
|
||||
'!join - Join Telegram conference via invite link \n\n'
|
||||
|
||||
'!import phone firstname lastname - Add Telegram contact with phone number \n\n'
|
||||
|
||||
'!group GroupName @InviteContact - Create a normal group\n'
|
||||
'!supergroup SupergroupName - Create a supergroup\n'
|
||||
'!channel ChannelName - Create a channel\n\n'
|
||||
|
||||
'!name first last - Change your name in Telegram\n'
|
||||
'!about text - Change about text in Telegram\n'
|
||||
'!username - Changes your @username in Telegram\n\n'
|
||||
|
||||
'!roster - Lists yout TG roster\n'
|
||||
)
|
||||
|
||||
def process_chat_user_command(self, iq):
|
||||
parsed = iq['body'].split(' ')
|
||||
jid = iq['from'].bare
|
||||
|
||||
Reference in New Issue
Block a user