diff --git a/config_example.py b/config_example.py index 1df42e2..f378100 100644 --- a/config_example.py +++ b/config_example.py @@ -22,6 +22,8 @@ CONFIG = { 'db_connect': 'db.sqlite', + 'media_external_formats': 'png|jpg|jpeg|gif|mp3|mp4|ogg', + 'media_web_link_prefix': 'http://tlgrm.localhost/media/', 'media_store_path': '/var/tg4xmpp/media/', 'media_max_download_size': 1024 * 1024 * 100, # in bytes diff --git a/xmpp_tg/__init__.py b/xmpp_tg/__init__.py index df6c300..573e21c 100644 --- a/xmpp_tg/__init__.py +++ b/xmpp_tg/__init__.py @@ -1,3 +1,3 @@ from xmpp_tg.xmpp import XMPPTelegram -__version__ = '0.3.2' +__version__ = '0.3.3' diff --git a/xmpp_tg/xmpp.py b/xmpp_tg/xmpp.py index 64ef1a2..3e740ab 100644 --- a/xmpp_tg/xmpp.py +++ b/xmpp_tg/xmpp.py @@ -10,7 +10,7 @@ import sleekxmpp from sleekxmpp.componentxmpp import ComponentXMPP import xml.etree.ElementTree as ET -from telethon.tl.functions.messages import GetDialogsRequest, SendMessageRequest, ImportChatInviteRequest, GetFullChatRequest, AddChatUserRequest, DeleteChatUserRequest, CreateChatRequest, DeleteHistoryRequest +from telethon.tl.functions.messages import GetDialogsRequest, SendMessageRequest, SendMediaRequest, ImportChatInviteRequest, GetFullChatRequest, AddChatUserRequest, DeleteChatUserRequest, CreateChatRequest, DeleteHistoryRequest from telethon.tl.functions.account import UpdateStatusRequest, GetAuthorizationsRequest, UpdateProfileRequest, UpdateUsernameRequest from telethon.tl.functions.contacts import DeleteContactRequest, BlockRequest, UnblockRequest, ImportContactsRequest from telethon.tl.functions.channels import JoinChannelRequest, LeaveChannelRequest, InviteToChannelRequest, EditBannedRequest, CreateChannelRequest @@ -21,6 +21,7 @@ from telethon.tl.types import PeerChannel, PeerChat, PeerUser, Chat, ChatForbidd from telethon.tl.types import UserStatusOnline, UserStatusRecently, UserStatusOffline from telethon.tl.types import Updates, UpdateShortSentMessage, UpdateMessageID from telethon.tl.types import ChannelBannedRights +from telethon.tl.types import InputMediaPhotoExternal from telethon.tl.types.messages import Dialogs, DialogsSlice from telethon.helpers import generate_random_long @@ -145,10 +146,16 @@ class XMPPTelegram(ComponentXMPP): tg_peer = InputPeerChannel(tg_id, self.tg_dialogs[jid]['supergroups'][tg_id].access_hash) if tg_peer: - # Отправляем сообщение и получаем новый апдейт - result = self.tg_connections[jid].invoke( - SendMessageRequest(tg_peer, msg, generate_random_long(), reply_to_msg_id=reply_mid) - ) + # 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]) + result = self.tg_connections[jid].invoke(SendMediaRequest(tg_peer, media, message, random_id = generate_random_long(), reply_to_msg_id = reply_mid)) + # no media — plain message # + else: + result = self.tg_connections[jid].invoke(SendMessageRequest(tg_peer, msg, generate_random_long(), reply_to_msg_id=reply_mid)) + msg_id = None # Ищем ID отправленного сообщения