我正在尝试用telethon编写访问机器人进行电报。
一切正常,但是:
如果新加入的用户并没有先写“ / start”,则该用户不会从漫游器收到消息。
该人是否仍然有可能收到该消息?
我读到不可能在某个地方这样做,但是外面有机器人如何做到这一点?
答案 0 :(得分:1)
您可以检测到用户何时加入events.ChatAction
,并限制他们立即使用client.edit_permissions
发送消息。在小组中,您可以通过带有按钮的消息让他们知道,他们必须私下解决该机器人的验证码,您可以使用events.Message
对此做出反应。
from telethon import TelegramClient, Button, events
bot = TelegramClient(...)
async def main():
async with bot:
# Needed to find the username of the bot itself,
# to link users to a private conversation with it.
me = await bot.get_me()
@bot.on(events.ChatAction)
async def handler(event):
if event.user_joined:
# Don't let them send messages
await bot.edit_permissions(event.chat_id, event.user_id, send_messages=False)
# Send a message with URL button to start your bot with parameter "captcha"
url = f'https://t.me/{me.username}?start=captcha'
await event.reply(
'Welcome! Please solve a captcha before talking',
buttons=Button.url('Solve captcha', url))
# Detect when people start your bot with parameter "captcha"
@bot.on(events.NewMessage(pattern='/start captcha'))
async def handler(event):
# Make them solve whatever proof you want here
await event.respond('Please solve this captcha: `1+2 = ?`')
# ...more logic here to handle the rest or with more handlers...
await bot.run_until_disconnected()