Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

python - telegram forward message from private channel to another channel

this code below can forward message from channel to another channel but how to modify it to forward from private channel that i'm not admin to my own private channel please :

from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel
import yaml
import sys
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s -      %(message)s')
logging.getLogger('telethon').setLevel(level=logging.WARNING)
logger = logging.getLogger(__name__)

 def start(config):
client = TelegramClient(config["session_name"], 
                        config["api_id"], 
                        config["api_hash"])
client.start()

input_channels_entities = []
output_channel_entities = []
for d in client.iter_dialogs():
    if d.name in config["input_channel_names"]:
        input_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
    if d.name in config["output_channel_names"]:
        output_channel_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
        
if not output_channel_entities:
    logger.error(f"Could not find any output channels in the user's dialogs")
    sys.exit(1)

if not input_channels_entities:
    logger.error(f"Could not find any input channels in the user's dialogs")
    sys.exit(1)
    
logging.info(f"Listening on {len(input_channels_entities)} channels. Forwarding messages to    {len(output_channel_entities)} channels.")

@client.on(events.NewMessage(chats=input_channels_entities))
async def handler(event):
    for output_channel in output_channel_entities:
        await client.forward_messages(output_channel, event.message)

client.run_until_disconnected()

if __name__ == "__main__":
if len(sys.argv) < 2:
    print(f"Usage: {sys.argv[0]} {{CONFIG_PATH}}")
    sys.exit(1)
with open(sys.argv[1], 'rb') as f:
    config = yaml.safe_load(f)
start(config)

NB: this is config file :

api_id: 'your api id get it from : my.telegram.org'

api_hash: 'your api hash get it from : my.telegram.org'

session_name: 'session name'

input_channel_names:

  • 'Input Channel Name 1'
  • 'Input Channel Name 2'

output_channel_names:

  • 'output Channel Name 1'
  • 'output Channel Name 2'

can someone modify it to forward message from private channel that im not admin to my own private channel telegram please


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...