XMPP bot that crossposts files from a Telegram channel https://conversations.im/j/dadoem@muc.kalli.st
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

import configparser
from telethon import TelegramClient, events
import io
import mimetypes
mimetypes.init()
from slixmpp import ClientXMPP
config = configparser.ConfigParser()
config.read('config.ini')
api_id = config['telegram']['api_id']
api_hash = config['telegram']['api_hash']
tchannel = config['telegram']['channel']
jid = config['xmpp']['jid']
password = config['xmpp']['password']
xchannel = config['xmpp']['channel']
nick = config['xmpp']['nick']
tclient = TelegramClient('anon', api_id, api_hash)
xclient = ClientXMPP(jid, password)
xclient.register_plugin('xep_0363')
xclient.register_plugin('xep_0045')
xclient.register_plugin('xep_0066')
input_channel = f'https://t.me/{tchannel}'
@tclient.on(events.NewMessage(chats=input_channel))
async def newMessageListener(event):
message = event.message
print("MESSAGE")
output = io.BytesIO()
mime_type = message.file.mime_type
ext = mimetypes.guess_extension(mime_type)
filename = f'file{ext}'
if await message.download_media(output):
try:
url = await xclient.plugin['xep_0363'].upload_file(filename, content_type=mime_type, input_file=output)
message = xclient.make_message(xchannel)
message['body'] = url
message['type'] = 'groupchat'
message['oob']['url'] = url
message.send()
except Exception as e:
print(e)
def session_start(*args):
xclient.send_presence()
xclient.get_roster()
xclient.plugin['xep_0045'].join_muc(xchannel, nick)
xclient.add_event_handler("session_start", session_start)
xclient.add_event_handler("disconnected", lambda _: xclient.connect())
with tclient:
xclient.connect()
xclient.process()
tclient.run_until_disconnected()