Telegram 的 websocket
Telegram's websocket
我想连接到电报的 WebSocket 或其他协议,以接收每条消息。我正在检查开发工具中的网络选项卡,但找不到任何 WebSocket 协议,但是,当我进入控制台时,我可以看到 [SW] on message {type: "ping", localNotifications: true, lang: {…}, settings: {…}}
这让我相信,发送它是为了保持与 WebSocket 的连接。我为 python 尝试了 telethon
库,但它只允许我在聊天中显示所有消息。
from telethon import TelegramClient
import asyncio
client = TelegramClient(name, api_id, api_hash)
client.start()
for i in client.iter_dialogs():
if i.name == 'test':
chj = i
async def main():
channel = await client.get_entity(chj)
messages = await client.get_messages(channel, limit= None) #pass your own args
#then if you want to get all the messages text
for x in messages:
print(x) #return message.text
"""for i in client.iter_dialogs():
print(i)"""
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
我希望在所需的聊天中,只要有人输入某些内容,我就会像在 WebSocket 中一样收到消息
我找到了答案。这可以使用 telethon
库
中的 events
来实现
from telethon import TelegramClient, events
name = 'test'
api_id = 1234
api_hash = 'hash'
client = TelegramClient(name, api_id, api_hash)
@client.on(events.NewMessage())
async def newMessageListener(event):
newMessage = event.message.message
print(newMessage)
with client:
client.run_until_disconnected()
我想连接到电报的 WebSocket 或其他协议,以接收每条消息。我正在检查开发工具中的网络选项卡,但找不到任何 WebSocket 协议,但是,当我进入控制台时,我可以看到 [SW] on message {type: "ping", localNotifications: true, lang: {…}, settings: {…}}
这让我相信,发送它是为了保持与 WebSocket 的连接。我为 python 尝试了 telethon
库,但它只允许我在聊天中显示所有消息。
from telethon import TelegramClient
import asyncio
client = TelegramClient(name, api_id, api_hash)
client.start()
for i in client.iter_dialogs():
if i.name == 'test':
chj = i
async def main():
channel = await client.get_entity(chj)
messages = await client.get_messages(channel, limit= None) #pass your own args
#then if you want to get all the messages text
for x in messages:
print(x) #return message.text
"""for i in client.iter_dialogs():
print(i)"""
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
我希望在所需的聊天中,只要有人输入某些内容,我就会像在 WebSocket 中一样收到消息
我找到了答案。这可以使用 telethon
库
events
来实现
from telethon import TelegramClient, events
name = 'test'
api_id = 1234
api_hash = 'hash'
client = TelegramClient(name, api_id, api_hash)
@client.on(events.NewMessage())
async def newMessageListener(event):
newMessage = event.message.message
print(newMessage)
with client:
client.run_until_disconnected()