// Python

Forwarding Telegram channel posts to a group

To forward messages coming to a channel or group on Telegram to another group, you can follow the steps below.

First, you need to get the API codes (api_id, api_hash) for the Telegram account you will use to forward messages from here.

Then, from here you can download the channel_id.py and forward.py files, or create their contents on your own computer by copy-pasting.

A slightly edited version is below.

channel_id.py (with this script, we need to get the chat_id of the channels that the account we will use as a bot is a member of.)

from telethon import TelegramClient, events
import asyncio
api_id = 'xxxx'
api_hash = 'xxxxx'
client = TelegramClient('none', api_id, api_hash)
@client.on(events.NewMessage)
async def handler(event):
        async for dialog in client.iter_dialogs():
          if dialog.is_channel:
           print(f'{dialog.id}:{dialog.title}')

client.start()
client.run_until_disconnected()

forward.py The script that will perform the message forwarding:

import asyncio

from telethon import TelegramClient, events

#-100117365xxxx:A KANAL
#-100164216xxxx:B GRUP
#-100126874xxxx:C GRUP

#Bu kanala mesaj gelince
reciedID = -100117365xxxx
reciedTitle = "A KANALI"

#Bu gruba yaz
sendGrp = -100164216xxxx
sendGrpTitle = 'B Grubu'

api_id = 'xxxx'
api_hash = 'xxxxx'
client = TelegramClient('none', api_id, api_hash)
@client.on(events.NewMessage)
async def handler(event):
    chat = await event.get_chat()
    chat_id = event.chat_id
    print(chat_id)

    if chat_id == reciedID : 
        print(reciedTitle + ' mesaj var')
        await client.send_message(sendGrp, event.raw_text)
        print(sendGrpTitle + ' yonlendirilecek')
       

client.start()
client.run_until_disconnected()

 

Finally, for the above code to work, you need to download/update the Python libraries on your computer.

sudo apt update
sudo apt install python3-pip

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade telethon

Then, we need to write the api_id and api_hash values into the channel_id.py file and get the chat_id of the groups and channels that the bot is a member of. After that, we can customize the chat_id parts in the forward.py file according to our needs and perform the message forwarding.

Bonus:

#Linux screen commands:
#New session:
screen -S session_name

#Closing:
screen -X -S session_id.session_name quit

#Connecting to a session:
screen -a -r session_id.session_name

#detach from current screen
CTRL + a + d

 

// FEEDBACK

Was this useful?

Your rating shows how helpful this note is to other readers.

Was this useful?

Click the stars to rate.

Average rating 0 / 5. Total votes: 0

No one has rated this post yet. Be the first!

Sorry this post was not helpful.

Your feedback is valuable to us.

How can we make this post more helpful?

// JOIN THE CONVERSATION

Share your experience

Got a question, a different solution or a note to add?

// KEEP READING

One more note.

All posts ↗︎