-2

this is my cod that i have? on python*

import asyncio
from telethon.sync import TelegramClient, events

def main():
    # Укажите свои api_id, api_hash и bot_token
    api_id = 27468555
    api_hash = '0c05213e90441c28256c5ab8a0d3c266'
    bot_token = '6144633328:AAFGbLEpHZlsBxD81oQ5PPhdm4ly7gpIHTQ'

    client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)

    chat_id_group = -1001958404478
    channel_source = 601230760

    @client.on(events.NewMessage(chats=channel_source))
    def handler(event):
        '''Забирает посты из телеграмм каналов и посылает их в наш канал'''
        message = event.message
        client.send_message(chat_id_group, message)

    client.start()
    client.run_until_disconnected()

main()

but it`s not work

When i started this code the debbuger show that def handler don`t work

  • please check the [telethon docs](https://docs.telethon.dev/en/stable/) first and use `async def handler`, asynchrony is needed. and await any function calls `await client.send_message(...)` – MustA Jun 17 '23 at 09:32

1 Answers1

0

I found this method:

from pyrogram import Client

api_id = "###"
api_hash = "###"

def copy_posts(source_channel_id, destination_channel_id):
    with Client("my_account", api_id, api_hash) as client:
        try:
            source_messages = client.get_chat_history(source_channel_id, limit=100)
            for message in source_messages:
                if message.photo is not None:
                    photo = message.photo
                    file_id = photo.file_id
                    caption = message.caption if message.caption else None
                    client.send_photo(destination_channel_id, file_id, caption=caption)
                elif message.text:
                    client.send_message(destination_channel_id, message.text)
            print("All good, posts forward in your group")
        except Exception as e:
            print(f"Error: {e}")

if __name__ == "__main__":
    source_channel_id =   # Parse channel
    destination_channel_id =   # Forward channel
    
    copy_posts(source_channel_id, destination_channel_id)

I don't think it's a good solution. Feel free to write a better one.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 19 '23 at 01:07