M.S*_*khi 12 telegram telegram-bot
任何人都可以开始我如何从我的电报机器人那里获得有关用户的信息.想象一下我的频道中管理员用户的机器人,我希望获得我的频道用户列表,或者在新用户加入时注意到.我怎样才能做到这一点.Telegram的文件如此无组织.到目前为止,我已经看过这些:
但这些都不是真的有帮助.
Telegram Bot不保留用户的任何信息.您应该自己保存与机器人通信的所有用户.例如,将其ID存储在数据库中.
如果是频道 - 您可以从频道的会员列表中获取此信息.
如果您需要得到通知 - 您的机器人应该将用户存储在某个位置并检查用户是否是新用户.
正如其他人已经提到的,您无法通过 Bot API 列出频道用户。
但是您可以使用MTProto API以普通用户身份登录,并以编程方式访问您在桌面或移动应用程序中可以看到的所有内容。
要使用 MTProto,您需要使用现有的 Telegram 帐户登录https://my.telegram.org/并获取凭据:api_id和api_hash。
这是一个如何使用Telethon python 库获取 Telegram 频道/组用户列表的工作示例。
from telethon import TelegramClient, sync
api_id = 'FILL REAL VALUES HERE'
api_hash = 'FILL REAL VALUES HERE'
client = TelegramClient('xxx', api_id, api_hash).start()
# get all the channels that I can access
channels = {d.entity.username: d.entity
for d in client.get_dialogs()
if d.is_channel}
# choose the one that I want list users from
channel = channels[channel_name]
# get all the users and print them
for u in client.get_participants(channel):
print(u.id, u.first_name, u.last_name, u.username)
Run Code Online (Sandbox Code Playgroud)
使用client.get_entity()可以很容易地按名称/电话/URL 搜索频道/用户。
小智 7
为了获取用户列表,您需要使用电报 API。
Telegram API 相当复杂。有一些客户可以更快地完成工作。
对于python,有Telethon,获取频道用户的代码是:
from telethon import TelegramClient
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.functions.channels import GetAdminLogRequest
from telethon.tl.types import InputChannel
from telethon.tl.types import ChannelAdminLogEventsFilter
from telethon.tl.types import InputUserSelf
from telethon.tl.types import InputUser
# 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 = ****** # Your api_id
api_hash = '********************************' # Your api_hash
phone_number = '+989122594574' # Your phone number
client = TelegramClient(phone_number, api_id, api_hash)
client.session.report_errors = False
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone_number)
client.sign_in(phone_number, input('Enter the code: '))
channel = client(ResolveUsernameRequest('tabe_eshgh')) # Your channel username
user = client(ResolveUsernameRequest('amir2b')) # Your channel admin username
admins = [InputUserSelf(), InputUser(user.users[0].id, user.users[0].access_hash)] # admins
admins = [] # No need admins for join and leave and invite filters
filter = None # All events
# param: (join, leave, invite, ban, unban, kick, unkick, promote, demote, info, settings, pinned, edit, delete)
filter = ChannelAdminLogEventsFilter(True, True, True, False, False, False, False, False, False, False, False, False, False, False)
result = client(GetAdminLogRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), '', 0, 0, 10, filter, admins))
##print(result)
for _user in result.users:
##print(_user.id)
with open(''.join(['users/', str(_user.id)]), 'w') as f:
f.write(str(_user.id))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
60447 次 |
| 最近记录: |