有没有办法获取我在 Telegram(最好是 Telegram Desktop)中加入的频道列表? 我也许能够在浏览器中使用 JS 甚至 Selenium 提出一个解决方案,但难道没有更符合人体工程学的方法可以在 Telegram 桌面(如 API)中工作吗?
如果重要的话,我在 Ubuntu 20.04 上将 Telegram 作为 snap 应用程序运行。
有没有办法获取我是 Telegram(最好是 Telegram Desktop)成员的 channels 列表?
好吧,如果你只想知道你是哪个频道的成员,你可以使用
dialog.entity
检查当前频道是否属于 Channel
client.iter_dialogs()
循环遍历每个频道isinstance
检查它是否是Channel
User
(聊天)ChatForbidden
(聊天你踢出去)from telethon import TelegramClient
from telethon.tl.types import Channel
import asyncio
async def main():
async with TelegramClient('anon', '2234242', 'e40gte4t63636423424325a57') as client:
# For each dialog
async for dialog in client.iter_dialogs(limit = None):
# If this is a Channel
if isinstance(dialog.entity, ( Channel )):
# Log
dialogType = type(dialog.entity)
print(f'{dialog.title:<42}\t{dialog.id}')
asyncio.run(main())
会输出类似的东西
channel_1 <channel_id>
(最好是电报桌面)
好吧,官方 Telegram Bot API 没有获取频道成员的选项。因此,使用 Telegrams MTPROTO 创建您自己的客户端来执行此操作是检索此数据的唯一合法方法。
如前所述,您可以抓取 Telegram 网络版,但这很可能很快就会改变,您需要编辑您的抓取工具。 Telethon 似乎是一个有效/稳定的解决方案。
您可以使用 Python telethon 库,它允许您从您的帐户以编程方式进行操作。
这是来自 Quick Start 的最小化代码,它允许打印您的所有聊天记录。在那里你可以做任何你需要的过滤或处理。 即使你想在那里写点东西。
from telethon import TelegramClient
# Remember to use your own values from my.telegram.org!
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
# Getting information about yourself
me = await client.get_me()
# "me" is a user object. You can pretty-print
# any Telegram object with the "stringify" method:
print(me.stringify())
# You can print all the dialogs/conversations that you are part of:
async for dialog in client.iter_dialogs():
print(dialog.name, 'has ID', dialog.id)
with client:
client.loop.run_until_complete(main())