当使用 telethon 在特定频道中发送消息时,我如何 运行 执行操作?
How do I run an action when a message is sent in a specific channel with telethon?
我正在尝试获取频道中新消息的消息内容,但电视节目文档非常混乱,我不知道如何指定特定频道。
Updates in the docs的介绍解释了如何注册处理程序:
from telethon import TelegramClient, events
client = TelegramClient('anon', api_id, api_hash)
@client.on(events.NewMessage)
async def my_event_handler(event):
if 'hello' in event.raw_text:
await event.reply('hi!')
client.start()
client.run_until_disconnected()
它有指向 events.NewMessage
的链接,我们可以看到它有以下可选参数:
class NewMessage(chats=None, *, blacklist_chats=False, func=None, incoming=None, outgoing=None, from_users=None, forwards=None, pattern=None)
它还声称:
在那里,我们找到 chats
:
的文档
May be one or more entities (username/peer/etc.), preferably IDs. By default, only matching chats will be handled.
综合起来:
MY_CHANNEL = -1001234 # or 'username', or invite link, etc.
@client.on(events.NewMessage(MY_CHANNEL))
async def my_event_handler(event):
pass # do work...
我正在尝试获取频道中新消息的消息内容,但电视节目文档非常混乱,我不知道如何指定特定频道。
Updates in the docs的介绍解释了如何注册处理程序:
from telethon import TelegramClient, events
client = TelegramClient('anon', api_id, api_hash)
@client.on(events.NewMessage)
async def my_event_handler(event):
if 'hello' in event.raw_text:
await event.reply('hi!')
client.start()
client.run_until_disconnected()
它有指向 events.NewMessage
的链接,我们可以看到它有以下可选参数:
class NewMessage(chats=None, *, blacklist_chats=False, func=None, incoming=None, outgoing=None, from_users=None, forwards=None, pattern=None)
它还声称:
在那里,我们找到 chats
:
May be one or more entities (username/peer/etc.), preferably IDs. By default, only matching chats will be handled.
综合起来:
MY_CHANNEL = -1001234 # or 'username', or invite link, etc.
@client.on(events.NewMessage(MY_CHANNEL))
async def my_event_handler(event):
pass # do work...