I used the code to create a user-bot that sends messages from one telegram channel to my telegram channel. The user-boot as a whole works well, but there are 2 problems.
- In the channel where I get posts, there is a text formatted as a link. In posts on my channel, the link in the text disappears, this usual text.
- Posts with ordinary links do not go to my channel at all. How to fix it?
from telethon import TelegramClient, events
from telethon import errors
import asyncio
import re
# ----
api_id =
api_hash = ""
# ----
channels = '@' # channel from
my_channel = '@' # channel to
KEYS = {} # Replacing words
# ----
Bad_Keys = [] # If there is a bad key in the post, the post is not published
# ----
tags = ''# Adding text
# ----
with TelegramClient('myApp13', api_id, api_hash) as client:
print("~Activated~")
@client.on(events.NewMessage(chats=channels))
async def Messages(event):
if not [element for element in Bad_Keys
if event.raw_text.lower().__contains__(element)]:
text = event.raw_text
for i in KEYS:
text = re.sub(i, KEYS[i], text)
if not event.grouped_id\
and not event.message.forward:
try:
await client.send_message(
entity=my_channel,
file=event.message.media,
message=text + tags,
parse_mode='md',
link_preview=False)
except errors.FloodWaitError as e:
print(f'[!] Flud errror. Wait: {e.seconds} seconds')
await asyncio.sleep(e.seconds)
except Exception as e:
print('[!] Error', e)
elif event.message.text and not event.message.media\
and not event.message.forward\
and not event.grouped_id:
try:
await client.send_message(
entity=my_channel,
message=text + tags,
parse_mode='md',
link_preview=False)
except errors.FloodWaitError as e:
print(f'[!] Flud error. Wait: {e.seconds} seconds')
await asyncio.sleep(e.seconds)
except Exception as e:
print('[!] Error', e)
elif event.message.forward:
try:
await event.message.forward_to(my_channel)
except errors.FloodWaitError as e:
print(f'[!] Flud error. Wait: {e.seconds} seconds')
except Exception as e:
print('[!] Error', e)
@client.on(events.Album(chats=channels))
async def Album(event):
text = event.original_update.message.message
print(text)
if not [element for element in Bad_Keys
if text.lower().__contains__(element)]:
for i in KEYS:
text = re.sub(i, KEYS[i], text)
try:
await client.send_message(
entity=my_channel,
file=event.messages,
message=text + tags,
parse_mode='md',
link_preview=False)
except errors.FloodWaitError as e:
print(f'[!] Flud error. Wait: {e.seconds} seconds')
await asyncio.sleep(e.seconds)
except Exception as e:
print('[!] Error', e)
client.run_until_disconnected()