Skip to content

Instantly share code, notes, and snippets.

@kuznetsov-m
Created August 19, 2021 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuznetsov-m/5fdc12f768359c6791589483eaf48c57 to your computer and use it in GitHub Desktop.
Save kuznetsov-m/5fdc12f768359c6791589483eaf48c57 to your computer and use it in GitHub Desktop.
Forward messages from channel to channel in Telegram
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon import TelegramClient
from telethon.tl.functions.messages import AddChatUserRequest
from telethon.tl.functions.messages import ForwardMessagesRequest
from telethon.tl.functions.messages import GetChatsRequest
from telethon.tl.functions.contacts import GetContactsRequest
from telethon.tl.functions.contacts import SearchRequest
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.tl.functions.channels import GetMessagesRequest
from telethon.tl.functions.users import GetFullUserRequest
from telethon.tl.types import InputChannel
from telethon.tl.types import InputUser
from telethon.tl.types import InputPeerChannel
from telethon.tl.types import InputPeerUser
from telethon.tl.types import UpdateNewChannelMessage
from telethon.tl.types import PeerUser, PeerChat, PeerChannel
from telethon.utils import get_input_peer
import re
import time
import sys
#Put your params here:
#!
api_id =
api_hash = ''
phone_number = ''
#!
client = TelegramClient('r0mkTelephone', api_id, api_hash, update_workers=0)
channel_from = sys.argv[1]
channel_to_send = sys.argv[2]
if client.connect():
print('Connected')
if not client.is_user_authorized():
client.send_code_request(phone_number)
myself = client.sign_in(phone_number, input('Enter code: '))
client.updates.polling = True
channel_manual = client.get_entity('t.me/' + channel_from)
print("Channel to: " + str(channel_manual.title))
print("Channel to ID: " + str(channel_manual.id))
print("Channel to Hash: " + str(channel_manual.access_hash))
channel_from_input_peer = InputPeerChannel(channel_manual.id, channel_manual.access_hash)
channel_to = client.get_entity('t.me/' + channel_to_send)
print("Channel to: " + str(channel_to.title))
print("Channel to ID: " + str(channel_to.id))
print("Channel to Hash: " + str(channel_to.access_hash))
channel_to_input_peer = InputPeerChannel(channel_to.id, channel_to.access_hash)
while True:
update = client.updates.poll()
if type(update) == UpdateNewChannelMessage and update.message.to_id.channel_id == channel_manual.id:
print('message id ' + str(update.message.id))
print('channel id ' + str(update.message.to_id.channel_id))
#print(update)
#print(dir(update))
client.invoke(ForwardMessagesRequest(from_peer=channel_from_input_peer, id=[update.message.id], to_peer=channel_to_input_peer))
#print(client.invoke(GetMessagesRequest(channel=my_channel_to, id=[update.message.id])))
#client.invoke(ForwardMessagesRequest(from_peer=my_channel_from, id=[update.message.id], to_peer=my_channel_to))
else:
continue
client.add_update_handler(forward)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment