Telethon 在按钮之后写一个 message/start 聊天 - 机器人在 /start 之前发送消息

Telethon write a message/start a chat after button - bot send message before /start

我正在尝试使用 telethon 为电报编写一个访问机器人。

  1. 用户已添加到群组或通过邀请加入 link。
  2. 受到限制,必须按一个按钮。
  3. 机器人写了一条简单的验证码消息,如“1+2=?”
  4. 如果对了,这个人就不受限制,如果错了,就会被踢出去...

一切正常,但是:
如果新加入的用户不先写“/start”,则用户不会收到机器人的消息。

有没有可能这个人无论如何都会收到消息?

我读到在某处无法做到这一点,但外面有机器人如何做到这一点?

您可以检测用户何时加入events.ChatAction and restrict them from sending messages immediately with client.edit_permissions. In the group, you can let them know with a message with buttons that they have to solve a captcha with the bot in private, which you can react to with 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()