简体   繁体   English

如何使用telethon,python仅从电报频道接收新消息

[英]how to recieve only new message fom telegram channel using telethon, python

I am new to python and its framework and i am having this trouble in accessing the latest messages from a telegram channel.我是 python 及其框架的新手,我在从电报频道访问最新消息时遇到了这个问题。

I want to get the latest message from the channel and process them using my code.我想从频道获取最新消息并使用我的代码处理它们。 with some searching in stackoverflow i found a solution for getting the messages of a channel.通过在 stackoverflow 中的一些搜索,我找到了一个获取频道消息的解决方案。 yet that code dumps all the messages from that telegram channel.但是该代码会转储来自该电报频道的所有消息。

The code to get channel messages.获取频道消息的代码。

    from telethon import TelegramClient, events, sync
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 123456
api_hash = 'abcdefghijklmnopqrstuvwxyz123456789'

client = TelegramClient('anon', api_id, api_hash)

async def main():
    # You can print the message history of any chat:
    async for message in client.iter_messages('SampleChannel'):
        print(message.sender.username, message.text)
        print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

with client:
    client.loop.run_until_complete(main())

I only want the recent messages on that channel.我只想要该频道上的最近消息。 suggest me the code i need to modify to do so.建议我需要修改的代码。

The documentation for client.iter_messages shows that this method has a limit parameter: client.iter_messages的文档显示此方法有一个limit参数:

Number of messages to be retrieved.要检索的消息数。

Your code simply needs to make use of this parameter:您的代码只需要使用此参数:

async def main():
    limit = 10
    async for message in client.iter_messages('SampleChannel', limit):
        print(message.sender.username, message.text)

If by "the latest" you mean the new messages as they arrive, look at the docs , and more specifically NewMessage , there is a good example there.如果“最新”是指新消息到达时,请查看docs ,更具体地说是NewMessage ,那里有一个很好的例子。 You can define several event handlers on NewMessage according to many parameters, like channel IDs, and even message pattern.您可以根据许多参数(例如通道 ID,甚至消息模式)在 NewMessage 上定义多个事件处理程序。 For your case, for instance, where you want to get new messages from a single channel, it could be例如,对于您的情况,您想从单个频道获取新消息,它可能是

client.on(events.NewMessage(chats=myChannelIDList))
async def my_event_handler(event):
    print(event.message)

This will print the new messages arriving to the entities defined in myChannelIDList.这将打印到达 myChannelIDList 中定义的实体的新消​​息。

According to the docs, the chats parameter can have "one or more entities (username/peer/etc.), preferably IDs."根据文档,chats 参数可以具有“一个或多个实体(用户名/对等方/等),最好是 ID”。 There are several Q/A in stackoverflow about how to get this IDs, this one might be useful for you since you are with Python, more specifically the example with this code: stackoverflow 中有几个关于如何获取这个 ID 的 Q/A,这个可能对你有用,因为你使用的是 Python,更具体地说是这个代码的例子:

#To get the channel_id,group_id,user_id
for chat in client.get_dialogs():
    print('name:{0} ids:{1} is_user:{2} is_channel{3} is_group:{4}'.format(chat.name,chat.id,chat.is_user,chat.is_channel,chat.is_group))

This will print IDs of all the "dialogs" of the Telegram account.这将打印 Telegram 帐户的所有“对话框”的 ID。 If you are part of a channel it will be there.如果您是频道的一部分,它将在那里。

Regards!问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM