[英]Telethon Bot - how to check if a user is subscribed to my channel by user's id
Via my telegram bot I have to check if the user who texting the bot is in my telegram channel or not, I tried:通过我的电报机器人,我必须检查发短信给机器人的用户是否在我的电报频道中,我试过:
user_id = event.message.peer_id.user_id #user's id
async for i in bot.iter_participants(mychannelID, filter=ChannelParticipantsRecent):
if user_id != i.id:
status = False
else:
status = True
break
it worked only on my localhost, but on my Linux server it results an error:它只在我的本地主机上工作,但在我的 Linux 服务器上它会导致错误:
Traceback (most recent call last):
File "/home/runner/ConcernedHonoredVolume-rcmov/venv/lib/python3.8/site-packages/telethon/client/updates.py", line 467, in _dispatch_update
await callback(event)
File "main.py", line 226, in enter
async for i in bot.iter_participants(MAIN_CHANNEL_ID, filter=ChannelParticipantsRecent):
File "/home/runner/ConcernedHonoredVolume-rcmov/venv/lib/python3.8/site-packages/telethon/requestiter.py", line 58, in __anext__
if await self._init(**self.kwargs):
File "/home/runner/ConcernedHonoredVolume-rcmov/venv/lib/python3.8/site-packages/telethon/client/chats.py", line 111, in _init
entity = await self.client.get_input_entity(entity)
File "/home/runner/ConcernedHonoredVolume-rcmov/venv/lib/python3.8/site-packages/telethon/client/users.py", line 466, in get_input_entity
raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=******8242) (PeerUser). Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
I have already read entities.html as mentioned, but nothing is working.如前所述,我已经阅读了entities.html ,但没有任何效果。
Also I tried:我也试过:
result = await bot(functions.channels.GetParticipantsRequest(
channel='username',
filter=types.ChannelParticipantsRecent(),
offset=42,
limit=20,
hash=0
))
if user_id not in result:
...
but I keep getting TypeError: argument of type 'ChannelParticipants' is not iterable
但我不断收到TypeError: argument of type 'ChannelParticipants' is not iterable
It turns out that I had to re-add my bot to the channel, because the bot lost access hash to the channel when I moved it to another device, since access hash is stored in the .session
file and lost after the transfer.事实证明,我不得不将我的机器人重新添加到频道,因为当我将机器人移动到另一台设备时,机器人失去了对频道的访问权限 hash,因为访问权限 hash 存储在.session
文件中,并在传输后丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.