将消息从一个频道自动转发到另一个频道的方法使用Python

huangapple go评论71阅读模式
英文:

How auto forward messages from a channel to another channel using Python

问题

I can help you with the translation. Here's the translated text:

如何将消息从一个频道即时自动转发到另一个频道?我创建了一个示例,但不起作用。

这是我的示例:

from telethon import TelegramClient, events

api_id = 99999999
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxx'
client = TelegramClient('session_name', api_id, api_hash)
client.start()

@client.on(events.NewMessage)
async def my_event_handler(event):
    chat = await event.get_chat()
    if chat.id == 'Horse Racing Daily Tips Premium':
        await client.forward_messages('Greyhounds Daily Tips Premium', event.message)

with client:
    client.run_until_disconnected()

我试图从我的源频道“Horse Racing Daily Tips Premium”发送到我的目标频道'Greyhounds Daily Tips Premium';我是Python和Telegram机器人的新手,所以不确定是否按照正确的方式进行操作。我已经从命令行启动了机器人,但不起作用。

英文:

How do I auto forward messages instantly from a channel to another channel? I created an example but doesn't work.

This is my example:

    `from telethon import TelegramClient, events

    api_id = 99999999
    api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxx'
    client = TelegramClient('session_name', api_id, api_hash)
    client.start()

    @client.on(events.NewMessage)
    async def my_event_handler(event):
        chat = await event.get_chat()
        if chat.id == 'Horse Racing Daily Tips Premium':
            await client.forward_messages('Greyhounds Daily Tips Premium', event.message)

    with client:
        client.run_until_disconnected()`

I tried to send from my source channel "Horse Racing Daily Tips Premium" to my destination channel 'Greyhounds Daily Tips Premium'; I am new of Python and Telegram bot so I am not sure I did the correct way.
I already launched the bot from my prompt command line, but doesn't work

答案1

得分: 1

1- chat.id 是整数,你不能将其与字符串进行比较。

async def my_event_handler(event):
    if not event.is_channel:
        return
    chat = await event.get_chat()
    if chat.title == 'Horse Racing Daily Tips Premium':
        await event.forward_to('Greyhounds Daily Tips Premium')

2- 如果有多个聊天具有相同的名称,使用字符串聊天标题来获取或匹配不总是可靠的。因此,如果是私人频道,最好使用聊天标识符,如果是公共频道,最好使用用户名。但如果对您的情况有效,可以继续使用。

英文:

1- chat.id is integer, you can't compare it to a string

async def my_event_handler(event):
    if not event.is_channel:
        return
    chat = await event.get_chat()
    if chat.title == 'Horse Racing Daily Tips Premium':
        await event.forward_to('Greyhounds Daily Tips Premium')

2- using string chat title to fetch or match is not reliable all the time if more than one chat has the same name, so prefer chat identifiers if it's private channel or usernames if it's public channel, but if it works for your case keep it.

答案2

得分: 0

你可以使用 event.forward_to(),像这样:

@client.on(events.NewMessage())
async def my_event_handler(event):
    if event.chat_id == 123456:
        await event.forward_to(987654)

或者使用 client.forward_messages(),像这样:

@client.on(events.NewMessage())
async def my_event_handler(event):
    if event.chat_id == 123456:
        await client.forward_messages(98765, event.id, event.chat_id)
英文:

you can use event.forward_to()
like this:

@client.on(events.NewMessage())
async def my_event_handler(event):
    if event.chat_id == 123456:
        await event.forward_to(987654) 

or use client.forward_messages()
like this:

@client.on(events.NewMessage())
async def my_event_handler(event):
    if event.chat_id == 123456:
        #await client.forward_messages(where you want to forward, messages id, from where)
        await client.forward_messages(98765, event.id, event.chat_id)

huangapple
  • 本文由 发表于 2023年6月29日 15:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定