0

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.

  1. 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.
  2. 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()
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jun 25 '22 at 14:20
  • Are you forwarding the message or just the part of it? have you tried client.forward_messages() instead? I suggest you try to remove link_preview=False from your send_message request – dotheM Jun 25 '22 at 15:10
  • Hi @dotheM! I removed 'link_preview=False', but nothing changed. Then I replaced 'send_message' with 'forward_messages', posted text with link in first channel and got this text in console: " [!] Error forward_messages() got an unexpected keyword argument 'file' " – Vlad Piker Jun 25 '22 at 15:36
  • So forward_message work on the text but not file, is that so? is it possible to just use forward_message for the text and send_message for files and media? – dotheM Jun 25 '22 at 15:53

0 Answers0