restructure
This commit is contained in:
@@ -4,6 +4,7 @@ import logging, traceback, pprint
|
||||
|
||||
from sleekxmpp.componentxmpp import ComponentXMPP
|
||||
from sleekxmpp import Presence, Message
|
||||
from sleekxmpp.exceptions import XMPPError
|
||||
|
||||
from telethon.tl.functions.messages import GetDialogsRequest, SendMessageRequest, SendMediaRequest, EditMessageRequest, DeleteMessagesRequest, ImportChatInviteRequest, GetFullChatRequest, AddChatUserRequest, DeleteChatUserRequest, CreateChatRequest, DeleteHistoryRequest
|
||||
from telethon.tl.functions.account import UpdateStatusRequest, GetAuthorizationsRequest, UpdateProfileRequest, UpdateUsernameRequest
|
||||
@@ -125,54 +126,57 @@ class XMPPTelegram(ComponentXMPP):
|
||||
self.gate_reply_message(iq, 'Error.')
|
||||
|
||||
else: # -- normal message --
|
||||
try:
|
||||
tg_id = int(iq['to'].node[1:])
|
||||
except ValueError:
|
||||
self.gate_reply_message(iq, 'Invalid JID')
|
||||
tg_peer = None
|
||||
msg = iq['body']
|
||||
reply_mid = None
|
||||
self.send_tg_message(iq)
|
||||
|
||||
if msg.startswith('>'): # quoting check
|
||||
msg_lines = msg.split('\n')
|
||||
matched = re.match(r'>[ ]*(?P<mid>[\d]+)[ ]*', msg_lines[0]) #TODO: check regex
|
||||
matched = matched.groupdict() if matched else {}
|
||||
def send_tg_message(self, iq):
|
||||
jid = iq['from'].bare
|
||||
try:
|
||||
tg_id = int(iq['to'].node[1:])
|
||||
except ValueError:
|
||||
raise XMPPError(text="Invalid Telegram-ID")
|
||||
tg_peer = None
|
||||
msg = iq['body']
|
||||
reply_mid = None
|
||||
|
||||
if 'mid' in matched: # citation
|
||||
reply_mid = int(matched['mid'])
|
||||
msg = '\n'.join(msg_lines[1:])
|
||||
if msg.startswith('>'): # quoting check
|
||||
msg_lines = msg.split('\n')
|
||||
matched = re.match(r'>[ ]*(?P<mid>[\d]+)[ ]*', msg_lines[0]) #TODO: check regex
|
||||
matched = matched.groupdict() if matched else {}
|
||||
|
||||
if iq['to'].bare.startswith( ('u', 'b') ): # normal user
|
||||
tg_peer = InputPeerUser(tg_id, self.tg_dialogs[jid]['users'][tg_id].access_hash)
|
||||
elif iq['to'].bare.startswith('g'): # generic group
|
||||
tg_peer = InputPeerChat(tg_id)
|
||||
elif iq['to'].bare.startswith( ('s', 'c') ): # supergroup
|
||||
tg_peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash)
|
||||
|
||||
# peer OK.
|
||||
if tg_peer:
|
||||
result = None
|
||||
|
||||
# detect media
|
||||
if msg.startswith('http') and re.match(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg):
|
||||
urls = re.findall(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg)
|
||||
message = msg.replace(urls[0], '')
|
||||
media = InputMediaPhotoExternal(urls[0], "Image")
|
||||
try:
|
||||
result = self.tg_connections[jid].invoke(SendMediaRequest(tg_peer, media, message, random_id = generate_random_long(), reply_to_msg_id = reply_mid))
|
||||
except Exception:
|
||||
print('Media upload failed.')
|
||||
|
||||
# media send failed. #
|
||||
if not result:
|
||||
result = self.tg_connections[jid].invoke(SendMessageRequest(tg_peer, msg, generate_random_long(), reply_to_msg_id=reply_mid))
|
||||
if 'mid' in matched: # citation
|
||||
reply_mid = int(matched['mid'])
|
||||
msg = '\n'.join(msg_lines[1:])
|
||||
|
||||
# find sent message id and save it
|
||||
if result and hasattr(result, 'id'): # update id
|
||||
msg_id = result.id
|
||||
self.tg_dialogs[jid]['messages'][tg_id] = {'id': msg_id, 'body': msg}
|
||||
#self.send_message(mto=iq['from'], mfrom=iq['to'], mtype='chat', mbody='[Your MID:{}]'.format(msg_id))
|
||||
if iq['to'].bare.startswith( ('u', 'b') ): # normal user
|
||||
tg_peer = InputPeerUser(tg_id, self.tg_dialogs[jid]['users'][tg_id].access_hash)
|
||||
elif iq['to'].bare.startswith('g'): # generic group
|
||||
tg_peer = InputPeerChat(tg_id)
|
||||
elif iq['to'].bare.startswith( ('s', 'c') ): # supergroup
|
||||
tg_peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash)
|
||||
|
||||
# peer OK.
|
||||
if tg_peer:
|
||||
result = None
|
||||
|
||||
# detect media
|
||||
if msg.startswith('http') and re.match(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg):
|
||||
urls = re.findall(r'(?:http\:|https\:)?\/\/.*\.(?:' + self.config['media_external_formats'] + ')', msg)
|
||||
message = msg.replace(urls[0], '')
|
||||
media = InputMediaPhotoExternal(urls[0], "Image")
|
||||
try:
|
||||
result = self.tg_connections[jid].invoke(SendMediaRequest(tg_peer, media, message, random_id = generate_random_long(), reply_to_msg_id = reply_mid))
|
||||
except Exception:
|
||||
print('Media upload failed.')
|
||||
|
||||
# media send failed. #
|
||||
if not result:
|
||||
result = self.tg_connections[jid].invoke(SendMessageRequest(tg_peer, msg, generate_random_long(), reply_to_msg_id=reply_mid))
|
||||
|
||||
# find sent message id and save it
|
||||
if result and hasattr(result, 'id'): # update id
|
||||
msg_id = result.id
|
||||
self.tg_dialogs[jid]['messages'][tg_id] = {'id': msg_id, 'body': msg}
|
||||
#self.send_message(mto=iq['from'], mfrom=iq['to'], mtype='chat', mbody='[Your MID:{}]'.format(msg_id))
|
||||
|
||||
def event_presence_unsub(self, presence):
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user